Sha256: 289f80425fa71f95ea37f4e03c10475669bde291bd3e37d4251d43e1c80389e3

Contents?: true

Size: 1.82 KB

Versions: 13

Compression:

Stored size: 1.82 KB

Contents

require 'authentication/hash_helper'

module Authentication

  # This token module works mostly like the SingleToken module with
  # three differences:
  #
  # * It uses a different field name (remember_me CHAR(40))
  # * It doesn't care about any expiration time set
  # * It will only assign a token if the token name is /remember.?me/i
  #
  # This module is ideally suited for the remember me functionality
  # because of these changes. This module would probably not be
  # necessary if you are using a token module that supports more than
  # one token. Since the default one (SingleToken) only supports one
  # we need a seperate module for the remember me functionality so
  # we can basically now store two tokens by default.
  #
  # This module supports the same "verified_at" hidden feature that
  # the Authentication::SaltedHash module supports
  class RememberMe
    include HashHelper

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

      conditions = ['id = ? AND remember_me = ?', user.id, hash_string(key)]
      conditions[0] << ' AND verified_at IS NOT NULL' if user.respond_to? :verified_at
      0 < user.class.where(conditions).count
    end

    # Will create a new remember me token. We will ignore the expiration
    # since a remember me is always forever.
    def assign_token(user, name, expire=nil)
      return nil unless valid_model? user
      return nil unless name =~ /remember.?me/i

      token = hash_string "remember-me-#{Time.zone.now}"
      user.remember_me = hash_string token
      token
    end

    private

    # This functionality is only used if remember me an available column
    def valid_model?(user)
      user.class.column_names.include? 'remember_me'
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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