-
Notifications
You must be signed in to change notification settings - Fork 84
/
persistence.rb
81 lines (69 loc) · 2.15 KB
/
persistence.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module GoogleAuthenticatorRails
module Session
module Persistence
class TokenNotFound < StandardError; end
def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
end
end
end
module ClassMethods
def find
cookie = controller.cookies[cookie_key]
if cookie
token, user_id = parse_cookie(cookie).values_at(:token, :user_id)
conditions = { klass.google_lookup_token => token, :id => user_id }
record = __send__(finder, conditions).first
session = new(record)
session.valid? ? session : nil
else
nil
end
end
def create(user)
raise GoogleAuthenticatorRails::Session::Persistence::TokenNotFound if user.nil? || !user.respond_to?(user.class.google_lookup_token) || user.google_token_value.blank?
controller.cookies[cookie_key] = create_cookie(user.google_token_value, user.id)
new(user)
end
def destroy
controller.cookies.delete cookie_key
end
private
def finder
@_finder ||= klass.public_methods.include?(:where) ? :rails_3_finder : :rails_2_finder
end
def rails_3_finder(conditions)
klass.where(conditions)
end
def rails_2_finder(conditions)
klass.scoped(:conditions => conditions)
end
def klass
@_klass ||= "#{self.to_s.sub("MfaSession", "")}".constantize
end
def parse_cookie(cookie)
token, user_id = cookie.split('::')
{ :token => token, :user_id => user_id }
end
def create_cookie(token, user_id)
value = [token, user_id].join('::')
options = GoogleAuthenticatorRails.cookie_options || {}
options.merge(
:value => value,
:expires => GoogleAuthenticatorRails.time_until_expiration.from_now
)
end
def cookie_key
suffix = GoogleAuthenticatorRails.cookie_key_suffix || 'mfa_credentials'
"#{klass.to_s.downcase}_#{suffix}"
end
end
module InstanceMethods
def valid?
!record.nil?
end
end
end
end