forked from jaredonline/google-authenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-authenticator-rails.rb
77 lines (60 loc) · 1.64 KB
/
google-authenticator-rails.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
# Stuff the gem requires
#
require 'active_support'
require 'active_record'
require 'openssl'
require 'rotp'
require 'google-qr'
# Stuff the gem is
#
GOOGLE_AUTHENTICATOR_RAILS_PATH = File.dirname(__FILE__) + "/google-authenticator-rails/"
[
"version",
"action_controller",
"active_record",
"session"
].each do |library|
require GOOGLE_AUTHENTICATOR_RAILS_PATH + library
end
# Sets up some basic accessors for use with the ROTP module
#
module GoogleAuthenticatorRails
# Drift is set to 6 because ROTP drift is not inclusive. This allows a drift of 5 seconds.
DRIFT = 6
# How long a Session::Persistence cookie should last.
@@time_until_expiration = 24.hours
# Last part of a Session::Persistence cookie's key
@@cookie_key_suffix = nil
# Additional configuration passed to a Session::Persistence cookie.
@@cookie_options = { :httponly => true }
def self.generate_password(secret, iteration)
ROTP::HOTP.new(secret).at(iteration)
end
def self.time_based_password(secret)
ROTP::TOTP.new(secret).now
end
def self.valid?(code, secret, drift = DRIFT)
ROTP::TOTP.new(secret).verify_with_drift(code, drift)
end
def self.generate_secret
ROTP::Base32.random_base32
end
def self.time_until_expiration
@@time_until_expiration
end
def self.time_until_expiration=(time_until_expiration)
@@time_until_expiration = time_until_expiration
end
def self.cookie_key_suffix
@@cookie_key_suffix
end
def self.cookie_key_suffix=(suffix)
@@cookie_key_suffix = suffix
end
def self.cookie_options
@@cookie_options
end
def self.cookie_options=(options)
@@cookie_options = options
end
end