Class: Omniauth::HubAz::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth/hub_az/hub_az/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt, public_key: nil, algorithm: nil) ⇒ Token

Returns a new instance of Token.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 11

def initialize(jwt, public_key: nil, algorithm: nil)
  public_key = Omniauth::HubAz.public_key if public_key.blank?
  algorithm = Omniauth::HubAz.algorithm if algorithm.blank?

  @decoded = JWT.decode(jwt, public_key, true, algorithm: algorithm)

  @payload = ActiveSupport::HashWithIndifferentAccess.new(@decoded[0])
  @header = ActiveSupport::HashWithIndifferentAccess.new(@decoded[1])

  @valid = true
rescue JWT::VerificationError, JWT::ExpiredSignature, JWT::DecodeError, JWT::IncorrectAlgorithm
  @payload = {}
  @header = {}
  @valid = false
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



5
6
7
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 5

def header
  @header
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 5

def payload
  @payload
end

Class Method Details

.verify!(jwt) ⇒ Object



7
8
9
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 7

def self.verify!(jwt)
  self.new(jwt)
end

Instance Method Details

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 35

def has_role?(role)
  roles.include?(role)
end

#rolesObject



31
32
33
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 31

def roles
  @payload[:roles] || []
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/omniauth/hub_az/hub_az/token.rb', line 27

def valid?
  !!@valid
end