Class: Acme::Client

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

Constant Summary collapse

VERSION =
'0.1.0'
DEFAULT_ENDPOINT =
'http://127.0.0.1:4000'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



4
5
6
7
8
# File 'lib/acme/client.rb', line 4

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

Instance Attribute Details

#noncesObject (readonly)

Returns the value of attribute nonces.



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

def nonces
  @nonces
end

#operation_endpointsObject (readonly)

Returns the value of attribute operation_endpoints.



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

def operation_endpoints
  @operation_endpoints
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



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

def private_key
  @private_key
end

Instance Method Details

#authorize(domain:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acme/client.rb', line 21

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

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

#connectionObject



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

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

#load_directory!Object



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

def load_directory!
  @operation_endpoints = if @directory_uri
    response = connection.get(@directory_uri)
    body = response.body
    {
      'new-reg' => body.fetch('new-reg'),
      'recover-reg' => body.fetch('recover-reg'),
      'new-authz' => body.fetch('new-authz'),
      'new-cert' => body.fetch('new-cert'),
      'revoke-cert' => body.fetch('revoke-cert'),
    }
  else
    DIRECTORY_DEFAULT
  end
end

#new_certificate(csr) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/acme/client.rb', line 34

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

  response = connection.post(@operation_endpoints.fetch('new-cert'), payload)
  OpenSSL::X509::Certificate.new(response.body)
end

#register(contact:) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/acme/client.rb', line 12

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

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