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)


82
83
84
85
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
# File 'lib/sorcery/model.rb', line 82

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

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

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

  user = sorcery_adapter.find_by_credentials(credentials)

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

  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)

    return authentication_response(user: user, failure: reason, &block) unless success
  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.



113
114
115
116
117
118
119
120
# File 'lib/sorcery/model.rb', line 113

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.


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

def set_encryption_attributes
  if @sorcery_config.encryption_provider.respond_to?(:stretches) && @sorcery_config.stretches
    @sorcery_config.encryption_provider.stretches = @sorcery_config.stretches
  end
  if @sorcery_config.encryption_provider.respond_to?(:join_token) && @sorcery_config.salt_join_token
    @sorcery_config.encryption_provider.join_token = @sorcery_config.salt_join_token
  end
  return unless @sorcery_config.encryption_provider.respond_to?(:pepper) && @sorcery_config.pepper

  @sorcery_config.encryption_provider.pepper = @sorcery_config.pepper
end

#sorcery_configObject

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



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

def sorcery_config
  @sorcery_config
end