Class: JWT::Claims::Audience

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

Overview

The Audience class is responsible for validating the audience claim (‘aud’) in a JWT token.

Instance Method Summary collapse

Constructor Details

#initialize(expected_audience:) ⇒ Audience

Initializes a new Audience instance.

Parameters:

  • expected_audience (String, Array<String>)

    the expected audience(s) for the JWT token.



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

def initialize(expected_audience:)
  @expected_audience = expected_audience
end

Instance Method Details

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

Verifies the audience claim (‘aud’) in the JWT token.

Parameters:

  • context (Object)

    the context containing the JWT payload.

  • _args (Hash)

    additional arguments (not used).

Returns:

  • (nil)

Raises:



20
21
22
23
# File 'lib/jwt/claims/audience.rb', line 20

def verify!(context:, **_args)
  aud = context.payload['aud']
  raise JWT::InvalidAudError, "Invalid audience. Expected #{expected_audience}, received #{aud || '<none>'}" if ([*aud] & [*expected_audience]).empty?
end