2020-10-24 04:05:11 -06:00
|
|
|
/*
|
2020-10-24 07:11:14 -06:00
|
|
|
Copyright 2020 Yarmo Mackenbach
|
2020-10-24 04:05:11 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2020-10-24 04:05:11 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-10-24 04:05:11 -06:00
|
|
|
|
2020-10-24 07:11:14 -06:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2020-10-24 04:05:11 -06:00
|
|
|
*/
|
2020-11-05 04:57:37 -07:00
|
|
|
const dns = require('dns')
|
|
|
|
const bent = require('bent')
|
|
|
|
const req = bent('GET')
|
2020-11-03 19:04:22 -07:00
|
|
|
const utils = require('../utils')
|
2020-10-24 03:18:06 -06:00
|
|
|
const reURI = /^dns:([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/
|
2020-10-23 14:35:53 -06:00
|
|
|
|
2020-11-05 04:57:37 -07:00
|
|
|
const customRequestHandler = async (spData, opts) => {
|
2020-11-07 18:07:02 -07:00
|
|
|
if ('resolveTxt' in dns) {
|
2020-11-05 04:57:37 -07:00
|
|
|
const prom = async () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
dns.resolveTxt(spData.profile.display, (err, records) => {
|
|
|
|
if (err) reject(err)
|
|
|
|
resolve(records)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
hostname: spData.profile.display,
|
|
|
|
records: {
|
2020-11-07 18:07:02 -07:00
|
|
|
txt: await prom(),
|
|
|
|
},
|
2020-11-05 04:57:37 -07:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-07 18:07:02 -07:00
|
|
|
const res = await req(spData.proof.uri, null, {
|
|
|
|
Accept: 'application/json',
|
|
|
|
})
|
2020-11-05 04:57:37 -07:00
|
|
|
const json = await res.json()
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 03:20:34 -06:00
|
|
|
const processURI = (uri, opts) => {
|
2020-11-07 18:07:02 -07:00
|
|
|
if (!opts) {
|
|
|
|
opts = {}
|
|
|
|
}
|
2020-10-24 03:20:34 -06:00
|
|
|
const match = uri.match(reURI)
|
2020-10-23 14:35:53 -06:00
|
|
|
|
|
|
|
return {
|
2020-10-24 08:42:22 -06:00
|
|
|
serviceprovider: {
|
|
|
|
type: 'web',
|
2020-11-07 18:07:02 -07:00
|
|
|
name: 'dns',
|
2020-10-24 08:42:22 -06:00
|
|
|
},
|
2020-10-23 14:35:53 -06:00
|
|
|
profile: {
|
|
|
|
display: match[1],
|
2020-11-05 18:27:56 -07:00
|
|
|
uri: `https://${match[1]}`,
|
2020-11-07 18:07:02 -07:00
|
|
|
qr: null,
|
2020-10-23 14:35:53 -06:00
|
|
|
},
|
|
|
|
proof: {
|
2020-12-09 18:59:04 -07:00
|
|
|
uri: utils.generateProxyURL('dns', match[1], opts),
|
2020-10-24 08:42:22 -06:00
|
|
|
fetch: null,
|
2020-10-24 09:30:22 -06:00
|
|
|
useProxy: false,
|
2020-11-07 18:07:02 -07:00
|
|
|
format: 'json',
|
2020-10-24 08:42:22 -06:00
|
|
|
},
|
|
|
|
claim: {
|
|
|
|
fingerprint: null,
|
|
|
|
format: 'uri',
|
2020-11-03 18:41:24 -07:00
|
|
|
path: ['records', 'txt'],
|
2020-11-07 18:07:02 -07:00
|
|
|
relation: 'contains',
|
2020-10-23 14:35:53 -06:00
|
|
|
},
|
2020-11-07 18:07:02 -07:00
|
|
|
customRequestHandler: customRequestHandler,
|
2020-10-23 14:35:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
2020-10-24 03:18:06 -06:00
|
|
|
uri: 'dns:domain.org',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: true,
|
2020-10-23 14:35:53 -06:00
|
|
|
},
|
|
|
|
{
|
2020-10-24 03:18:06 -06:00
|
|
|
uri: 'dns:domain.org?type=TXT',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: true,
|
2020-10-23 14:35:53 -06:00
|
|
|
},
|
|
|
|
{
|
2020-10-24 03:18:06 -06:00
|
|
|
uri: 'https://domain.org',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: false,
|
|
|
|
},
|
2020-10-23 14:35:53 -06:00
|
|
|
]
|
|
|
|
|
2020-10-24 03:18:06 -06:00
|
|
|
exports.reURI = reURI
|
2020-10-24 03:20:34 -06:00
|
|
|
exports.processURI = processURI
|
2020-10-23 14:35:53 -06:00
|
|
|
exports.tests = tests
|