Module: Sorcery::Model::ClassMethods

Defined in:
lib/sorcery/model.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(*credentials, &block) ⇒ 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)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sorcery/model.rb', line 86

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

  if credentials[0].blank?
    return authentication_response(return_value: false, failure: :invalid_login, &block)
  end

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

  user = sorcery_adapter.find_by_credentials(credentials)

  unless user
    return authentication_response(failure: :invalid_login, &block)
  end

  set_encryption_attributes

  if user.respond_to?(:active_for_authentication?) && !user.active_for_authentication?
    return authentication_response(user: user, failure: :inactive, &block)
  end

  @sorcery_config.before_authenticate.each do |callback|
    success, reason = user.send(callback)

    unless success
      return authentication_response(user: user, failure: reason, &block)
    end
  end

  unless user.valid_password?(credentials[1])
    return authentication_response(user: user, failure: :invalid_password, &block)
  end

  authentication_response(user: user, return_value: user, &block)
end

#encrypt(*tokens) ⇒ Object

encrypt tokens using current encryption_provider.



125
126
127
128
129
130
131
132
# File 'lib/sorcery/model.rb', line 125

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

#set_encryption_attributesObject

FIXME: This method of passing config to the hashing provider is

questionable, and has been refactored in Sorcery v1.


136
137
138
139
140
# File 'lib/sorcery/model.rb', line 136

def set_encryption_attributes
  @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
  @sorcery_config.encryption_provider.pepper = @sorcery_config.pepper if @sorcery_config.encryption_provider.respond_to?(:pepper) && @sorcery_config.pepper
end

#sorcery_configObject

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



78
79
80
# File 'lib/sorcery/model.rb', line 78

def sorcery_config
  @sorcery_config
end