Module: Rack::Acme

Defined in:
lib/rack/acme/version.rb,
lib/rack/acme/endpoint.rb,
lib/rack/acme/file_cache.rb,
lib/rack/acme.rb

Defined Under Namespace

Classes: Endpoint, FileCache

Constant Summary collapse

VERSION =
"0.1.0".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject



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

def cache
  path = "./tokens"
  @cache ||= FileCache.new(path)
end

.certificate_handlerObject



59
60
61
# File 'lib/rack/acme.rb', line 59

def certificate_handler
  @certificate_handler || proc {}
end

.connection_optionsObject



103
104
105
# File 'lib/rack/acme.rb', line 103

def connection_options
  @connection_options ||= { request: { open_timeout: 5, timeout: 5 } }
end

.contactObject

Returns the value of attribute contact.



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

def contact
  @contact
end

.endpointObject



86
87
88
# File 'lib/rack/acme.rb', line 86

def endpoint
  @endpoint ||= default_endpoint
end

.restart_handlerObject



64
65
66
# File 'lib/rack/acme.rb', line 64

def restart_handler
  @restart_handler || proc {}
end

Class Method Details

.build_clientObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rack/acme.rb', line 73

def build_client
  client = ::Acme::Client.new(
    private_key: private_key,
    endpoint: endpoint,
    connection_options: connection_options
  )

  registration = client.register(contact: "mailto:#{contact}")
  registration.agree_terms

  client
end

.build_private_keyObject



99
100
101
# File 'lib/rack/acme.rb', line 99

def build_private_key
  OpenSSL::PKey::RSA.new(4096)
end

.clientObject



69
70
71
# File 'lib/rack/acme.rb', line 69

def client
  @client ||= build_client
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Rack::Acme)

    the object that the method was called on



12
13
14
# File 'lib/rack/acme.rb', line 12

def configure
  yield self if block_given?
end

.default_endpointObject



91
92
93
# File 'lib/rack/acme.rb', line 91

def default_endpoint
  "https://acme-staging.api.letsencrypt.org/"
end

.issue(domain) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack/acme.rb', line 22

def issue(domain)
  raise ArgumentError, "contact is nil" if contact.nil?

  authorization = client.authorize(domain: domain)

  case authorization.status
  when "pending"
    challenge = authorization.http01

    token = challenge.token
    challenge_content = challenge.file_content

    cache[token] = challenge_content

    challenge.request_verification

    sleep 1

    csr = ::Acme::Client::CertificateRequest.new(names: [domain])
    certificate = client.new_certificate(csr)

    certificate_handler.call(certificate) if certificate_handler.respond_to?(:call)
    restart_handler.call if restart_handler.respond_to?(:call)

    certificate

  when "valid"
    csr = Acme::Client::CertificateRequest.new(names: [domain])
    certificate = client.new_certificate(csr)

    certificate_handler.call(certificate) if certificate_handler.respond_to?(:call)
    restart_handler.call if restart_handler.respond_to?(:call)

    certificate
  end
end

.private_keyObject



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

def private_key
  @private_key ||= build_private_key
end