13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'omf_common/lib/omf_common/auth/jwt_authenticator.rb', line 13
def self.parse(jwt_string)
jwt_string = jwt_string.split.join
case jwt_string.count('.')
when 2 , claims, signature = jwt_string.split('.', 3).collect do |segment|
UrlSafeBase64.decode64 segment.to_s
end
, claims = [, claims].collect do |json|
puts "JSON>>> #{json}"
JSON.parse(json, :symbolize_names => true)
end
signature_base_string = jwt_string.split('.')[0, 2].join('.')
jwt = JSON::JWT.new claims
jwt. =
jwt.signature = signature
unless issuer = claims[:iss]
warn "JWT: Message is missing :iss element"
return nil
end
cert = nil
issuer.split(',').compact.select do |addr|
begin
cert = OmfCommon::Auth::CertificateStore.instance.cert_for(addr)
rescue OmfCommon::Auth::MissingCertificateException
nil
end
end
unless cert
warn "JWT: Can't find cert for issuer '#{issuer}'"
return nil
end
unless OmfCommon::Auth::CertificateStore.instance.verify(cert)
warn "JWT: Invalid certificate '#{cert.to_s}', NOT signed by CA certs, or its CA cert NOT loaded into cert store."
end
jwt.verify signature_base_string, cert.to_x509.public_key
claims[:cnt]
else
warn('JWT: Invalid Format. JWT should include 2 or 3 dots.')
return nil
end
end
|