Class: JWTEasy::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_easy/decoder.rb

Overview

Decoder object for decoding tokens.

  • This is usually not instantiated directly, but rather by way of calling JWTEasy.decode.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, configuration = nil) ⇒ Decoder

Initializes a new encoder instance.

  • If no configuration object is passed or is nil, the value of JWTEasy.configuration is used as the configuration object

Parameters:

  • token (String)

    the token to be decoded

  • configuration (Configuration) (defaults to: nil)

    the configuration object



19
20
21
22
23
# File 'lib/jwt_easy/decoder.rb', line 19

def initialize(token, configuration = nil)
  @token          = token
  @configuration  = configuration || JWTEasy.configuration
  @headers        = {}
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/jwt_easy/decoder.rb', line 9

def configuration
  @configuration
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/jwt_easy/decoder.rb', line 9

def token
  @token
end

Instance Method Details

#decodeResult

Decodes the token with the configured options.

Returns:

  • (Result)

    the result of the decoding



28
29
30
# File 'lib/jwt_easy/decoder.rb', line 28

def decode
  Result.new(*JWT.decode(token, configuration.secret, verification?, headers))
end

#headersHash

Determines the headers to be used during decoding.

Returns:

  • (Hash)

    the headers to be used



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jwt_easy/decoder.rb', line 42

def headers
  @headers.tap do |headers|
    headers[:algorithm] = configuration.algorithm if verification?
    case configuration.claim
    when CLAIM_EXPIRATION_TIME
      headers.merge!(exp_headers)
    when CLAIM_NOT_BEFORE_TIME
      headers.merge!(nbf_headers)
    end
  end
end

#verification?Boolean

Determines if verification will be used during decoding.

Returns:

  • (Boolean)

    if verification will be used or not



35
36
37
# File 'lib/jwt_easy/decoder.rb', line 35

def verification?
  configuration.secret.nil? == false
end