Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the KeyBase to register the different key types
- Loading branch information
Showing
5 changed files
with
52 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,50 @@ | ||
# 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| | ||
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| | ||
mappings.fetch(keypair.class) do |klass| | ||
raise JWT::JWKError, "Cannot create JWK from a #{klass.name}" | ||
end.new(keypair) | ||
end | ||
|
||
def classes | ||
@mappings = nil # reset the cached mappings | ||
@classes ||= [] | ||
end | ||
|
||
alias new create_from | ||
|
||
private | ||
|
||
def mappings | ||
@mappings ||= generate_mappings | ||
end | ||
|
||
def generate_mappings | ||
classes.each_with_object({}) do |klass, hash| | ||
next unless klass.const_defined?('KTYS') | ||
Array(klass::KTYS).each do |kty| | ||
hash[kty] = klass | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
require_relative 'jwk/key_base' | ||
require_relative 'jwk/rsa' | ||
require_relative 'jwk/hmac' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module JWK | ||
class KeyBase | ||
attr_reader :keypair, :kid | ||
|
||
def initialize(keypair, kid = nil) | ||
@keypair = keypair | ||
@kid = kid | ||
end | ||
|
||
def self.inherited(klass) | ||
::JWT::JWK.classes << klass | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters