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: Certificate, CertificateRequest, 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.6.2'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

def initialize(jwk: nil, private_key: nil, endpoint: DEFAULT_ENDPOINT, directory_uri: nil, 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

  @endpoint, @directory_uri, @connection_options = endpoint, directory_uri, connection_options
  @nonces ||= []
  load_directory!
end

Instance Attribute Details

#directory_uriObject (readonly)

Returns the value of attribute directory_uri.



49
50
51
# File 'lib/acme/client.rb', line 49

def directory_uri
  @directory_uri
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



49
50
51
# File 'lib/acme/client.rb', line 49

def endpoint
  @endpoint
end

#jwkObject (readonly)

Returns the value of attribute jwk.



49
50
51
# File 'lib/acme/client.rb', line 49

def jwk
  @jwk
end

#noncesObject (readonly)

Returns the value of attribute nonces.



49
50
51
# File 'lib/acme/client.rb', line 49

def nonces
  @nonces
end

#operation_endpointsObject (readonly)

Returns the value of attribute operation_endpoints.



49
50
51
# File 'lib/acme/client.rb', line 49

def operation_endpoints
  @operation_endpoints
end

Class Method Details

.revoke_certificate(certificate, *arguments) ⇒ Object



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

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

Instance Method Details

#authorize(domain:) ⇒ Object



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

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.headers['Location'], response)
end

#connectionObject



100
101
102
103
104
105
# File 'lib/acme/client.rb', line 100

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

#fetch_authorization(uri) ⇒ Object



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

def fetch_authorization(uri)
  response = connection.get(uri)
  ::Acme::Client::Resources::Authorization.new(self, uri, response)
end

#new_certificate(csr) ⇒ Object



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

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



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

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



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

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