class JWT::Claims::NotBefore

The NotBefore class is responsible for validating the 'nbf' (Not Before) claim in a JWT token.

Attributes

leeway[R]

Public Class Methods

new(leeway:) click to toggle source

Initializes a new NotBefore instance.

@param leeway [Integer] the amount of leeway (in seconds) to allow when validating the 'nbf' claim. Defaults to 0.

# File lib/jwt/claims/not_before.rb, line 10
def initialize(leeway:)
  @leeway = leeway || 0
end

Public Instance Methods

verify!(context:, **_args) click to toggle source

Verifies the 'nbf' (Not Before) claim in the JWT token.

@param context [Object] the context containing the JWT payload. @param _args [Hash] additional arguments (not used). @raise [JWT::ImmatureSignature] if the 'nbf' claim has not been reached. @return [nil]

# File lib/jwt/claims/not_before.rb, line 20
def verify!(context:, **_args)
  return unless context.payload.is_a?(Hash)
  return unless context.payload.key?('nbf')

  raise JWT::ImmatureSignature, 'Signature nbf has not been reached' if context.payload['nbf'].to_i > (Time.now.to_i + leeway)
end