Class: Trainmaster::Session
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Trainmaster::Session
- Includes:
- Repia::Support::UUIDModel
- Defined in:
- app/models/trainmaster/session.rb
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Determines if the session has expired or not.
-
#initialize(attributes = {}) ⇒ Session
constructor
Creates a session object.
-
#role ⇒ Object
Returns the role of the session user.
Constructor Details
#initialize(attributes = {}) ⇒ Session
Creates a session object. The attributes must include user. The secret to the JWT is generated here and is unique to the session being created. Since the JWT includes the session id, the secret can be retrieved.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/trainmaster/session.rb', line 17 def initialize(attributes = {}) seconds = attributes.delete(:seconds) || (24 * 3600 * 14) super self.uuid = UUIDTools::UUID.().to_s iat = Time.now.to_i payload = { user_uuid: self.user.uuid, session_uuid: self.uuid, role: self.user.role, iat: iat, exp: iat + seconds } self.secret = UUIDTools::UUID.random_create self.token = JWT.encode(payload, self.secret, 'HS256') end |
Instance Method Details
#expired? ⇒ Boolean
Determines if the session has expired or not.
36 37 38 39 40 41 42 43 |
# File 'app/models/trainmaster/session.rb', line 36 def expired? begin JWT.decode self.token, nil, false rescue JWT::ExpiredSignature return true end return false end |
#role ⇒ Object
Returns the role of the session user.
48 49 50 51 52 53 |
# File 'app/models/trainmaster/session.rb', line 48 def role if !instance_variable_defined?(:@role) @role = user.role end return @role end |