class Digicert::CertificateDownloader

Attributes

format[R]
platform[R]

Public Class Methods

fetch(certificate_id, attributes = {}) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 17
def self.fetch(certificate_id, attributes = {})
  new(attributes.merge(resource_id: certificate_id)).fetch
end
fetch_by_format(certificate_id, format:) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 21
def self.fetch_by_format(certificate_id, format:)
  fetch(certificate_id, format: format)
end
fetch_by_platform(certificate_id, platform:) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 25
def self.fetch_by_platform(certificate_id, platform:)
  fetch(certificate_id, platform: platform)
end
fetch_content(certificate_id) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 34
def self.fetch_content(certificate_id)
  new(resource_id: certificate_id, format: "pem_all").fetch_content
end
fetch_to_path(certificate_id, path:, ext: "zip", **attributes) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 29
def self.fetch_to_path(certificate_id, path:, ext: "zip", **attributes)
  new(attributes.merge(resource_id: certificate_id)).
    fetch_to_path(path: path, extension: ext)
end

Public Instance Methods

fetch() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 5
def fetch
  request_klass.new(:get, certificate_download_path).run
end
fetch_content() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 13
def fetch_content
  extract_certificate_content
end
fetch_to_path(path:, extension: "zip") click to toggle source
# File lib/digicert/certificate_downloader.rb, line 9
def fetch_to_path(path:, extension: "zip")
  download_to_path(path: path, extension: extension)
end

Private Instance Methods

certificate_download_path() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 51
def certificate_download_path
  download_path_by_format ||
    download_path_by_platform ||
    download_path_by_order_specified_platform
end
convert_response_to_hash(content) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 85
def convert_response_to_hash(content)
  contents = split_pem_certificates(content)

  Hash.new.tap do |content_hash|
    content_hash[:text] = content
    content_hash[:certificate] = contents.first
    content_hash[:root_certificate] = contents.last
    content_hash[:intermediate_certificate] = extract_intermediate(contents)
  end
end
download_path_by_format() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 69
def download_path_by_format
  if format
    [resource_path, "format", format].join("/")
  end
end
download_path_by_order_specified_platform() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 81
def download_path_by_order_specified_platform
  [resource_path, "platform"].join("/")
end
download_path_by_platform() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 75
def download_path_by_platform
  if platform
    [resource_path, "platform", platform].join("/")
  end
end
download_to_path(path:, extension:) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 57
def download_to_path(path:, extension:)
  response = fetch

  if response.code.to_i == 200
    write_to_path(response.body, path: path, extension: extension)
  end
end
extract_certificate_content() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 65
def extract_certificate_content
  convert_response_to_hash(fetch.body)
end
extract_intermediate(certificates) click to toggle source

Extract intermediate certificate

Normally the second certificate item is intermediate certificate but in some rare case digicert responds with four certificate, so this method will also check for the length of the responses and it will build an array if necessary.

# File lib/digicert/certificate_downloader.rb, line 118
def extract_intermediate(certificates)
  certificate = certificates[1]

  if certificates.length > 3
    certificate = [certificate, certificates[2]]
  end

  certificate
end
extract_local_attribute_ids() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 42
def extract_local_attribute_ids
  @format = attributes.delete(:format)
  @platform = attributes.delete(:platform)
end
resource_path() click to toggle source
# File lib/digicert/certificate_downloader.rb, line 47
def resource_path
  ["certificate", resource_id, "download"].join("/")
end
split_pem_certificates(content) click to toggle source

Spliting certificate content

Digicert returns all of the certificates including `root` one when we specify `pem_all` as format. The format it returns the content has a pattern, which is it will have all of the three certificates. The sequance for the certificates are `certificate`, `intermediate` and `root` and each of them are separated by `END CERTIFICATE—–`

This method will split those using the specified identifier and it will return an array in the same sequance.

# File lib/digicert/certificate_downloader.rb, line 107
def split_pem_certificates(content)
  content.split(/(?<=END CERTIFICATE-----)\r?\n/)
end
write_to_path(content, path:, extension:) click to toggle source
# File lib/digicert/certificate_downloader.rb, line 128
def write_to_path(content, path:, extension:)
  filename = ["certificate", extension].join(".")
  file_with_path = [path, filename].join("/")

  File.open(file_with_path, "w") do |file|
    file.write(content)
  end
end