Class: LeSsl::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/le_ssl/manager.rb

Constant Summary collapse

PRODUCTION_ENDPOINT =
'https://acme-v01.api.letsencrypt.org/'
DEVELOPMENT_ENDPOINT =
'https://acme-staging.api.letsencrypt.org/'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/le_ssl/manager.rb', line 6

def initialize(options={})
	email = options[:email] || ENV['CERT_ACCOUNT_EMAIL'].presence

	raise LeSsl::NoContactEmailError if email.nil?
	raise LeSsl::TermsNotAcceptedError unless options[:agree_terms] == true

	self.private_key = options[:private_key].presence

	private_key			# Check private key

	register(email) unless options[:skip_register] == true
end

Instance Method Details

#authorize_for_domain(domain) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/le_ssl/manager.rb', line 19

def authorize_for_domain(domain)
	authorization = client.authorize(domain: domain)
	challenge = authorization.http01

	file_name = Rails.root.join('public', challenge.filename)
	dir = File.dirname(Rails.root.join('public', challenge.filename))

	FileUtils.mkdir_p(dir)

	File.write(file_name, challenge.file_content)

	challenge.request_verification

	sleep(1)
	
	File.delete(file_name) if challenge.verify_status == 'invalid'
	
	return challenge.verify_status
end

#register(email) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/le_ssl/manager.rb', line 55

def register(email)
	client.register(contact: "mailto:#{email}").agree_terms
	return true
rescue Acme::Client::Error::Malformed => e
	return false if e.message == "Registration key is already in use"
	raise e
end

#request_certificate(*domains) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/le_ssl/manager.rb', line 39

def request_certificate(*domains)
	csr = Acme::Client::CertificateRequest.new(names: domains)
	certificate = client.new_certificate(csr)

	FileUtils.mkdir_p(Rails.root.join('config', 'ssl'))

	File.write(Rails.root.join('config', 'ssl', 'privkey.pem'), certificate.request.private_key.to_pem)
	File.write(Rails.root.join('config', 'ssl', 'cert.pem'), certificate.to_pem)
	File.write(Rails.root.join('config', 'ssl', 'chain.pem'), certificate.chain_to_pem)
	File.write(Rails.root.join('config', 'ssl', 'fullchain.pem'), certificate.fullchain_to_pem)

	return certificate
rescue Acme::Client::Error::Unauthorized => e
	raise LeSsl::UnauthorizedError, e.message
end