Method: CF::UAA::TokenCoder.decode
- Defined in:
- lib/uaa/token_coder.rb
.decode(token, options = {}, obsolete1 = nil, obsolete2 = nil) ⇒ Hash
Decodes a JWT token and optionally verifies the signature. Both a symmetrical key and a public key can be provided for signature verification. The JWT header indicates what signature algorithm was used and the corresponding key is used to verify the signature (if verify is true).
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/uaa/token_coder.rb', line 95 def self.decode(token, = {}, obsolete1 = nil, obsolete2 = nil) unless .is_a?(Hash) && obsolete1.nil? && obsolete2.nil? # deprecated: def self.decode(token, skey = nil, pkey = nil, verify = true) warn "#{self.class}##{__method__} is deprecated with these parameters. Please use options hash." = {:skey => } [:pkey], [:verify] = obsolete1, obsolete2 end = () segments = token.split('.') raise InvalidTokenFormat, "Not enough or too many segments" unless [2,3].include? segments.length header_segment, payload_segment, crypto_segment = segments signing_input = [header_segment, payload_segment].join('.') header = Util.json_decode64(header_segment) payload = Util.json_decode64(payload_segment, (:sym if [:symbolize_keys])) return payload unless [:verify] raise SignatureNotAccepted, "Signature algorithm not accepted" unless [:accept_algorithms].include?(algo = header["alg"]) return payload if algo == 'none' signature = Util.decode64(crypto_segment) if ["HS256", "HS384", "HS512"].include?(algo) raise InvalidSignature, "Signature verification failed" unless [:skey] && signature == OpenSSL::HMAC.digest(init_digest(algo), [:skey], signing_input) elsif ["RS256", "RS384", "RS512"].include?(algo) raise InvalidSignature, "Signature verification failed" unless [:pkey] && [:pkey].verify(init_digest(algo), signature, signing_input) else raise SignatureNotSupported, "Algorithm not supported" end payload end |