Class: Acme::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/acme/client.rb,
lib/acme/client.rb,
lib/acme/client/version.rb

Defined Under Namespace

Modules: JWK, Resources, Util Classes: CertificateRequest, Error, FaradayMiddleware, SelfSignCertificate

Constant Summary collapse

DEFAULT_DIRECTORY =
'http://127.0.0.1:4000/directory'.freeze
USER_AGENT =
"Acme::Client v#{Acme::Client::VERSION} (#{repo_url})".freeze
CONTENT_TYPES =
{
  pem: 'application/pem-certificate-chain'
}
VERSION =
'2.0.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwk: nil, kid: nil, private_key: nil, directory: DEFAULT_DIRECTORY, connection_options: {}) ⇒ Client

Returns a new instance of Client.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/acme/client.rb', line 31

def initialize(jwk: nil, kid: nil, private_key: nil, directory: DEFAULT_DIRECTORY, connection_options: {})
  if jwk.nil? && private_key.nil?
    raise ArgumentError, 'must specify jwk or private_key'
  end

  @jwk = if jwk
    jwk
  else
    Acme::Client::JWK.from_private_key(private_key)
  end

  @kid, @connection_options = kid, connection_options
  @directory = Acme::Client::Resources::Directory.new(URI(directory), @connection_options)
  @nonces ||= []
end

Instance Attribute Details

#jwkObject (readonly)

Returns the value of attribute jwk.



47
48
49
# File 'lib/acme/client.rb', line 47

def jwk
  @jwk
end

#noncesObject (readonly)

Returns the value of attribute nonces.



47
48
49
# File 'lib/acme/client.rb', line 47

def nonces
  @nonces
end

Instance Method Details

#accountObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/acme/client.rb', line 85

def 
  @kid ||= begin
    response = post(endpoint_for(:new_account), payload: { onlyReturnExisting: true }, mode: :jwk)
    response.headers.fetch(:location)
  end

  response = post(@kid)
  arguments = (response)
  Acme::Client::Resources::Account.new(self, url: @kid, **arguments)
end

#account_deactivateObject



79
80
81
82
83
# File 'lib/acme/client.rb', line 79

def 
  response = post(kid, payload: { status: 'deactivated' })
  arguments = (response)
  Acme::Client::Resources::Account.new(self, url: kid, **arguments)
end

#account_update(contact: nil, terms_of_service_agreed: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/acme/client.rb', line 69

def (contact: nil, terms_of_service_agreed: nil)
  payload = {}
  payload[:contact] = Array(contact) if contact
  payload[:termsOfServiceAgreed] = terms_of_service_agreed if terms_of_service_agreed

  response = post(kid, payload: payload)
  arguments = (response)
  Acme::Client::Resources::Account.new(self, url: kid, **arguments)
end

#authorization(url:) ⇒ Object



139
140
141
142
143
# File 'lib/acme/client.rb', line 139

def authorization(url:)
  response = get(url)
  arguments = attributes_from_authorization_response(response)
  Acme::Client::Resources::Authorization.new(self, url: url, **arguments)
end

#caa_identitiesObject



196
197
198
# File 'lib/acme/client.rb', line 196

def caa_identities
  @directory.caa_identities
end

#certificate(url:) ⇒ Object



134
135
136
137
# File 'lib/acme/client.rb', line 134

def certificate(url:)
  response = download(url, format: :pem)
  response.body
end

#challenge(url:) ⇒ Object



151
152
153
154
155
# File 'lib/acme/client.rb', line 151

def challenge(url:)
  response = get(url)
  arguments = attributes_from_challenge_response(response)
  Acme::Client::Resources::Challenges.new(self, **arguments)
end

#deactivate_authorization(url:) ⇒ Object



145
146
147
148
149
# File 'lib/acme/client.rb', line 145

def deactivate_authorization(url:)
  response = post(url, payload: { status: 'deactivated' })
  arguments = attributes_from_authorization_response(response)
  Acme::Client::Resources::Authorization.new(self, url: url, **arguments)
