File: jwt_id.rb

package info (click to toggle)
ruby-jwt 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 876 kB
  • sloc: ruby: 5,550; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module JWT
  module Claims
    # The JwtId class is responsible for validating the JWT ID claim ('jti') in a JWT token.
    class JwtId
      # Initializes a new JwtId instance.
      #
      # @param validator [#call] an object responding to `call` to validate the JWT ID.
      def initialize(validator:)
        @validator = validator
      end

      # Verifies the JWT ID claim ('jti') in the JWT token.
      #
      # @param context [Object] the context containing the JWT payload.
      # @param _args [Hash] additional arguments (not used).
      # @raise [JWT::InvalidJtiError] if the JWT ID claim is invalid or missing.
      # @return [nil]
      def verify!(context:, **_args)
        jti = context.payload['jti']
        if validator.respond_to?(:call)
          verified = validator.arity == 2 ? validator.call(jti, context.payload) : validator.call(jti)
          raise(JWT::InvalidJtiError, 'Invalid jti') unless verified
        elsif jti.to_s.strip.empty?
          raise(JWT::InvalidJtiError, 'Missing jti')
        end
      end

      private

      attr_reader :validator
    end
  end
end