Sha256: 2ade24a898ff20aa33e280259345d35f1136aa314c56b4138e9061d591f29321

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

class Acme::Client
  DEFAULT_ENDPOINT = 'http://127.0.0.1:4000'
  DIRECTORY_DEFAULT = {
    'new-authz' => '/acme/new-authz',
    'new-cert' => '/acme/new-cert',
    'new-reg' => '/acme/new-reg',
    'revoke-cert' => '/acme/revoke-cert'
  }

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

  attr_reader :private_key, :nonces, :operation_endpoints

  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

  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

  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

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

  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acme-client-0.1.1 lib/acme/client.rb