diff --git a/lib/jwt/encode.rb b/lib/jwt/encode.rb index bda51bbd..35ddba79 100644 --- a/lib/jwt/encode.rb +++ b/lib/jwt/encode.rb @@ -13,7 +13,7 @@ def initialize(options) @payload = options[:payload] @key = options[:key] @algorithm = options[:algorithm] - @headers = options[:headers] + @headers = options[:headers].each_with_object({}) { |(key, value), headers| headers[key.to_s] = value } end def segments @@ -39,7 +39,8 @@ def encoded_header_and_payload end def encode_header - encode(@headers.merge(ALG_KEY => @algorithm)) + @headers[ALG_KEY] = @algorithm + encode(@headers) end def encode_payload diff --git a/spec/jwt_spec.rb b/spec/jwt_spec.rb index 1592c731..0fd5e64b 100644 --- a/spec/jwt_spec.rb +++ b/spec/jwt_spec.rb @@ -399,5 +399,9 @@ headers = JSON.parse(::JWT::Base64.url_decode(JWT.encode('Hello World', 'secret', 'HS256', { alg: 'HS123'}).split('.').first)) expect(headers['alg']).to eq('HS256') end + + it "should generate the same token" do + expect(JWT.encode('Hello World', 'secret', 'HS256', { alg: 'HS256'})).to eq JWT.encode('Hello World', 'secret', 'HS256') + end end end