Added support for authentication tokens + fix for #1 #2
20
lib/index.js
20
lib/index.js
|
@ -44,12 +44,12 @@ module.exports.create = function (config) {
|
||||||
defaultHeaders['X-Auth-Key'] = config.authKey
|
defaultHeaders['X-Auth-Key'] = config.authKey
|
||||||
}
|
}
|
||||||
function api (method, path, body, tokenType) {
|
function api (method, path, body, tokenType) {
|
||||||
const headers = defaultHeaders;
|
const headers = defaultHeaders
|
||||||
if (tokenType && config.bearerTokens) {
|
if (tokenType && config.bearerTokens) {
|
||||||
if (!(tokenType in config.bearerTokens)) {
|
if (!(tokenType in config.bearerTokens)) {
|
||||||
throw new Error('Unrecognized token type');
|
throw new Error('Unrecognized token type')
|
||||||
}
|
}
|
||||||
headers['Authorization'] = 'Bearer ' + config.bearerTokens[tokenType];
|
headers.Authorization = 'Bearer ' + config.bearerTokens[tokenType]
|
||||||
}
|
}
|
||||||
return request({
|
return request({
|
||||||
url: baseUrl + path,
|
url: baseUrl + path,
|
||||||
|
@ -92,7 +92,7 @@ module.exports.create = function (config) {
|
||||||
throw new Error('Can not edit zone ' + JSON.stringify(domain) + ' from this account')
|
throw new Error('Can not edit zone ' + JSON.stringify(domain) + ' from this account')
|
||||||
}
|
}
|
||||||
|
|
||||||
const resp = await api('POST', `/zones/${zone.id}/dns_records`, {type: 'TXT', name: dnsPrefix, content: txtRecord, ttl: 300}, 'zone')
|
const resp = await api('POST', `/zones/${zone.id}/dns_records`, { type: 'TXT', name: dnsPrefix, content: txtRecord, ttl: 300 }, 'zone')
|
||||||
if (resp.statusCode !== 200) {
|
if (resp.statusCode !== 200) {
|
||||||
formatError('Could not add record', resp)
|
formatError('Could not add record', resp)
|
||||||
}
|
}
|
||||||
|
@ -113,14 +113,14 @@ module.exports.create = function (config) {
|
||||||
throw new Error('Can not edit zone ' + JSON.stringify(domain) + ' from this account')
|
throw new Error('Can not edit zone ' + JSON.stringify(domain) + ' from this account')
|
||||||
}
|
}
|
||||||
|
|
||||||
const resp = await api('GET', `/zones/${zone.id}/dns_records?name=${encodeURI(dnsPrefix + '.' + domain)}`, undefined,'zone')
|
const resp = await api('GET', `/zones/${zone.id}/dns_records?name=${encodeURI(dnsPrefix + '.' + domain)}`, undefined, 'zone')
|
||||||
if (resp.statusCode !== 200) {
|
if (resp.statusCode !== 200) {
|
||||||
formatError('Could not read record', resp)
|
formatError('Could not read record', resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
let {result} = resp.body
|
const { result } = resp.body
|
||||||
|
|
||||||
let record = result.filter(record => (record.type === 'TXT' && record.content === txtRecord))[0]
|
const record = result.filter(record => (record.type === 'TXT' && record.content === txtRecord))[0]
|
||||||
|
|
||||||
if (record) {
|
if (record) {
|
||||||
const resp = await api('DELETE', `/zones/${zone.id}/dns_records/${record.id}`, undefined, 'zone')
|
const resp = await api('DELETE', `/zones/${zone.id}/dns_records/${record.id}`, undefined, 'zone')
|
||||||
|
@ -149,12 +149,12 @@ module.exports.create = function (config) {
|
||||||
formatError('Could not read record', resp)
|
formatError('Could not read record', resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
let {result} = resp.body
|
const { result } = resp.body
|
||||||
|
|
||||||
let record = result.filter(record => (record.type === 'TXT' && record.content === txtRecord))[0]
|
const record = result.filter(record => (record.type === 'TXT' && record.content === txtRecord))[0]
|
||||||
|
|
||||||
if (record) {
|
if (record) {
|
||||||
return {dnsAuthorization: record.content}
|
return { dnsAuthorization: record.content }
|
||||||
} else {
|
} else {
|
||||||
return null // TODO: not found. should this throw?!
|
return null // TODO: not found. should this throw?!
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -25,6 +25,13 @@
|
||||||
"@root/request": "^1.3.11"
|
"@root/request": "^1.3.11"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"acme-dns-01-test": "^3.2.1"
|
"acme-dns-01-test": "^3.2.1",
|
||||||
|
"standard": "^14.1.0",
|
||||||
|
"husky": "^3.0.4"
|
||||||
|
},
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "npx standard --fix && git update-index --again"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
test.js
2
test.js
|
@ -11,7 +11,7 @@ const [zone, authEmail, authType, credential, zoneToken] = process.argv.slice(2)
|
||||||
const config = { authEmail }
|
const config = { authEmail }
|
||||||
switch (authType) {
|
switch (authType) {
|
||||||
case 'token':
|
case 'token':
|
||||||
config.bearerTokens = {list: credential, zone: zoneToken || credential}
|
config.bearerTokens = { list: credential, zone: zoneToken || credential }
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
config.authKey = credential
|
config.authKey = credential
|
||||||
|
|
Loading…
Reference in New Issue