Class: RailsIdentity::Session

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Repia::UUIDModel
Defined in:
app/models/rails_identity/session.rb

Instance Method Summary collapse

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.timestamp_create().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.

Returns:

  • (Boolean)


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

#roleObject



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