Class: JWT::Decode

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt/decode.rb

Overview

The Decode class is responsible for decoding and verifying JWT tokens.

Constant Summary collapse

ALGORITHM_KEYS =

Order is very important - first check for string keys, next for symbols

['algorithm',
:algorithm,
'algorithms',
:algorithms].freeze

Instance Method Summary collapse

Constructor Details

#initialize(jwt, key, verify, options, &keyfinder) ⇒ Decode

Initializes a new Decode instance.

Parameters:

  • jwt (String)

    the JWT to decode.

  • key (String, Array<String>)

    the key(s) to use for verification.

  • verify (Boolean)

    whether to verify the token’s signature.

  • options (Hash)

    additional options for decoding and verification.

  • keyfinder (Proc)

    an optional key finder block to dynamically find the key for verification.

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/jwt/decode.rb', line 22

def initialize(jwt, key, verify, options, &keyfinder)
  raise JWT::DecodeError, 'Nil JSON web token' unless jwt

  @token = EncodedToken.new(jwt)
  @key = key
  @options = options
  @verify = verify
  @keyfinder = keyfinder
end

Instance Method Details

#decode_segmentsArray<Hash>

Decodes the JWT token and verifies its segments if verification is enabled.

Returns:

  • (Array<Hash>)

    an array containing the decoded payload and header.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jwt/decode.rb', line 35

def decode_segments
  validate_segment_count!
  if @verify
    verify_algo
    set_key
    verify_signature
    Claims::DecodeVerifier.verify!(token.unverified_payload, @options)
  end

  [token.unverified_payload, token.header]
end