Class: Acme::Client::JWK::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/acme/client/jwk/base.rb

Direct Known Subclasses

ECDSA, HMAC, RSA

Constant Summary collapse

THUMBPRINT_DIGEST =
OpenSSL::Digest::SHA256

Instance Method Summary collapse

Constructor Details

#initializeBase

Initialize a new JWK.

Returns nothing.

Raises:

  • (NotImplementedError)


7
8
9
# File 'lib/acme/client/jwk/base.rb', line 7

def initialize
  raise NotImplementedError
end

Instance Method Details

#jwa_algObject

The name of the algorithm as needed for the ‘alg` member of a JWS object.

Returns a String.

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/acme/client/jwk/base.rb', line 71

def jwa_alg
  raise NotImplementedError
end

#jws(header: {}, payload:) ⇒ Object

Generate a JWS JSON web signature.

header - A Hash of extra header fields to include. payload - A Hash of payload data.

Returns a JSON String.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acme/client/jwk/base.rb', line 17

def jws(header: {}, payload:)
  header = jws_header(header)
  encoded_header = Acme::Client::Util.urlsafe_base64(header.to_json)
  encoded_payload = Acme::Client::Util.urlsafe_base64(payload.nil? ? '' : payload.to_json)

  signature_data = "#{encoded_header}.#{encoded_payload}"
  signature = sign(signature_data)
  encoded_signature = Acme::Client::Util.urlsafe_base64(signature)

  {
    protected: encoded_header,
    payload: encoded_payload,
    signature: encoded_signature
  }.to_json
end

#jws_header(header) ⇒ Object

Header fields for a JSON web signature.

typ: - Value for the ‘typ` field. Default ’JWT’.

Returns a Hash.



59
60
61
62
63
64
65
66
# File 'lib/acme/client/jwk/base.rb', line 59

def jws_header(header)
  jws = {
    typ: 'JWT',
    alg: jwa_alg
  }.merge(header)
  jws[:jwk] = to_h if header[:kid].nil?
  jws
end

#sign(message) ⇒ Object

Sign a message with the private key.

message - A String message to sign.

Returns a String signature. rubocop:disable Lint/UnusedMethodArgument

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/acme/client/jwk/base.rb', line 81

def sign(message)
  raise NotImplementedError
end

#thumbprintObject

JWK thumbprint as used for key authorization.

Returns a String.



50
51
52
# File 'lib/acme/client/jwk/base.rb', line 50

def thumbprint
  Acme::Client::Util.urlsafe_base64(THUMBPRINT_DIGEST.digest(to_json))
end

#to_hObject

Get this JWK as a Hash for JSON serialization.

Returns a Hash.

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/acme/client/jwk/base.rb', line 43

def to_h
  raise NotImplementedError
end

#to_jsonObject

Serialize this JWK as JSON.

Returns a JSON string.



36
37
38
# File 'lib/acme/client/jwk/base.rb', line 36

def to_json
  to_h.to_json
end