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

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
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 JSONAPIUtils

#jsonapi_serializer_class_name

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

Class Method Details

.from_api_key(access, secret) ⇒ Object



74
75
76
77
# File 'lib/authify/api/models/user.rb', line 74

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



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

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



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

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

#admin_for?(organization) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/authify/api/models/user.rb', line 70

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

#authenticate(unencrypted_password) ⇒ Object



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

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

#set_verification_token!Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/authify/api/models/user.rb', line 52

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

  email_opts = {
    body: "Your verification token is: #{token}"
  }

  Resque.enqueue(
    Authify::Core::Jobs::Email,
    email,
    'Authify Verification Email',
    email_opts
  )
end

#verify(vtoken) ⇒ Object



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

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