-
Notifications
You must be signed in to change notification settings - Fork 372
/
jwk.rb
36 lines (30 loc) · 910 Bytes
/
jwk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true
require_relative 'jwk/key_abstract'
require_relative 'jwk/rsa'
require_relative 'jwk/hmac'
require_relative 'jwk/key_finder'
module JWT
module JWK
MAPPINGS = {
'RSA' => ::JWT::JWK::RSA,
OpenSSL::PKey::RSA => ::JWT::JWK::RSA,
'oct' => ::JWT::JWK::HMAC,
String => ::JWT::JWK::HMAC
}.freeze
class << self
def import(jwk_data)
jwk_kty = jwk_data[:kty] || jwk_data['kty']
raise JWT::JWKError, 'Key type (kty) not provided' unless jwk_kty
MAPPINGS.fetch(jwk_kty.to_s) do |kty|
raise JWT::JWKError, "Key type #{kty} not supported"
end.import(jwk_data)
end
def create_from(keypair)
MAPPINGS.fetch(keypair.class) do |klass|
raise JWT::JWKError, "Cannot create JWK from a #{klass.name}"
end.new(keypair)
end
alias new create_from
end
end
end