Class: JWT::Verify

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

Overview

JWT verify methods

Constant Summary collapse

DEFAULTS =
{
  leeway: 0
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, options) ⇒ Verify

Returns a new instance of Verify.



27
28
29
30
# File 'lib/jwt/verify.rb', line 27

def initialize(payload, options)
  @payload = payload
  @options = DEFAULTS.merge(options)
end

Class Method Details

.verify_claims(payload, options) ⇒ Object



19
20
21
22
23
24
# File 'lib/jwt/verify.rb', line 19

def verify_claims(payload, options)
  options.each do |key, val|
    next unless key.to_s =~ /verify/
    Verify.send(key, payload, options) if val
  end
end

Instance Method Details

#verify_audObject



32
33
34
35
36
37
# File 'lib/jwt/verify.rb', line 32

def verify_aud
  return unless (options_aud = @options[:aud])

  aud = @payload['aud']
  raise(JWT::InvalidAudError, "Invalid audience. Expected #{options_aud}, received #{aud || '<none>'}") if ([*aud] & [*options_aud]).empty?
end

#verify_expirationObject



39
40
41
42
# File 'lib/jwt/verify.rb', line 39

def verify_expiration
  return unless @payload.include?('exp')
  raise(JWT::ExpiredSignature, 'Signature has expired') if @payload['exp'].to_i <= (Time.now.to_i - exp_leeway)
end

#verify_iatObject



44
45
46
47
48
49
# File 'lib/jwt/verify.rb', line 44

def verify_iat
  return unless @payload.include?('iat')

  iat = @payload['iat']
  raise(JWT::InvalidIatError, 'Invalid iat') if !iat.is_a?(Numeric) || iat.to_f > (Time.now.to_f + iat_leeway)
end

#verify_issObject



51
52
53
54
55
56
57
58
59
# File 'lib/jwt/verify.rb', line 51

def verify_iss
  return unless (options_iss = @options[:iss])

  iss = @payload['iss']

  return if Array(options_iss).map(&:to_s).include?(iss.to_s)

  raise(JWT::InvalidIssuerError, "Invalid issuer. Expected #{options_iss}, received #{iss || '<none>'}")
end

#verify_jtiObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jwt/verify.rb', line 61

def verify_jti
  options_verify_jti = @options[:verify_jti]
  jti = @payload['jti']

  if options_verify_jti.respond_to?(:call)
    verified = options_verify_jti.arity == 2 ? options_verify_jti.call(jti, @payload) : options_verify_jti.call(jti)
    raise(JWT::InvalidJtiError, 'Invalid jti') unless verified
  elsif jti.to_s.strip.empty?
    raise(JWT::InvalidJtiError, 'Missing jti')
  end
end

#verify_not_beforeObject



73
74
75
76
# File 'lib/jwt/verify.rb', line 73

def verify_not_before
  return unless @payload.include?('nbf')
  raise(JWT::ImmatureSignature, 'Signature nbf has not been reached') if @payload['nbf'].to_i > (Time.now.to_i + nbf_leeway)
end

#verify_subObject



78
79
80
81
82
# File 'lib/jwt/verify.rb', line 78

def verify_sub
  return unless (options_sub = @options[:sub])
  sub = @payload['sub']
  raise(JWT::InvalidSubError, "Invalid subject. Expected #{options_sub}, received #{sub || '<none>'}") unless sub.to_s == options_sub.to_s
end