Module: Auth::Behavior::Core::PasswordMethods

Defined in:
lib/auth/behavior/core/password_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
# File 'lib/auth/behavior/core/password_methods.rb', line 2

def self.included(base)
  # so apparently dynamic methods haven't been generated by AR yet, so this stuff's been moved to
  # #after_initialize. Less than ideal but whatever.
#    base.send(:alias_method_chain, :secret=, :encryption)
#    base.send(:alias_method_chain, :secret_confirmation=, :encryption)
end

Instance Method Details

#after_initializeObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/auth/behavior/core/password_methods.rb', line 9

def after_initialize
  # FIXME: HACK - see self.included(base)
  
  if attributes.keys.include?('secret')
    self.secret # uh, makes AR define the method, I guess? This feels clunky...
  end
  
  class << self
    alias_method_chain :secret=, :encryption
    alias_method_chain :secret_confirmation=, :encryption
  end
end

#encrypt(p) ⇒ Object



41
42
43
44
# File 'lib/auth/behavior/core/password_methods.rb', line 41

def encrypt(p)
  self.salt ||= Auth::Token.new.to_s
  Auth.encryptor.encrypt(p, salt)
end

#expired?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/auth/behavior/core/password_methods.rb', line 37

def expired?
  authenticatable.password_expired?
end

#matches?(phrase) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/auth/behavior/core/password_methods.rb', line 46

def matches?(phrase)
  Auth.encryptor.matches?(secret, phrase, salt)
end

#reset_perishable_tokenObject



58
59
60
# File 'lib/auth/behavior/core/password_methods.rb', line 58

def reset_perishable_token
  self.perishable_token = Auth::Token.new.to_s
end

#reset_persistence_tokenObject



50
51
52
# File 'lib/auth/behavior/core/password_methods.rb', line 50

def reset_persistence_token
  self.persistence_token = Auth::Token.new.to_s
end

#reset_single_access_tokenObject



54
55
56
# File 'lib/auth/behavior/core/password_methods.rb', line 54

def reset_single_access_token
  self.single_access_token = Auth::Token.new.to_s
end

#secret_confirmation_with_encryption=(phrase) ⇒ Object



72
73
74
75
# File 'lib/auth/behavior/core/password_methods.rb', line 72

def secret_confirmation_with_encryption=(phrase)
  encrypted_phrase = phrase.blank? ? phrase : encrypt(phrase)
  self.secret_confirmation_without_encryption = encrypted_phrase
end

#secret_with_encryption=(phrase) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/auth/behavior/core/password_methods.rb', line 62

def secret_with_encryption=(phrase)
  @unencrypted_secret = phrase
  encrypted_phrase = phrase.blank? ? phrase : encrypt(phrase)
  self.secret_without_encryption = encrypted_phrase
  reset_persistence_token
  reset_single_access_token unless single_access_token # don't reset after it has a value
  reset_perishable_token
  return encrypted_phrase
end

#single_access_tokenObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/auth/behavior/core/password_methods.rb', line 22

def single_access_token
  current = super
  return current if current
  if authenticatable
    # authenticatable.passwords.last == self
    if (previous = authenticatable.passwords[-1]) != self
      if previous && previous.single_access_token
        return self.single_access_token = previous.single_access_token
      end
    end
  end
  
  nil
end

#unencrypted_secretObject



77
78
79
# File 'lib/auth/behavior/core/password_methods.rb', line 77

def unencrypted_secret
  @unencrypted_secret
end