Class: RailsIdentity::Session
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RailsIdentity::Session
- Includes:
- Repia::UUIDModel
- Defined in:
- app/models/rails_identity/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
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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/models/rails_identity/session.rb', line 15 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.
34 35 36 37 38 39 40 41 |
# File 'app/models/rails_identity/session.rb', line 34 def expired? begin JWT.decode self.token, nil, false rescue JWT::ExpiredSignature return true end return false end |
#role ⇒ Object
43 44 45 46 47 48 |
# File 'app/models/rails_identity/session.rb', line 43 def role if @role.nil? @role = user.role end return @role end |