|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module JWT |
| 4 | + module Claims |
| 5 | + DEFAULTS = { |
| 6 | + leeway: 0 |
| 7 | + }.freeze |
| 8 | + |
| 9 | + ClaimsContext = Struct.new(:payload, keyword_init: true) |
| 10 | + |
| 11 | + class << self |
| 12 | + def verify!(payload, options) |
| 13 | + options = DEFAULTS.merge(options) |
| 14 | + verify_aud(payload, options) |
| 15 | + verify_expiration(payload, options) |
| 16 | + verify_iat(payload, options) |
| 17 | + verify_iss(payload, options) |
| 18 | + verify_jti(payload, options) |
| 19 | + verify_not_before(payload, options) |
| 20 | + verify_sub(payload, options) |
| 21 | + verify_required_claims(payload, options) |
| 22 | + end |
| 23 | + |
| 24 | + def verify_aud(payload, options) |
| 25 | + return unless options[:verify_aud] |
| 26 | + |
| 27 | + Claims::Audience.new(expected_audience: options[:aud]).validate!(context: ClaimsContext.new(payload: payload)) |
| 28 | + end |
| 29 | + |
| 30 | + def verify_expiration(payload, options) |
| 31 | + return unless options[:verify_expiration] |
| 32 | + |
| 33 | + Claims::Expiration.new(leeway: options[:exp_leeway] || options[:leeway]).validate!(context: ClaimsContext.new(payload: payload)) |
| 34 | + end |
| 35 | + |
| 36 | + def verify_iat(payload, options) |
| 37 | + return unless options[:verify_iat] |
| 38 | + |
| 39 | + Claims::IssuedAt.new.validate!(context: ClaimsContext.new(payload: payload)) |
| 40 | + end |
| 41 | + |
| 42 | + def verify_iss(payload, options) |
| 43 | + return unless options[:verify_iss] |
| 44 | + |
| 45 | + Claims::Issuer.new(issuers: options[:iss]).validate!(context: ClaimsContext.new(payload: payload)) |
| 46 | + end |
| 47 | + |
| 48 | + def verify_jti(payload, options) |
| 49 | + return unless options[:verify_jti] |
| 50 | + |
| 51 | + Claims::JwtId.new(validator: options[:verify_jti]).validate!(context: ClaimsContext.new(payload: payload)) |
| 52 | + end |
| 53 | + |
| 54 | + def verify_not_before(payload, options) |
| 55 | + return unless options[:verify_not_before] |
| 56 | + |
| 57 | + Claims::NotBefore.new(leeway: options[:nbf_leeway] || options[:leeway]).validate!(context: ClaimsContext.new(payload: payload)) |
| 58 | + end |
| 59 | + |
| 60 | + def verify_sub(payload, options) |
| 61 | + return unless options[:verify_sub] |
| 62 | + return unless options[:sub] |
| 63 | + |
| 64 | + Claims::Subject.new(expected_subject: options[:sub]).validate!(context: ClaimsContext.new(payload: payload)) |
| 65 | + end |
| 66 | + |
| 67 | + def verify_required_claims(payload, options) |
| 68 | + return unless (options_required_claims = options[:required_claims]) |
| 69 | + |
| 70 | + Claims::Required.new(required_claims: options_required_claims).validate!(context: ClaimsContext.new(payload: payload)) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +require_relative 'claims/audience' |
| 77 | +require_relative 'claims/expiration' |
| 78 | +require_relative 'claims/issued_at' |
| 79 | +require_relative 'claims/issuer' |
| 80 | +require_relative 'claims/jwt_id' |
| 81 | +require_relative 'claims/not_before' |
| 82 | +require_relative 'claims/numeric' |
| 83 | +require_relative 'claims/required' |
| 84 | +require_relative 'claims/subject' |
0 commit comments