Module: Touth::Authenticator
- Defined in:
- lib/touth/authenticator.rb
Class Method Summary collapse
- .gen_data_key(model, data_sign) ⇒ Object
- .get_model(token) ⇒ Object
- .issue_access_token(model, lifetime = Touth.access_token_lifetime) ⇒ Object
- .valid_access_token?(token) ⇒ Boolean
Class Method Details
.gen_data_key(model, data_sign) ⇒ Object
52 53 54 |
# File 'lib/touth/authenticator.rb', line 52 def gen_data_key(model, data_sign) Touth.digest [data_sign, model.send(Touth.password_field)].join end |
.get_model(token) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/touth/authenticator.rb', line 29 def get_model(token) @access_token_data_cache ||= {} model = @access_token_data_cache[token] return model if model begin data_sign, data_key, data = [token].pack('H*').unpack 'A32A32A*' if data_sign == Touth.digest(data) model_class, id, expires_at = Marshal.load data model = model_class.find id if gen_data_key(model, data_sign) == data_key && Time.now.to_i < expires_at @access_token_data_cache[token] = model end end rescue nil end end |
.issue_access_token(model, lifetime = Touth.access_token_lifetime) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/touth/authenticator.rb', line 6 def issue_access_token(model, lifetime = Touth.access_token_lifetime) expires_at = Time.now.to_i + lifetime data = Marshal.dump([ model.class, model.id, expires_at, ]) data_sign = Touth.digest data data_key = gen_data_key model, data_sign [ data_sign, data_key, data, ].join.unpack('H*')[0] end |
.valid_access_token?(token) ⇒ Boolean
25 26 27 |
# File 'lib/touth/authenticator.rb', line 25 def valid_access_token?(token) !!get_model(token) end |