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)


52
53
54
55
56
57
# File 'lib/sorcery/model.rb', line 52

def authenticate(*credentials)
  raise ArgumentError, "at least 2 arguments required" if credentials.size < 2
  user = where("#{@sorcery_config.username_attribute_name} = ?", credentials[0]).first
  _salt = user.send(@sorcery_config.salt_attribute_name) if user && !@sorcery_config.salt_attribute_name.nil? && !@sorcery_config.encryption_provider.nil?
  user if user && @sorcery_config.before_authenticate.all? {|c| user.send(c)} && credentials_match?(user.send(@sorcery_config.crypted_password_attribute_name),credentials[1],_salt)
end

#credentials_match?(crypted, *tokens) ⇒ Boolean

Calls the configured encryption provider to compare the supplied password with the encrypted one.

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/sorcery/model.rb', line 60

def credentials_match?(crypted, *tokens)
  return crypted == tokens.join if @sorcery_config.encryption_provider.nil?
  @sorcery_config.encryption_provider.matches?(crypted, *tokens)
end

#encrypt(*tokens) ⇒ Object

encrypt tokens using current encryption_provider.



66
67
68
69
70
71
72
73
# File 'lib/sorcery/model.rb', line 66

def encrypt(*tokens)
  return tokens.first if @sorcery_config.encryption_provider.nil?
  
  @sorcery_config.encryption_provider.stretches = @sorcery_config.stretches if @sorcery_config.encryption_provider.respond_to?(:stretches) && @sorcery_config.stretches
  @sorcery_config.encryption_provider.join_token = @sorcery_config.salt_join_token if @sorcery_config.encryption_provider.respond_to?(:join_token) && @sorcery_config.salt_join_token
  CryptoProviders::AES256.key = @sorcery_config.encryption_key if @sorcery_config.encryption_algorithm == :aes256
  @sorcery_config.encryption_provider.encrypt(*tokens)
end

#sorcery_configObject

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



44
45
46
# File 'lib/sorcery/model.rb', line 44

def sorcery_config
  @sorcery_config
end