Class: Cased::Authorization

Inherits:
Object
  • Object
show all
Defined in:
app/models/cased/authorization.rb

Defined Under Namespace

Classes: MissingApplicationKey

Constant Summary collapse

ALGORITHM =
'HS256'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, user_id:, issued_at:, expires_at:, issuer:) ⇒ Authorization

Returns a new instance of Authorization.



45
46
47
48
49
50
51
# File 'app/models/cased/authorization.rb', line 45

def initialize(user:, user_id:, issued_at:, expires_at:, issuer:)
  @user = user
  @user_id = user_id
  @issued_at = Time.at(issued_at)
  @expires_at = Time.at(expires_at)
  @issuer = issuer
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at.



42
43
44
# File 'app/models/cased/authorization.rb', line 42

def expires_at
  @expires_at
end

#issued_atObject (readonly)

Returns the value of attribute issued_at.



41
42
43
# File 'app/models/cased/authorization.rb', line 41

def issued_at
  @issued_at
end

#issuerObject (readonly)

Returns the value of attribute issuer.



43
44
45
# File 'app/models/cased/authorization.rb', line 43

def issuer
  @issuer
end

#userObject (readonly)

Returns the value of attribute user.



39
40
41
# File 'app/models/cased/authorization.rb', line 39

def user
  @user
end

#user_idObject (readonly)

Returns the value of attribute user_id.



40
41
42
# File 'app/models/cased/authorization.rb', line 40

def user_id
  @user_id
end

Class Method Details

.load!(token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/cased/authorization.rb', line 19

def self.load!(token)
  raise MissingApplicationKey if Cased.config.guard_application_key.blank?

  # JWT.decode will raise here if the token has expired or the application
  # key does not match meaning it has been tampered with.
  data, = JWT.decode(token, Cased.config.guard_application_key, true, algorithm: ALGORITHM)

  new(
    user: data.fetch('user'),
    user_id: data.fetch('user_id'),
    expires_at: data.fetch('exp'),
    issuer: data.fetch('iss'),
    issued_at: data.fetch('iat'),
  )
end

.validate!(token) ⇒ Object



35
36
37
# File 'app/models/cased/authorization.rb', line 35

def self.validate!(token)
  load!(token)
end

Instance Method Details

#to_paramObject



61
62
63
# File 'app/models/cased/authorization.rb', line 61

def to_param
  user_id
end

#to_sObject



57
58
59
# File 'app/models/cased/authorization.rb', line 57

def to_s
  user
end

#tokenObject



53
54
55
# File 'app/models/cased/authorization.rb', line 53

def token
  user_id
end