Skip to content

Commit 3f7e0cb

Browse files
committed
Example and more specs
1 parent 2bbf6bc commit 3f7e0cb

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Raise an error if the ECDSA signing or verification key is not an instance of `OpenSSL::PKey::EC` [#688](https://github.com/jwt/ruby-jwt/pull/688) ([@anakinj](https://github.com/anakinj))
1111
- Allow `OpenSSL::PKey::EC::Point` to be used as the verification key in ECDSA [#689](https://github.com/jwt/ruby-jwt/pull/689) ([@anakinj](https://github.com/anakinj))
1212
- Require claims to have been verified before accessing the `JWT::EncodedToken#payload`
13-
- Resolve the algorithm based on the alg JWK parameter [#692](https://github.com/jwt/ruby-jwt/pull/692) ([@anakinj](https://github.com/anakinj))
13+
- Resolve the verification algorithm based on the JWK "alg" parameter [#692](https://github.com/jwt/ruby-jwt/pull/692) ([@anakinj](https://github.com/anakinj))
1414
- Your contribution here
1515

1616
**Fixes and enhancements:**

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ See [CHANGELOG.md](CHANGELOG.md) for a complete set of changes and [upgrade guid
1313

1414
## Sponsors
1515

16-
|Logo|Message|
17-
|----|-------|
18-
|![auth0 logo](https://user-images.githubusercontent.com/83319/31722733-de95bbde-b3ea-11e7-96bf-4f4e8f915588.png)|If you want to quickly add secure token-based authentication to Ruby projects, feel free to check Auth0's Ruby SDK and free plan at [auth0.com/developers](https://auth0.com/developers?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=rubyjwt&utm_content=auth)|
16+
| Logo | Message |
17+
| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
18+
| ![auth0 logo](https://user-images.githubusercontent.com/83319/31722733-de95bbde-b3ea-11e7-96bf-4f4e8f915588.png) | If you want to quickly add secure token-based authentication to Ruby projects, feel free to check Auth0's Ruby SDK and free plan at [auth0.com/developers](https://auth0.com/developers?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=rubyjwt&utm_content=auth) |
1919

2020
## Installing
2121

@@ -251,6 +251,21 @@ encoded_token.payload # => { 'exp'=>1234, 'jti'=>'1234", 'sub'=>'my-subject' }
251251
encoded_token.header # {'kid'=>'hmac', 'alg'=>'HS256'}
252252
```
253253

254+
A JWK can be used to verify the token, the alg parameter is used to resolve the JWA to be used.
255+
256+
```ruby
257+
jwk_json = '{
258+
"kty": "oct",
259+
"k": "c2VjcmV0",
260+
"alg": "HS256",
261+
"kid": "hmac"
262+
}'
263+
264+
jwk = JWT::JWK.import(JSON.parse(jwk_json))
265+
encoded_token = JWT::EncodedToken.new(token.jwt)
266+
encoded_token.verify!(signature: { key: jwk})
267+
```
268+
254269
#### Using a keyfinder
255270

256271
A keyfinder can be used to verify a signature. A keyfinder is an object responding to the `#call` method. The method expects to receive one argument, which is the token to be verified.

lib/jwt/encoded_token.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def valid_signature?(algorithm: nil, key: nil, key_finder: nil)
156156
keys = Array(key || key_finder.call(self))
157157
verifiers = JWA.create_verifiers(algorithms: algorithm, keys: keys, preferred_algorithm: header['alg'])
158158

159+
raise JWT::VerificationError, 'No algorithm provided' if verifiers.empty?
160+
159161
valid = verifiers.any? do |jwa|
160162
jwa.verify(data: signing_input, signature: signature)
161163
end

spec/jwt/encoded_token_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@
139139
end
140140
end
141141

142+
context 'when algorithm is not given' do
143+
it 'raises an error' do
144+
expect { token.verify_signature!(key: 'secret') }.to raise_error(JWT::VerificationError, 'No algorithm provided')
145+
end
146+
end
147+
142148
context 'when header has invalid alg value' do
143149
let(:header) { { 'alg' => 'HS123' } }
144150

0 commit comments

Comments
 (0)