lib/acme/client/resources/authorization.rb in acme-client-1.0.0 vs lib/acme/client/resources/authorization.rb in acme-client-2.0.0

- old
+ new

@@ -1,44 +1,74 @@ +# frozen_string_literal: true + class Acme::Client::Resources::Authorization - HTTP01 = Acme::Client::Resources::Challenges::HTTP01 - DNS01 = Acme::Client::Resources::Challenges::DNS01 - TLSSNI01 = Acme::Client::Resources::Challenges::TLSSNI01 + attr_reader :url, :identifier, :domain, :expires, :status, :wildcard - attr_reader :client, :uri, :domain, :status, :expires, :http01, :dns01, :tls_sni01 - - def initialize(client, uri, response) + def initialize(client, **arguments) @client = client - @uri = uri - assign_attributes(response.body) + assign_attributes(arguments) end - def verify_status - response = @client.connection.get(@uri) + def deactivate + assign_attributes **@client.deactivate_authorization(url: url).to_h + true + end - assign_attributes(response.body) - status + def reload + assign_attributes **@client.authorization(url: url).to_h + true end - private + def challenges + @challenges.map do |challenge| + initialize_challenge(challenge) + end + end - def assign_attributes(body) - @expires = Time.iso8601(body['expires']) if body.key? 'expires' - @domain = body['identifier']['value'] - @status = body['status'] - assign_challenges(body['challenges']) + def http01 + @http01 ||= challenges.find { |challenge| + challenge.is_a?(Acme::Client::Resources::Challenges::HTTP01) + } end + alias_method :http, :http01 - def assign_challenges(challenges) - challenges.each do |attributes| - challenge = case attributes.fetch('type') - when 'http-01' - @http01 ||= HTTP01.new(self) - when 'dns-01' - @dns01 ||= DNS01.new(self) - when 'tls-sni-01' - @tls_sni01 ||= TLSSNI01.new(self) - end + def dns01 + @dns01 ||= challenges.find { |challenge| + challenge.is_a?(Acme::Client::Resources::Challenges::DNS01) + } + end + alias_method :dns, :dns01 - challenge.assign_attributes(attributes) if challenge - end + def to_h + { + url: url, + identifier: identifier, + status: status, + expires: expires, + challenges: @challenges, + wildcard: wildcard + } + end + + private + + def initialize_challenge(attributes) + arguments = { + type: attributes.fetch('type'), + status: attributes.fetch('status'), + url: attributes.fetch('url'), + token: attributes.fetch('token'), + error: attributes['error'] + } + Acme::Client::Resources::Challenges.new(@client, **arguments) + end + + def assign_attributes(url:, status:, expires:, challenges:, identifier:, wildcard: false) + @url = url + @identifier = identifier + @domain = identifier.fetch('value') + @status = status + @expires = expires + @challenges = challenges + @wildcard = wildcard end end