Skip to content

Commit 37a0d9e

Browse files
committed
Readme and example of usage
1 parent 8f87e8b commit 37a0d9e

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
**Features:**
55

6+
- Support custom algorithms by passing algorithm objects[#512](https://github.com/jwt/ruby-jwt/pull/512) ([@anakinj](https://github.com/anakinj)).
67
- Your contribution here
78

89
**Fixes and enhancements:**

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ decoded_token = JWT.decode token, rsa_public, true, { algorithm: 'PS256' }
211211
puts decoded_token
212212
```
213213

214+
### **Custom algorithms**
215+
216+
An object implementing custom signing or verification behaviour can be passed in the `algorithm` option when encoding and decoding. The given object needs to implement the method `valid_alg?` and `verify` and/or `alg` and `sign`, depending if object is used for encoding or decoding.
217+
218+
```ruby
219+
module CustomHS512Algorithm
220+
def self.alg
221+
'HS512'
222+
end
223+
224+
def self.valid_alg?(alg_to_validate)
225+
alg_to_validate == alg
226+
end
227+
228+
def self.sign(data:, signing_key:)
229+
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), data, signing_key)
230+
end
231+
232+
def self.verify(data:, signature:, verification_key:)
233+
::OpenSSL.secure_compare(sign(data: data, signing_key: verification_key), signature)
234+
end
235+
end
236+
237+
token = ::JWT.encode({'pay' => 'load'}, 'secret', CustomHS512Algorithm)
238+
payload, header = ::JWT.decode(token, 'secret', true, algorithm: CustomHS512Algorithm)
239+
```
240+
214241
## Support for reserved claim names
215242
JSON Web Token defines some reserved claim names and defines how they should be
216243
used. JWT supports these reserved claim names:

lib/jwt/algos.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def create(algorithm)
4040
end
4141

4242
def implementation?(algorithm)
43-
algorithm.respond_to?(:valid_alg?) &&
44-
(algorithm.respond_to?(:sign) || algorithm.respond_to?(:verify))
43+
(algorithm.respond_to?(:valid_alg?) && algorithm.respond_to?(:verify)) ||
44+
(algorithm.respond_to?(:alg) && algorithm.respond_to?(:sign))
4545
end
4646

4747
private

spec/integration/readme_examples_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,29 @@
352352
expect(jwk_hash[:kid].size).to eq(43)
353353
end
354354
end
355+
356+
context 'custom algorithm example' do
357+
it 'allows a module to be used as algorithm on encode and decode' do
358+
custom_hs512_alg = Module.new do
359+
def self.alg
360+
'HS512'
361+
end
362+
363+
def self.valid_alg?(alg_to_validate)
364+
alg_to_validate == alg
365+
end
366+
367+
def self.sign(data:, signing_key:)
368+
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), data, signing_key)
369+
end
370+
371+
def self.verify(data:, signature:, verification_key:)
372+
sign(data: data, signing_key: verification_key) == signature
373+
end
374+
end
375+
376+
token = ::JWT.encode({ 'pay' => 'load' }, 'secret', custom_hs512_alg)
377+
_payload, _header = ::JWT.decode(token, 'secret', true, algorithm: custom_hs512_alg)
378+
end
379+
end
355380
end

0 commit comments

Comments
 (0)