lib/json/jwt.rb in json-jwt-1.9.4 vs lib/json/jwt.rb in json-jwt-1.10.0
- old
+ new
@@ -4,10 +4,11 @@
require 'active_support/core_ext'
require 'json/jose'
module JSON
class JWT < ActiveSupport::HashWithIndifferentAccess
+ attr_accessor :blank_payload
attr_accessor :signature
class Exception < StandardError; end
class InvalidFormat < Exception; end
class VerificationFailed < Exception; end
@@ -17,12 +18,14 @@
def initialize(claims = {})
@content_type = 'application/jwt'
self.typ = :JWT
self.alg = :none
- [:exp, :nbf, :iat].each do |key|
- claims[key] = claims[key].to_i if claims[key]
+ unless claims.nil?
+ [:exp, :nbf, :iat].each do |key|
+ claims[key] = claims[key].to_i if claims[key]
+ end
end
update claims
end
def sign(private_key_or_secret, algorithm = :autodetect)
@@ -69,32 +72,48 @@
else
super
end
end
+ def to_json *args
+ if @blank_payload && args.empty?
+ ''
+ else
+ super
+ end
+ end
+
+ def update claims
+ if claims.nil?
+ @blank_payload = true
+ else
+ super
+ end
+ end
+
def pretty_generate
[
JSON.pretty_generate(header),
JSON.pretty_generate(self)
]
end
class << self
- def decode_compact_serialized(jwt_string, key_or_secret, algorithms = nil, encryption_methods = nil)
+ def decode_compact_serialized(jwt_string, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false)
case jwt_string.count('.') + 1
when JWS::NUM_OF_SEGMENTS
- JWS.decode_compact_serialized jwt_string, key_or_secret, algorithms
+ JWS.decode_compact_serialized jwt_string, key_or_secret, algorithms, allow_blank_payload
when JWE::NUM_OF_SEGMENTS
JWE.decode_compact_serialized jwt_string, key_or_secret, algorithms, encryption_methods
else
raise InvalidFormat.new("Invalid JWT Format. JWT should include #{JWS::NUM_OF_SEGMENTS} or #{JWE::NUM_OF_SEGMENTS} segments.")
end
end
- def decode_json_serialized(input, key_or_secret, algorithms = nil, encryption_methods = nil)
+ def decode_json_serialized(input, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false)
input = input.with_indifferent_access
if (input[:signatures] || input[:signature]).present?
- JWS.decode_json_serialized input, key_or_secret, algorithms
+ JWS.decode_json_serialized input, key_or_secret, algorithms, allow_blank_payload
elsif input[:ciphertext].present?
JWE.decode_json_serialized input, key_or_secret, algorithms, encryption_methods
else
raise InvalidFormat.new("Unexpected JOSE JSON Serialization Format.")
end