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: 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.4.0'.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.



31
32
33
34
35
# File 'lib/acme/client.rb', line 31

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.



37
38
39
# File 'lib/acme/client.rb', line 37

def nonces
  @nonces
end

#operation_endpointsObject (readonly)

Returns the value of attribute operation_endpoints.



37
38
39
# File 'lib/acme/client.rb', line 37

def operation_endpoints
  @operation_endpoints
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



37
38
39
# File 'lib/acme/client.rb', line 37

def private_key
  @private_key
end

Class Method Details

.revoke_certificate(certificate, *arguments) ⇒ Object



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

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

Instance Method Details

#authorize(domain:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/acme/client.rb', line 48

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(arguments) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/acme/client.rb', line 90

def challenge_from_hash(arguments)
  attributes = arguments.to_h
  %w(type uri token).each do |key|
    raise ArgumentError, "missing key: #{key}" unless attributes.key?(key)
  end

  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)
  else
    raise 'Unsupported resource type'
  end
end

#connectionObject



83
84
85
86
87
88
# File 'lib/acme/client.rb', line 83

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



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

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



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

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



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

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