Class: JWT::JWK::AKP

Inherits:
KeyBase
  • Object
show all
Defined in:
lib/pq_crypto/jwt/jwk/akp.rb

Defined Under Namespace

Classes: NullKidGenerator

Constant Summary collapse

KTY =
"AKP".freeze
KTYS =
[KTY, PQCrypto::Signature::PublicKey, JWT::JWK::AKP].freeze
AKP_KEY_ELEMENTS =
%i[kty alg pub priv].freeze
PRIVATE_EXPORT_OPTIONS =
%i[private include_private].freeze

Instance Method Summary collapse

Constructor Details

#initialize(key, params = nil, options = {}) ⇒ AKP

Returns a new instance of AKP.



18
19
20
21
22
23
24
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 18

def initialize(key, params = nil, options = {})
  params = params.is_a?(String) ? { kid: params } : (params || {})
  params = params.transform_keys(&:to_sym)
  key_params = extract_key_params(key)
  @checked_public_key = check_jwk_params!(key_params, params)
  super({ kid_generator: NullKidGenerator }.merge(options || {}), key_params.merge(params))
end

Instance Method Details

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 54

def []=(key, value)
  raise ArgumentError, "cannot overwrite cryptographic key attributes" if AKP_KEY_ELEMENTS.include?(key.to_sym)

  super
end

#export(options = {}) ⇒ Object



41
42
43
44
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 41

def export(options = {})
  include_private = PRIVATE_EXPORT_OPTIONS.any? { |key| (options || {})[key] }
  parameters.clone.tap { |exported| exported.delete(:priv) unless include_private }
end

#jwaObject



52
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 52

def jwa = PQCrypto::JWT.algorithm_for(self[:alg]) || super

#key_digestObject



51
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 51

def key_digest = PQCrypto::JWT::JWK.thumbprint(string_export)

#membersObject



46
47
48
49
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 46

def members
  keys = private? ? %i[alg kty pub priv] : %i[alg kty pub]
  keys.each_with_object({}) { |key, out| out[key] = self[key] }
end

#private?Boolean

Returns:

  • (Boolean)


26
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 26

def private? = parameters.key?(:priv) && !parameters[:priv].nil?

#public_keyObject



29
30
31
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 29

def public_key
  @public_key ||= @checked_public_key || PQCrypto::JWT::JWK.public_key_from_jwk(string_export)
end

#secret_keyObject

Raises:

  • (JWT::JWKError)


35
36
37
38
39
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 35

def secret_key
  raise JWT::JWKError, "AKP JWK does not contain private material" unless private?

  @secret_key ||= PQCrypto::JWT::JWK.secret_key_from_jwk(string_export(include_private: true))
end

#signing_keyObject



33
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 33

def signing_key = private? ? secret_key : public_key

#verify_keyObject



27
# File 'lib/pq_crypto/jwt/jwk/akp.rb', line 27

def verify_key = public_key