Module: Sorcery::Model::ClassMethods

Defined in:
lib/sorcery/model.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(*credentials) ⇒ Object

The default authentication method. Takes a username and password, Finds the user by the username and compares the user’s password to the one supplied to the method. returns the user if success, nil otherwise.

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sorcery/model.rb', line 83

def authenticate(*credentials)
  raise ArgumentError, 'at least 2 arguments required' if credentials.size < 2

  return false if credentials[0].blank?

  if @sorcery_config.downcase_username_before_authenticating
    credentials[0].downcase!
  end

  user = sorcery_adapter.find_by_credentials(credentials)

  if user.respond_to?(:active_for_authentication?)
    return nil unless user.active_for_authentication?
  end

  set_encryption_attributes

  user if user && @sorcery_config.before_authenticate.all? { |c| user.send(c) } && user.valid_password?(credentials[1])
end

#encrypt(*tokens) ⇒ Object

encrypt tokens using current encryption_provider.



104
105
106
107
108
109
110
111
# File 'lib/sorcery/model.rb', line 104

def encrypt(*tokens)
  return tokens.first if @sorcery_config.encryption_provider.nil?

  set_encryption_attributes

  CryptoProviders::AES256.key = @sorcery_config.encryption_key
  @sorcery_config.encryption_provider.encrypt(*tokens)
end

#sorcery_configObject

Returns the class instance variable for configuration, when called by the class itself.



75
76
77
# File 'lib/sorcery/model.rb', line 75

def sorcery_config
  @sorcery_config
end