Class: Acme::Client

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

Defined Under Namespace

Modules: Resources Classes: Certificate, CertificateRequest, Crypto, Error, FaradayMiddleware, SelfSignCertificate

Constant Summary collapse

DEFAULT_ENDPOINT =
'http://127.0.0.1:4000'.freeze
DIRECTORY_DEFAULT =
{
  'new-authz' => '/acme/new-authz',
  'new-cert' => '/acme/new-cert',
  'new-reg' => '/acme/new-reg',
  'revoke-cert' => '/acme/revoke-cert'
}.freeze
VERSION =
'0.3.3'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, endpoint: DEFAULT_ENDPOINT, directory_uri: nil, connection_options: {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/acme/client.rb', line 10

def initialize(private_key:, endpoint: DEFAULT_ENDPOINT, directory_uri: nil, connection_options: {})
  @endpoint, @private_key, @directory_uri, @connection_options = endpoint, private_key, directory_uri, connection_options
  @nonces ||= []
  load_directory!
end

Instance Attribute Details

#noncesObject (readonly)

Returns the value of attribute nonces.



16
17
18
# File 'lib/acme/client.rb', line 16

def nonces
  @nonces
end

#operation_endpointsObject (readonly)

Returns the value of attribute operation_endpoints.



16
17
18
# File 'lib/acme/client.rb', line 16

def operation_endpoints
  @operation_endpoints
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



16
17
18
# File 'lib/acme/client.rb', line 16

def private_key
  @private_key
end

Class Method Details

.revoke_certificate(certificate, *arguments) ⇒ Object



57
58
59
60
# File 'lib/acme/client.rb', line 57

def self.revoke_certificate(certificate, *arguments)
  client = new(*arguments)
  client.revoke_certificate(certificate)
end

Instance Method Details

#authorize(domain:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/acme/client.rb', line 27

def authorize(domain:)
  payload = {
    resource: 'new-authz',
    identifier: {
      type: 'dns',
      value: domain
    }
  }

  response = connection.post(@operation_endpoints.fetch('new-authz'), payload)
  ::Acme::Client::Resources::Authorization.new(self, response)
end

#challenge_from_hash(attributes) ⇒ Object



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

def challenge_from_hash(attributes)
  case attributes.fetch('type')
  when 'http-01'
    Acme::Client::Resources::Challenges::HTTP01.new(self, attributes)
  when 'dns-01'
    Acme::Client::Resources::Challenges::DNS01.new(self, attributes)
  when 'tls-sni-01'
    Acme::Client::Resources::Challenges::TLSSNI01.new(self, attributes)
  end
end

#connectionObject



62
63
64
65
66
67
# File 'lib/acme/client.rb', line 62

def connection
  @connection ||= Faraday.new(@endpoint, **@connection_options) do |configuration|
    configuration.use Acme::Client::FaradayMiddleware, client: self
    configuration.adapter Faraday.default_adapter
  end
end

#new_certificate(csr) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/acme/client.rb', line 40

def new_certificate(csr)
  payload = {
    resource: 'new-cert',
    csr: Base64.urlsafe_encode64(csr.to_der)
  }

  response = connection.post(@operation_endpoints.fetch('new-cert'), payload)
  ::Acme::Client::Certificate.new(OpenSSL::X509::Certificate.new(response.body), response.headers['location'], fetch_chain(response), csr)
end

#register(contact:) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/acme/client.rb', line 18

def register(contact:)
  payload = {
    resource: 'new-reg', contact: Array(contact)
  }

  response = connection.post(@operation_endpoints.fetch('new-reg'), payload)
  ::Acme::Client::Resources::Registration.new(self, response)
end

#revoke_certificate(certificate) ⇒ Object



50
51
52
53
54
55
# File 'lib/acme/client.rb', line 50

def revoke_certificate(certificate)
  payload = { resource: 'revoke-cert', certificate: Base64.urlsafe_encode64(certificate.to_der) }
  endpoint = @operation_endpoints.fetch('revoke-cert')
  response = connection.post(endpoint, payload)
  response.success?
end