base.rb 1014 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Acme::Client::Resources::Challenges::Base
  attr_reader :client, :status, :uri, :token, :error

  def initialize(client, attributes)
    @client = client
    assign_attributes(attributes)
  end

  def verify_status
    response = @client.connection.get(@uri)

    assign_attributes(response.body)
    @error = response.body['error']
    status
  end

  def request_verification
    response = @client.connection.post(@uri, resource: 'challenge', type: challenge_type, keyAuthorization: authorization_key)
    response.success?
  end

  def to_h
    { 'token' => token, 'uri' => uri, 'type' => challenge_type }
  end

  private

  def challenge_type
    self.class::CHALLENGE_TYPE
  end

  def authorization_key
    "#{token}.#{crypto.thumbprint}"
  end

  def assign_attributes(attributes)
    @status = attributes.fetch('status', 'pending')
    @uri = attributes.fetch('uri')
    @token = attributes.fetch('token')
  end

  def crypto
    @crypto ||= Acme::Client::Crypto.new(@client.private_key)
  end
end