acme-dns-01-godaddy.js-ARCH.../lib/index.js

162 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-07-28 11:28:13 +00:00
'use strict';
var request; // = require('@root/request');
// request = require('util').promisify(request);
var OTE_ENVIRONMENT = 'https://api.ote-godaddy.com/v1/';
var PRODUCTION_ENVIRONMENT = 'https://api.godaddy.com/v1/';
var defaults = {
baseUrl: PRODUCTION_ENVIRONMENT
};
module.exports.create = function(config) {
2019-07-31 01:37:01 +00:00
var baseUrl = config.baseUrl || defaults.baseUrl;
2019-07-28 11:28:13 +00:00
var apiKey = config.key || config.apiKey;
var apiSecret = config.secret || config.apiSecret;
2019-07-31 01:37:01 +00:00
var auth = 'sso-key ' + apiKey + ':' + apiSecret;
2019-07-28 11:28:13 +00:00
function api(method, path, form) {
return request({
method: method,
url: baseUrl + path,
headers: {
2019-07-31 01:37:01 +00:00
Authorization: auth
2019-07-28 11:28:13 +00:00
},
json: form || true
});
}
return {
init: function(deps) {
request = deps.request;
return null;
},
zones: function(data) {
return api('GET', 'domains?statuses=ACTIVE').then(function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
return resp.body.map(function(x) {
return x.domain;
});
});
},
set: function(data) {
var ch = data.challenge;
var txt = ch.dnsAuthorization;
// If the domain to be verified is
// optional params commented
var records = [
{
2019-07-31 01:37:01 +00:00
data: txt,
name: ch.dnsPrefix,
2019-07-28 11:28:13 +00:00
// "port": 0,
// "priority": 0,
// "protocol": "string",
// "service": "string",
2019-07-31 01:37:01 +00:00
ttl: 600,
type: 'TXT'
2019-07-28 11:28:13 +00:00
// "weight": 0
}
];
2019-07-31 01:37:01 +00:00
return api('PATCH', 'domains/' + ch.dnsZone + '/records', records).then(
function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error(
'record did not set. check subdomain, api key, etc'
);
}
2019-07-28 11:28:13 +00:00
2019-07-31 01:37:01 +00:00
return true;
}
);
2019-07-28 11:28:13 +00:00
},
remove: function(data) {
var ch = data.challenge;
// get all domain records of type or name
2019-07-31 01:37:01 +00:00
return api(
'GET',
'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
)
2019-07-28 11:28:13 +00:00
.then(function(resp) {
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
throw new Error('Could not get list of zones. Check api key, etc');
}
var newEntries = [];
// filter all TXT records without record to remove
for (let i = 0; i < resp.body.length; i++) {
if (resp.body[i]['data'] !== ch.dnsAuthorization) {
2019-07-31 01:37:01 +00:00
newEntries.push(resp.body[i]);
2019-07-28 11:28:13 +00:00
}
}
return newEntries;
2019-07-31 01:37:01 +00:00
})
.then(function(newRecords) {
2019-07-28 11:28:13 +00:00
// godaddy doesn't provide an endpoint for a single record removal
// but provides this endpoint to replace all records of a given type
// https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceType
// however, calling the endpoint with no records does no changes
// hence when only a single record of type exists and is the one to remove
// we call the endpoint with this dummy record
2019-07-31 01:37:01 +00:00
if (!newRecords.length) {
2019-07-28 11:28:13 +00:00
newRecords.push({
2019-07-31 01:37:01 +00:00
data: 'free_to_delete',
name: 'remove_placeholder',
ttl: 600
2019-07-28 11:28:13 +00:00
});
}
// update - overwrite all type and name records under domain
2019-07-31 01:37:01 +00:00
return api(
'PUT',
'domains/' + ch.dnsZone + '/records/TXT',
newRecords
).then(function(resp) {
2019-07-28 11:28:13 +00:00
if (200 !== resp.statusCode) {
console.error(resp.statusCode);
console.error(resp.body);
2019-07-31 01:37:01 +00:00
throw new Error(
'record did not remove. check subdomain, api key, etc'
);
2019-07-28 11:28:13 +00:00
}
return true;
});
});
},
get: function(data) {
var ch = data.challenge;
2019-07-31 01:37:01 +00:00
return api(
'GET',
'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
).then(function(resp) {
2019-07-28 11:28:13 +00:00
resp = resp.body;
var entry = null;
2019-07-31 01:37:01 +00:00
if (resp.length) {
2019-07-28 11:28:13 +00:00
entry = resp.filter(function(x) {
return x.data === ch.dnsAuthorization;
})[0];
}
2019-07-31 01:37:01 +00:00
return entry ? { dnsAuthorization: entry.data } : null;
2019-07-28 11:28:13 +00:00
});
}
};
};