Sha256: 73ddf6ccff5684401f46e99c9c674b1a1693b260539689fa3a1286f2d05f55d6

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

require 'authentication/hash_helper'

module Authentication
  # Implements a token with expiration that is stored on the model
  # being authenticated. This is designed to implement the common
  # practice of having a token in the URL that will automatically
  # authenticate the user. 
  #
  # The model should implement the fields "token_key" (a 40 character
  # field) and "token_expiration" (a datetime field). If they are not
  # implemented this class cannot authenticate or assign tokens.
  #
  # This token module is called SingleToken because it only can
  # store one token. If another token is assigned the first is lost
  # and will not authenticate the user anymore. For common needs such
  # as forgot my password and account restoration this is fine.
  #
  # This token does NOT honor the verified_at field that the
  # Authentication::SaltedHash module and Authentication::RememberMe
  # module do since this token may be used to actually implement the
  # email verification.
  class SingleToken
    include HashHelper

    # Will test to see if the given key is valid for the given user
    def authenticate(user, key)
      return false unless valid_model? user
      return false unless key.to_s.length == 40

      conditions = [
        'id = ? AND token_key = ? AND (token_expiration >= ? OR token_expiration IS NULL)',
        user.id, hash_string(key), Time.zone.now
      ]
      0 < user.class.where(conditions).count
    end

    # Will create a new token for the given user with the given expiration
    def assign_token(user, name, expire)
      return nil unless valid_model? user

      user.token_expiration = expire
      token = hash_string "token-#{Time.zone.now}"
      user.token_key = hash_string token
      token
    end

    private

    def valid_model?(user)
      user.class.includes_all_columns? :token_key, :token_expiration
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
adva-0.3.2 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.3.1 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.3.0 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.2.4 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.2.3 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.2.2 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.2.1 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.2.0 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.1.4 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.1.3 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.1.2 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.1.1 vendor/gems/authentication/lib/authentication/single_token.rb
adva-0.1.0 vendor/gems/authentication/lib/authentication/single_token.rb