|
9 | 9 | require_relative 'claims/numeric' |
10 | 10 | require_relative 'claims/required' |
11 | 11 | require_relative 'claims/subject' |
| 12 | +require_relative 'claims/decode_verifier' |
| 13 | +require_relative 'claims/verifier' |
12 | 14 |
|
13 | 15 | module JWT |
| 16 | + # JWT Claim verifications |
| 17 | + # https://datatracker.ietf.org/doc/html/rfc7519#section-4 |
| 18 | + # |
| 19 | + # Verification is supported for the following claims: |
| 20 | + # exp |
| 21 | + # nbf |
| 22 | + # iss |
| 23 | + # iat |
| 24 | + # jti |
| 25 | + # aud |
| 26 | + # sub |
| 27 | + # required |
| 28 | + # numeric |
| 29 | + # |
14 | 30 | module Claims |
15 | | - VerificationContext = Struct.new(:payload, keyword_init: true) |
16 | | - |
17 | | - VERIFIERS = { |
18 | | - verify_expiration: ->(options) { Claims::Expiration.new(leeway: options[:exp_leeway] || options[:leeway]) }, |
19 | | - verify_not_before: ->(options) { Claims::NotBefore.new(leeway: options[:nbf_leeway] || options[:leeway]) }, |
20 | | - verify_iss: ->(options) { options[:iss] && Claims::Issuer.new(issuers: options[:iss]) }, |
21 | | - verify_iat: ->(*) { Claims::IssuedAt.new }, |
22 | | - verify_jti: ->(options) { Claims::JwtId.new(validator: options[:verify_jti]) }, |
23 | | - verify_aud: ->(options) { options[:aud] && Claims::Audience.new(expected_audience: options[:aud]) }, |
24 | | - verify_sub: ->(options) { options[:sub] && Claims::Subject.new(expected_subject: options[:sub]) }, |
25 | | - required_claims: ->(options) { Claims::Required.new(required_claims: options[:required_claims]) } |
26 | | - }.freeze |
| 31 | + # Represents a claim verification error |
| 32 | + Error = Struct.new(:message, keyword_init: true) |
27 | 33 |
|
28 | 34 | class << self |
| 35 | + # @deprecated Use {verify_payload!} instead. Will be removed in the next major version of ruby-jwt. |
29 | 36 | def verify!(payload, options) |
30 | | - VERIFIERS.each do |key, verifier_builder| |
31 | | - next unless options[key] || options[key.to_s] |
| 37 | + Deprecations.warning('Calling ::JWT::Claims::verify! will be removed in the next major version of ruby-jwt') |
| 38 | + DecodeVerifier.verify!(payload, options) |
| 39 | + end |
| 40 | + |
| 41 | + # Checks if the claims in the JWT payload are valid. |
| 42 | + # @example |
| 43 | + # |
| 44 | + # ::JWT::Claims.verify_payload!({"exp" => Time.now.to_i + 10}, :exp) |
| 45 | + # ::JWT::Claims.verify_payload!({"exp" => Time.now.to_i - 10}, exp: { leeway: 11}) |
| 46 | + # |
| 47 | + # @param payload [Hash] the JWT payload. |
| 48 | + # @param options [Array] the options for verifying the claims. |
| 49 | + # @return [void] |
| 50 | + # @raise [JWT::DecodeError] if any claim is invalid. |
| 51 | + def verify_payload!(payload, *options) |
| 52 | + verify_token!(VerificationContext.new(payload: payload), *options) |
| 53 | + end |
| 54 | + |
| 55 | + # Checks if the claims in the JWT payload are valid. |
| 56 | + # |
| 57 | + # @param payload [Hash] the JWT payload. |
| 58 | + # @param options [Array] the options for verifying the claims. |
| 59 | + # @return [Boolean] true if the claims are valid, false otherwise |
| 60 | + def valid_payload?(payload, *options) |
| 61 | + payload_errors(payload, *options).empty? |
| 62 | + end |
| 63 | + |
| 64 | + # Returns the errors in the claims of the JWT token. |
| 65 | + # |
| 66 | + # @param options [Array] the options for verifying the claims. |
| 67 | + # @return [Array<JWT::Claims::Error>] the errors in the claims of the JWT |
| 68 | + def payload_errors(payload, *options) |
| 69 | + token_errors(VerificationContext.new(payload: payload), *options) |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def verify_token!(token, *options) |
| 75 | + Verifier.verify!(token, *options) |
| 76 | + end |
32 | 77 |
|
33 | | - verifier_builder&.call(options)&.verify!(context: VerificationContext.new(payload: payload)) |
34 | | - end |
35 | | - nil |
| 78 | + def token_errors(token, *options) |
| 79 | + Verifier.errors(token, *options) |
36 | 80 | end |
37 | 81 | end |
38 | 82 | end |
|
0 commit comments