end

#external_account_requiredObject



200
201
202
# File 'lib/acme/client.rb', line 200

def 
  @directory.
end

#finalize(url:, csr:) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/acme/client.rb', line 123

def finalize(url:, csr:)
  unless csr.respond_to?(:to_der)
    raise ArgumentError, 'csr must respond to `#to_der`'
  end

  base64_der_csr = Acme::Client::Util.urlsafe_base64(csr.to_der)
  response = post(url, payload: { csr: base64_der_csr })
  arguments = attributes_from_order_response(response)
  Acme::Client::Resources::Order.new(self, **arguments)
end

#get_nonceObject



178
179
180
181
182
# File 'lib/acme/client.rb', line 178

def get_nonce
  response = Faraday.head(endpoint_for(:new_nonce), nil, 'User-Agent' => USER_AGENT)
  nonces << response.headers['replay-nonce']
  true
end

#kidObject



96
97
98
# File 'lib/acme/client.rb', line 96

def kid
  @kid ||= .kid
end

#metaObject



184
185
186
# File 'lib/acme/client.rb', line 184

def meta
  @directory.meta
end

#new_account(contact:, terms_of_service_agreed: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/acme/client.rb', line 49

def (contact:, terms_of_service_agreed: nil)
  payload = {
    contact: Array(contact)
  }

  if terms_of_service_agreed
    payload[:termsOfServiceAgreed] = terms_of_service_agreed
  end

  response = post(endpoint_for(:new_account), payload: payload, mode: :jws)
  @kid = response.headers.fetch(:location)

  if response.body.nil? || response.body.empty?
    
  else
    arguments = (response)
    Acme::Client::Resources::Account.new(self, url: @kid, **arguments)
  end
end

#new_order(identifiers:, not_before: nil, not_after: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/acme/client.rb', line 100

def new_order(identifiers:, not_before: nil, not_after: nil)
  payload = {}
  payload['identifiers'] = if identifiers.is_a?(Hash)
    identifiers
  else
    Array(identifiers).map do |identifier|
      { type: 'dns', value: identifier }
    end
  end
  payload['notBefore'] = not_before if not_before
  payload['notAfter'] = not_after if not_after

  response = post(endpoint_for(:new_order), payload: payload)
  arguments = attributes_from_order_response(response)
  Acme::Client::Resources::Order.new(self, **arguments)
end

#order(url:) ⇒ Object



117
118
119
120
121
# File 'lib/acme/client.rb', line 117

def order(url:)
  response = get(url)
  arguments = attributes_from_order_response(response)
  Acme::Client::Resources::Order.new(self, **arguments.merge(url: url))
end

#request_challenge_validation(url:, key_authorization:) ⇒ Object



157
158
159
160
161
# File 'lib/acme/client.rb', line 157

def request_challenge_validation(url:, key_authorization:)
  response = post(url, payload: { keyAuthorization: key_authorization })
  arguments = attributes_from_challenge_response(response)
  Acme::Client::Resources::Challenges.new(self, **arguments)
end

#revoke(certificate:, reason: nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/acme/client.rb', line 163

def revoke(certificate:, reason: nil)
  der_certificate = if certificate.respond_to?(:to_der)
    certificate.to_der
  else
    OpenSSL::X509::Certificate.new(certificate).to_der
  end

  base64_der_certificate = Acme::Client::Util.urlsafe_base64(der_certificate)
  payload = { certificate: base64_der_certificate }
  payload[:reason] = reason unless reason.nil?

  response = post(endpoint_for(:revoke_certificate), payload: payload)
  response.success?
end

#terms_of_serviceObject



188
189
190
# File 'lib/acme/client.rb', line 188

def terms_of_service
  @directory.terms_of_service
end

#websiteObject



192
193
194
# File 'lib/acme/client.rb', line 192

def website
  @directory.website
end