Class: JWT::Claims::Expiration

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

Overview

The Expiration class is responsible for validating the expiration claim (‘exp’) in a JWT token.

Instance Method Summary collapse

Constructor Details

#initialize(leeway:) ⇒ Expiration

Initializes a new Expiration instance.

Parameters:

  • leeway (Integer)

    the amount of leeway (in seconds) to allow when validating the expiration time. Default: 0.



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

def initialize(leeway:)
  @leeway = leeway || 0
end

Instance Method Details

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

Verifies the expiration claim (‘exp’) 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
24
25
# File 'lib/jwt/claims/expiration.rb', line 20

def verify!(context:, **_args)
  return unless context.payload.is_a?(Hash)
  return unless context.payload.key?('exp')

  raise JWT::ExpiredSignature, 'Signature has expired' if context.payload['exp'].to_i <= (Time.now.to_i - leeway)
end