Class: JWT::Claims::Required

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

Overview

The Required class is responsible for validating that all required claims are present in a JWT token.

Instance Method Summary collapse

Constructor Details

#initialize(required_claims:) ⇒ Required

Initializes a new Required instance.

Parameters:

  • required_claims (Array<String>)

    the list of required claims.



10
11
12
# File 'lib/jwt/claims/required.rb', line 10

def initialize(required_claims:)
  @required_claims = required_claims
end

Instance Method Details

#verify!(context:, **_args) ⇒ nil

Verifies that all required claims are present in the JWT payload.

Parameters:

  • context (Object)

    the context containing the JWT payload.

  • _args (Hash)

    additional arguments (not used).

Returns:

  • (nil)

Raises:



20
21
22
23
24
25
26
# File 'lib/jwt/claims/required.rb', line 20

def verify!(context:, **_args)
  required_claims.each do |required_claim|
    next if context.payload.is_a?(Hash) && context.payload.key?(required_claim)

    raise JWT::MissingRequiredClaim, "Missing required claim #{required_claim}"
  end
end