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.

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:



17
18
19
20
21
22
23
24
25
# File 'lib/jwt/decode.rb', line 17

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.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jwt/decode.rb', line 30

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

  [token.payload, token.header]
end