Class: Warden::Cognito::TokenDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/warden/cognito/token_decoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, pool_identifier = nil) ⇒ TokenDecoder

Returns a new instance of TokenDecoder.



6
7
8
9
# File 'lib/warden/cognito/token_decoder.rb', line 6

def initialize(token, pool_identifier = nil)
  @token = token
  @jwk_loader = find_loader(pool_identifier)
end

Instance Attribute Details

#jwk_loaderObject (readonly)

Returns the value of attribute jwk_loader.



4
5
6
# File 'lib/warden/cognito/token_decoder.rb', line 4

def jwk_loader
  @jwk_loader
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/warden/cognito/token_decoder.rb', line 4

def token
  @token
end

Instance Method Details

#cognito_userObject



24
25
26
# File 'lib/warden/cognito/token_decoder.rb', line 24

def cognito_user
  @cognito_user ||= CognitoClient.scope(pool_identifier).fetch(token)
end

#decoded_tokenObject



19
20
21
22
# File 'lib/warden/cognito/token_decoder.rb', line 19

def decoded_token
  @decoded_token ||= ::JWT.decode(token, nil, true, iss: jwk_loader.jwt_issuer, verify_iss: true,
                                                    algorithms: ['RS256'], jwks: jwk_loader)
end

#pool_identifierObject



32
33
34
# File 'lib/warden/cognito/token_decoder.rb', line 32

def pool_identifier
  jwk_loader.pool_identifier
end

#subObject



15
16
17
# File 'lib/warden/cognito/token_decoder.rb', line 15

def sub
  decoded_token.first['sub']
end

#user_attribute(attribute_name) ⇒ Object



28
29
30
# File 'lib/warden/cognito/token_decoder.rb', line 28

def user_attribute(attribute_name)
  token_attribute(attribute_name).presence || cognito_user_attribute(attribute_name)
end

#validate!Object



11
12
13
# File 'lib/warden/cognito/token_decoder.rb', line 11

def validate!
  decoded_token.present?
end