Skip to content

Commit d39b60c

Browse files
bellebaumanakinj
authored andcommitted
JWK: More Documentation
1 parent 7221480 commit d39b60c

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,27 @@ JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwks})
612612

613613
### Importing and exporting JSON Web Keys
614614

615-
The ::JWT::JWK class can be used to import and export both the public key (default behaviour) and the private key. To include the private key in the export pass the `include_private` parameter to the export method.
615+
The ::JWT::JWK class can be used to import both JSON Web Keys and OpenSSL keys
616+
and export to either format with and without the private key included.
617+
618+
To include the private key in the export pass the `include_private` parameter to the export method.
616619

617620
```ruby
621+
# Import a JWK Hash (showing an HMAC example)
622+
jwk = JWT::JWK.new({ kty: 'oct', k: 'my-secret', kid: 'my-kid' })
623+
624+
# Import an OpenSSL key
618625
# You can optionally add descriptive parameters to the JWK
619626
desc_params = { kid: 'my-kid', use: 'sig' }
620627
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), desc_params)
621628

629+
# Export as JWK Hash (public key only by default)
622630
jwk_hash = jwk.export
623631
jwk_hash_with_private_key = jwk.export(include_private: true)
632+
633+
# Export as OpenSSL key
634+
public_key = jwk.public_key
635+
private_key = jwk.keypair if jwk.private?
624636
```
625637

626638
### Key ID (kid) and JWKs

lib/jwt/jwk.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
module JWT
66
module JWK
77
class << self
8-
def import(jwk_data)
9-
jwk_kty = jwk_data[:kty] || jwk_data['kty']
10-
raise JWT::JWKError, 'Key type (kty) not provided' unless jwk_kty
8+
def create_from(key, params = nil, options = {})
9+
if key.is_a?(Hash)
10+
jwk_kty = key[:kty] || key['kty']
11+
raise JWT::JWKError, 'Key type (kty) not provided' unless jwk_kty
1112

12-
mappings.fetch(jwk_kty.to_s) do |kty|
13-
raise JWT::JWKError, "Key type #{kty} not supported"
14-
end.import(jwk_data)
15-
end
13+
return mappings.fetch(jwk_kty.to_s) do |kty|
14+
raise JWT::JWKError, "Key type #{kty} not supported"
15+
end.new(key, params, options)
16+
end
1617

17-
def create_from(key, params = nil, options = {})
18-
mappings.fetch(keypair.class) do |klass|
18+
mappings.fetch(key.class) do |klass|
1919
raise JWT::JWKError, "Cannot create JWK from a #{klass.name}"
2020
end.new(key, params, options)
2121
end
@@ -26,6 +26,7 @@ def classes
2626
end
2727

2828
alias new create_from
29+
alias import create_from
2930

3031
private
3132

spec/integration/readme_examples_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,24 @@
370370
end
371371
end
372372

373+
it 'JWK import and export' do
374+
# Import a JWK Hash (showing an HMAC example)
375+
_jwk = JWT::JWK.new({ kty: 'oct', k: 'my-secret', kid: 'my-kid' })
376+
377+
# Import an OpenSSL key
378+
# You can optionally add descriptive parameters to the JWK
379+
desc_params = { kid: 'my-kid', use: 'sig' }
380+
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), desc_params)
381+
382+
# Export as JWK Hash (public key only by default)
383+
_jwk_hash = jwk.export
384+
_jwk_hash_with_private_key = jwk.export(include_private: true)
385+
386+
# Export as OpenSSL key
387+
_public_key = jwk.public_key
388+
_private_key = jwk.keypair if jwk.private?
389+
end
390+
373391
it 'JWK with thumbprint as kid via symbol' do
374392
JWT.configuration.jwk.kid_generator_type = :rfc7638_thumbprint
375393

0 commit comments

Comments
 (0)