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



84
85
86
87
# File 'lib/authify/api/models/user.rb', line 84

def self.from_api_key(access, secret)
  key = APIKey.find_by_access_key(access)
  key.user if key && key.compare_secret(secret) && key.user.verified?
end

.from_email(email, password) ⇒ Object



89
90
91
92
# File 'lib/authify/api/models/user.rb', line 89

def self.from_email(email, password)
  found_user = Models::User.find_by_email(email)
  found_user if found_user && found_user.authenticate(password) && found_user.verified?
end

.from_identity(provider, uid) ⇒ Object



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

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

Instance Method Details

#add_verification_token!(opts = {}) ⇒ Object

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



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

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)


80
81
82
# File 'lib/authify/api/models/user.rb', line 80

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

#authenticate(unencrypted_password) ⇒ Object



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

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



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

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