Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure alg parameter value isn't added twice #297

Merged
merged 4 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/jwt/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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