Class: Authify::API::Models::User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Helpers::TextProcessing, JSONAPIUtils, Core::SecureHashing
Defined in:
lib/authify/api/models/user.rb

Overview

A User of the system

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::TextProcessing

#decoded_hash, #dehandlebar, #from_base64, #human_readable, #valid_formats

Methods included from JSONAPIUtils

#jsonapi_serializer_class_name

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/authify/api/models/user.rb', line 10

def password
  @password
end

Class Method Details

.from_api_key(access, secret) ⇒ Object



86
87
88
89
90
# File 'lib/authify/api/models/user.rb', line 86

def self.from_api_key(access, secret)
  key = APIKey.find_by_access_key(access)
  verification_truthiness = (key.user.verified? || !CONFIG[:verifications][:required])
  key.user if key && key.compare_secret(secret) && verification_truthiness
end

.from_email(email, password) ⇒ Object



92
93
94
95
96
# File 'lib/authify/api/models/user.rb', line 92

def self.from_email(email, password)
  found_user = Models::User.find_by_email(email)
  verification_truthiness = (found_user.verified? || !CONFIG[:verifications][:required])
  found_user if found_user && found_user.authenticate(password) && verification_truthiness
end

.from_identity(provider, uid) ⇒ Object



98
99
100
101
# File 'lib/authify/api/models/user.rb', line 98

def self.from_identity(provider, uid)
  provided_identity = Identity.find_by_provider_and_uid(provider, uid)
  provided_identity.user if provided_identity
end

.uniq_handle_generator(name, email) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/authify/api/models/user.rb', line 103

def self.uniq_handle_generator(name, email)
  possibilities = [email.split('@').first.downcase.gsub(/[._-]/, '')]
  possibilities << name.downcase.gsub(/[.-]/, '_') if name && !name.empty?
  possibilities.each do |possibility|
    return possibility unless find_by_handle(possibility)
  end
  100.times do
    possibilities.each do |possibility|
      rando_num = rand(9999)
      attempt   = "#{possibility.downcase.gsub(/[.-]/, '_')}#{rando_num}"
      return attempt unless find_by_handle(attempt)
    end
  end
  false # didn't work if we got here
end

Instance Method Details

#add_verification_token!(opts = {}) ⇒ Object

Both sets a token in the DB and emails it to the user



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/authify/api/models/user.rb', line 55

def add_verification_token!(opts = {})
  return false if verified?
  token = peppered_sha512(rand(999).to_s)[0...16]
  valid_time  = Time.now + (15 * 60)
  valid_until = valid_time.to_i
  self.verification_token = "#{token}:#{valid_until}"

  subdata = { 'token' => token, 'valid_until' => valid_time }

  email_opts = {
    body: if opts.key?(:body)
            dehandlebar(opts[:body], subdata)
          else
            "Your verification token is: #{token}"
          end
  }

  email_opts[:html_body] = dehandlebar(opts[:html_body], subdata) if opts.key?(:html_body)
  subject = if opts.key?(:subject)
              dehandlebar(opts[:subject], subdata)
            else
              'Authify Verification Email'
            end

  Resque.enqueue Authify::Core::Jobs::Email, email, subject, email_opts
end

#admin_for?(organization) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/authify/api/models/user.rb', line 82

def admin_for?(organization)
  admin? || organization.admins.include?(self)
end

#authenticate(unencrypted_password) ⇒ Object



42
43
44
45
46
# File 'lib/authify/api/models/user.rb', line 42

def authenticate(unencrypted_password)
  return false unless unencrypted_password && !unencrypted_password.empty?
  return false unless password_digest && !password_digest.empty?
  compare_salted_sha512(unencrypted_password, password_digest)
end

#verify(vtoken) ⇒ Object



48
49
50
51
52
# File 'lib/authify/api/models/user.rb', line 48

def verify(vtoken)
  return false unless verification_token
  token, valid_until = verification_token.split(':')
  token == vtoken && Time.now.to_i <= Integer(valid_until)
end