Class: Challah::PasswordProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/challah/providers/password_provider.rb

Class Method Summary collapse

Class Method Details

.save(user) ⇒ Object



3
4
5
# File 'lib/challah/providers/password_provider.rb', line 3

def self.save(user)
  set(uid: user.username, token: user.password, user_id: user.id, authorization: user.class.authorization_model)
end

.set(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/challah/providers/password_provider.rb', line 7

def self.set(options = {})
  user_id     = options.fetch(:user_id)
  uid         = options.fetch(:uid, '')
  token       = options.fetch(:token, '')
  auth_model  = options.fetch(:authorization, ::Authorization)

  if token.to_s.blank?
    authorization = auth_model.get({
      user_id:  user_id,
      provider: :password
    })

    if authorization
      token = authorization.token
    end
  else
    token = Challah::Encrypter.encrypt(token)
  end

  auth_model.set({
    provider: :password,
    user_id:  user_id,
    uid:      uid,
    token:    token
  })
end

.valid?(record) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/challah/providers/password_provider.rb', line 34

def self.valid?(record)
  password_validator = Challah.options[:password_validator]
  password_validator.new(force: true).validate(record)
  record.errors[:password].size.zero?
end