-- Copyright © by Jeff Foley 2017-2023. All rights reserved.
-- Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
-- SPDX-License-Identifier: Apache-2.0

local json = require("json")

name = "CertCentral"
type = "cert"

function start()
    set_rate_limit(1)
end

function check()
    local c
    local cfg = datasrc_config()
    if (cfg ~= nil) then
        c = cfg.credentials
    end

    if (c ~= nil and c.username ~= nil and
        c.username ~= "" and c.key ~= nil and c.key ~= "") then
        return true
    end
    return false
end

function vertical(ctx, domain)
    local c
    local cfg = datasrc_config()
    if (cfg ~= nil) then
        c = cfg.credentials
    end

    if (c == nil or c.username == nil or
        c.username == "" or c.key == nil or c.key == "") then
        return
    end

    local body, err = json.encode({
        ['accountId']=c.username,
        ['domains']={domain},
    })
    if (err ~= nil and err ~= "") then
        return
    end

    local resp, err = request(ctx, {
        ['url']="https://daas.digicert.com/apicontroller/v1/scan/getSubdomains",
        ['method']="POST",
        ['header']={
            ['Content-Type']="application/json",
            ['X-DC-DEVKEY']=c.key,
        },
        ['body']=body,
    })
    if (err ~= nil and err ~= "") then
        log(ctx, "vertical request to service failed: " .. err)
        return
    elseif (resp.status_code < 200 or resp.status_code >= 400) then
        log(ctx, "vertical request to service returned with status code: " .. resp.status)
        return
    end

    local j = json.decode(resp.body)
    if (j == nil) then
        log(ctx, "failed to decode the JSON response")
        return
    elseif (j.error ~= nil) then
        log (ctx, "error returned by the vertical request: " .. j.error)
        return
    end

    for _, sub in pairs(j.data[1].subdomains) do
        new_name(ctx, sub)
    end
end
