Sha256: c6eb9f58bb4eaff713c796b111ed36d1c9404d0b8231ad7171ad5d18d274fb9d

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require 'fileutils'

module LetsencryptWebfaction
  class DomainValidator
    def initialize(domains, client, public_dirs)
      @domains = domains
      @client = client
      @public_dirs = public_dirs.map { |dir| File.expand_path(dir) }
    end

    def validate!
      write_files!

      challenges.each(&:request_verification)

      10.times do
        return true if all_challenges_valid?
        sleep(1)
      end

      print_errors
      false
    end

    private

    def authorizations
      @domains.map { |domain| @client.authorize(domain: domain) }
    end

    def challenges
      @challenges ||= authorizations.map(&:http01)
    end

    def all_challenges_valid?
      challenges.reject { |challenge| challenge.verify_status == 'valid' }.empty?
    end

    def write_files!
      challenges.each do |challenge|
        @public_dirs.each do |public_dir|
          # Save the file. We'll create a public directory to serve it from, and we'll creating the challenge directory.
          FileUtils.mkdir_p(File.join(public_dir, File.dirname(challenge.filename)))

          # Then writing the file
          File.write(File.join(public_dir, challenge.filename), challenge.file_content)
        end
      end
    end

    def print_errors
      validations = authorizations.map(&:domain).zip(challenges)
      $stderr.puts 'Failed to verify statuses.'
      validations.each { |tuple| Validation.new(*tuple).print_error }
    end

    class Validation
      def initialize(domain, challenge)
        @domain = domain
        @challenge = challenge
      end

      def print_error
        if @challenge.verify_status == 'valid'
          $stderr.puts "#{@domain}: Success"
        else
          $stderr.puts "#{@domain}: #{@challenge.error['detail']}"
          $stderr.puts "Make sure that you can access #{url}"
        end
      end

      def url
        "http://#{@domain}/#{@challenge.filename}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
letsencrypt_webfaction-2.2.2 lib/letsencrypt_webfaction/domain_validator.rb
letsencrypt_webfaction-2.2.1 lib/letsencrypt_webfaction/domain_validator.rb
letsencrypt_webfaction-2.2.0 lib/letsencrypt_webfaction/domain_validator.rb
letsencrypt_webfaction-2.1.0 lib/letsencrypt_webfaction/domain_validator.rb