Class: JWT::Claims::Subject

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

Overview

The Subject class is responsible for validating the subject claim (‘sub’) in a JWT token.

Instance Method Summary collapse

Constructor Details

#initialize(expected_subject:) ⇒ Subject

Initializes a new Subject instance.

Parameters:

  • expected_subject (String)

    the expected subject for the JWT token.



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

def initialize(expected_subject:)
  @expected_subject = expected_subject.to_s
end

Instance Method Details

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

Verifies the subject claim (‘sub’) 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/subject.rb', line 20

def verify!(context:, **_args)
  sub = context.payload['sub']
  raise(JWT::InvalidSubError, "Invalid subject. Expected #{expected_subject}, received #{sub || '<none>'}") unless sub.to_s == expected_subject
end