Run prettier on code

This commit is contained in:
Yarmo Mackenbach 2020-11-08 02:07:02 +01:00
parent 1a2c6b7b84
commit 4c6715ddb4
22 changed files with 306 additions and 204 deletions

View file

@ -18,15 +18,20 @@ const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
switch (checkRelation) {
default:
case 'contains':
re = new RegExp(checkClaim.replace('[', '\\[').replace(']', '\\]'), "gi")
re = new RegExp(
checkClaim.replace('[', '\\[').replace(']', '\\]'),
'gi'
)
res.isVerified = re.test(proofData.replace(/\r?\n|\r/, ''))
break
case 'equals':
res.isVerified = proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase()
res.isVerified =
proofData.replace(/\r?\n|\r/, '').toLowerCase() ==
checkClaim.toLowerCase()
break
case 'oneOf':
re = new RegExp(checkClaim, "gi")
res.isVerified = re.test(proofData.join("|"))
re = new RegExp(checkClaim, 'gi')
res.isVerified = re.test(proofData.join('|'))
break
}
return res
@ -39,22 +44,37 @@ const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
return res
}
res = runOnJson(res, proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation)
res = runOnJson(
res,
proofData[checkPath[0]],
checkPath.slice(1),
checkClaim,
checkRelation
)
return res
}
const run = (proofData, spData) => {
let res = {
isVerified: false,
errors: []
errors: [],
}
switch (spData.proof.format) {
case 'json':
res = runOnJson(res, proofData, spData.claim.path, utils.generateClaim(spData.claim.fingerprint, spData.claim.format), spData.claim.relation)
res = runOnJson(
res,
proofData,
spData.claim.path,
utils.generateClaim(spData.claim.fingerprint, spData.claim.format),
spData.claim.relation
)
break
case 'text':
re = new RegExp(utils.generateClaim(spData.claim.fingerprint, spData.claim.format), "gi")
re = new RegExp(
utils.generateClaim(spData.claim.fingerprint, spData.claim.format),
'gi'
)
res = re.test(proofData.replace(/\r?\n|\r/, ''))
break
}

View file

@ -20,13 +20,17 @@ const claimVerification = require('./claimVerification')
const utils = require('./utils')
const verify = async (uri, fingerprint, opts) => {
if (!fingerprint) { fingerprint = null }
if (!opts) { opts = {} }
if (!fingerprint) {
fingerprint = null
}
if (!opts) {
opts = {}
}
const defaultOpts = {
returnMatchesOnly: false,
proxyPolicy: 'adaptive',
doipProxyHostname: 'proxy.keyoxide.org'
doipProxyHostname: 'proxy.keyoxide.org',
}
opts = mergeOptions(defaultOpts, opts)
@ -39,7 +43,13 @@ const verify = async (uri, fingerprint, opts) => {
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches
}
let claimVerificationDone = false, claimVerificationResult, sp, iSp = 0, res, proofData, spData
let claimVerificationDone = false,
claimVerificationResult,
sp,
iSp = 0,
res,
proofData,
spData
while (!claimVerificationDone && iSp < spMatches.length) {
spData = spMatches[iSp]
spData.claim.fingerprint = fingerprint
@ -48,7 +58,10 @@ const verify = async (uri, fingerprint, opts) => {
if (spData.customRequestHandler instanceof Function) {
proofData = await spData.customRequestHandler(spData, opts)
} else if (!spData.proof.useProxy || 'proxyPolicy' in opts && !opts.useProxyWhenNeeded) {
} else if (
!spData.proof.useProxy ||
('proxyPolicy' in opts && !opts.useProxyWhenNeeded)
) {
proofData = await serviceproviders.directRequestHandler(spData, opts)
} else {
proofData = await serviceproviders.proxyRequestHandler(spData, opts)
@ -67,13 +80,13 @@ const verify = async (uri, fingerprint, opts) => {
if (!claimVerificationResult) {
claimVerificationResult = {
isVerified: false
isVerified: false,
}
}
return {
isVerified: claimVerificationResult.isVerified,
serviceproviderData: spData
serviceproviderData: spData,
}
}

View file

@ -52,7 +52,8 @@ const data = {
}
const match = (uri, opts) => {
let matches = [], sp
let matches = [],
sp
list.forEach((spName, i) => {
sp = data[spName]
@ -65,7 +66,11 @@ const match = (uri, opts) => {
}
const directRequestHandler = async (spData, opts) => {
const res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri, null, { Accept: 'application/json' })
const res = await req(
spData.proof.fetch ? spData.proof.fetch : spData.proof.uri,
null,
{ Accept: 'application/json' }
)
switch (spData.proof.format) {
case 'json':
@ -82,7 +87,11 @@ const directRequestHandler = async (spData, opts) => {
const proxyRequestHandler = async (spData, opts) => {
const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri
const res = await req(utils.generateProxyURL(spData.proof.format, url, opts), null, { Accept: 'application/json' })
const res = await req(
utils.generateProxyURL(spData.proof.format, url, opts),
null,
{ Accept: 'application/json' }
)
const json = await res.json()
return json.content
}

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/dev\.to\/(.*)\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'devto'
name: 'devto',
},
profile: {
display: match[1],
uri: `https://dev.to/${match[1]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://dev.to/api/articles/${match[1]}/${match[2]}`,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['body_markdown'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://dev.to/alice/post',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://dev.to/alice/post/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice/post',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/(.*)\/u\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'discourse'
name: 'discourse',
},
profile: {
display: `${match[2]}@${match[1]}`,
uri: uri,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://${match[1]}/u/${match[2]}.json`,
useProxy: true,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['user', 'bio_raw'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://domain.org/u/alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/u/alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -20,7 +20,7 @@ const utils = require('../utils')
const reURI = /^dns:([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/
const customRequestHandler = async (spData, opts) => {
if (('resolveTxt' in dns)) {
if ('resolveTxt' in dns) {
const prom = async () => {
return new Promise((resolve, reject) => {
dns.resolveTxt(spData.profile.display, (err, records) => {
@ -32,59 +32,63 @@ const customRequestHandler = async (spData, opts) => {
return {
hostname: spData.profile.display,
records: {
txt: await prom()
}
txt: await prom(),
},
}
} else {
const res = await req(spData.proof.uri, null, { Accept: 'application/json' })
const res = await req(spData.proof.uri, null, {
Accept: 'application/json',
})
const json = await res.json()
return json
}
}
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'dns'
name: 'dns',
},
profile: {
display: match[1],
uri: `https://${match[1]}`,
qr: null
qr: null,
},
proof: {
uri: utils.generateProxyURL('dns', match[1]),
fetch: null,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'uri',
path: ['records', 'txt'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: customRequestHandler
customRequestHandler: customRequestHandler,
}
}
const tests = [
{
uri: 'dns:domain.org',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'dns:domain.org?type=TXT',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/(.*)\/users\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'fediverse'
name: 'fediverse',
},
profile: {
display: `@${match[2]}@${match[1]}`,
uri: uri,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: null,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'fingerprint',
path: ['summary'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://domain.org/users/alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/users/alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/(.*)\/(.*)\/gitea_proof\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'gitea'
name: 'gitea',
},
profile: {
display: `${match[2]}@${match[1]}`,
uri: `https://${match[1]}/${match[2]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://${match[1]}/api/v1/repos/${match[2]}/gitea_proof`,
useProxy: true,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['description'],
relation: 'equals'
relation: 'equals',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://domain.org/alice/gitea_proof',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice/gitea_proof/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice/other_proof',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/gist\.github\.com\/(.*)\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'github'
name: 'github',
},
profile: {
display: match[1],
uri: `https://github.com/${match[1]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://api.github.com/gists/${match[2]}`,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['files', 'openpgp.md', 'content'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://gist.github.com/Alice/123456789',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://gist.github.com/Alice/123456789/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/Alice/123456789',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -25,66 +25,68 @@ const customRequestHandler = async (spData, opts) => {
const resUser = await req(urlUser, 'json', { Accept: 'application/json' })
const jsonUser = await resUser.json()
const user = jsonUser.find(user => user.username === match[2])
const user = jsonUser.find((user) => user.username === match[2])
if (!user) {
throw new Error(`No user with username ${match[2]}`);
throw new Error(`No user with username ${match[2]}`)
}
const urlProject = `https://${match[1]}/api/v4/users/${user.id}/projects`
const resProject = await req(urlProject, {}, { Accept: 'application/json' })
const jsonProject = await resProject.json()
const project = jsonProject.find(proj => proj.path === 'gitlab_proof')
const project = jsonProject.find((proj) => proj.path === 'gitlab_proof')
if (!project) {
throw new Error(`No project at ${spData.proof.uri}`);
throw new Error(`No project at ${spData.proof.uri}`)
}
return project
}
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'gitlab'
name: 'gitlab',
},
profile: {
display: `${match[2]}@${match[1]}`,
uri: `https://${match[1]}/${match[2]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: null,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['description'],
relation: 'equals'
relation: 'equals',
},
customRequestHandler: customRequestHandler
customRequestHandler: customRequestHandler,
}
}
const tests = [
{
uri: 'https://gitlab.domain.org/alice/gitlab_proof',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://gitlab.domain.org/alice/gitlab_proof/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice/other_proof',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/news\.ycombinator\.com\/user\?id=(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'hackernews'
name: 'hackernews',
},
profile: {
display: match[1],
uri: uri,
qr: null
qr: null,
},
proof: {
uri: `https://hacker-news.firebaseio.com/v0/user/${match[1]}.json`,
fetch: null,
useProxy: true,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'uri',
path: ['about'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://news.ycombinator.com/user?id=Alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://news.ycombinator.com/user?id=Alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/user?id=Alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/liberapay\.com\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'liberapay'
name: 'liberapay',
},
profile: {
display: match[1],
uri: uri,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://liberapay.com/${match[1]}/public.json`,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['statements', 'content'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://liberapay.com/alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://liberapay.com/alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/lobste\.rs\/u\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'lobsters'
name: 'lobsters',
},
profile: {
display: match[1],
uri: uri,
qr: null
qr: null,
},
proof: {
uri: `https://lobste.rs/u/${match[1]}.json`,
fetch: null,
useProxy: true,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['about'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://lobste.rs/u/Alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://lobste.rs/u/Alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/u/Alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/(.*)\/@(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'mastodon'
name: 'mastodon',
},
profile: {
display: `@${match[2]}@${match[1]}`,
uri: uri,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: null,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'fingerprint',
path: ['attachment', 'value'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://domain.org/@alice',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/@alice/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,56 +16,58 @@ limitations under the License.
const reURI = /^https:\/\/(?:www\.)?reddit\.com\/user\/(.*)\/comments\/(.*)\/(.*)\/?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'reddit'
name: 'reddit',
},
profile: {
display: match[1],
uri: `https://www.reddit.com/user/${match[1]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://www.reddit.com/user/${match[1]}/comments/${match[2]}.json`,
useProxy: true,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: ['data', 'children', 'data', 'selftext'],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://www.reddit.com/user/Alice/comments/123456/post',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://www.reddit.com/user/Alice/comments/123456/post/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://reddit.com/user/Alice/comments/123456/post',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://reddit.com/user/Alice/comments/123456/post/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/user/Alice/comments/123456/post',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,48 +16,50 @@ limitations under the License.
const reURI = /^https:\/\/twitter\.com\/(.*)\/status\/([0-9]*)(?:\?.*)?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'web',
name: 'twitter'
name: 'twitter',
},
profile: {
display: `@${match[1]}`,
uri: `https://twitter.com/${match[1]}`,
qr: null
qr: null,
},
proof: {
uri: uri,
fetch: `https://mobile.twitter.com/${match[1]}/status/${match[2]}`,
useProxy: false,
format: 'text'
format: 'text',
},
claim: {
fingerprint: null,
format: 'message',
path: [],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'https://twitter.com/alice/status/1234567890123456789',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://twitter.com/alice/status/1234567890123456789/',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org/alice/status/1234567890123456789',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -16,50 +16,53 @@ limitations under the License.
const reURI = /^xmpp:([a-zA-Z0-9\.\-\_]*)@([a-zA-Z0-9\.\-\_]*)(?:\?(.*))?/
const processURI = (uri, opts) => {
if (!opts) { opts = {} }
if (!opts) {
opts = {}
}
const match = uri.match(reURI)
return {
serviceprovider: {
type: 'communication',
name: 'xmpp'
name: 'xmpp',
},
profile: {
display: `${match[1]}@${match[2]}`,
uri: uri,
qr: uri
qr: uri,
},
proof: {
uri: 'xmppVcardServerDomain' in opts
uri:
'xmppVcardServerDomain' in opts
? `https://${opts.xmppVcardServerDomain}/api/vcard/${match[1]}@${match[2]}/DESC`
: null,
fetch: null,
useProxy: false,
format: 'json'
format: 'json',
},
claim: {
fingerprint: null,
format: 'message',
path: [],
relation: 'contains'
relation: 'contains',
},
customRequestHandler: null
customRequestHandler: null,
}
}
const tests = [
{
uri: 'xmpp:alice@domain.org',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'xmpp:alice@domain.org?omemo-sid-123456789=A1B2C3D4E5F6G7H8I9',
shouldMatch: true
shouldMatch: true,
},
{
uri: 'https://domain.org',
shouldMatch: false
}
shouldMatch: false,
},
]
exports.reURI = reURI

View file

@ -1,19 +1,23 @@
const generateProxyURL = (type, url, opts) => {
if (!opts || !opts.doipProxyHostname) { return null }
return `https://${opts.doipProxyHostname}/api/1/get/${type}/${encodeURIComponent(url)}`
if (!opts || !opts.doipProxyHostname) {
return null
}
return `https://${
opts.doipProxyHostname
}/api/1/get/${type}/${encodeURIComponent(url)}`
}
const generateClaim = (fingerprint, format) => {
switch (format) {
case 'uri':
return `openpgp4fpr:${fingerprint}`
break;
break
case 'message':
return `[Verifying my OpenPGP key: openpgp4fpr:${fingerprint}]`
break;
break
case 'fingerprint':
return fingerprint
break;
break
default:
throw new Error('No valid claim format')
}

View file

@ -37,14 +37,15 @@ doipjs.serviceproviders.list.forEach((sp, i) => {
doipjs.serviceproviders.data[sp].tests.forEach((test, j) => {
if (test.shouldMatch) {
it(`should match "${test.uri}"`, () => {
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.true
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be
.true
})
} else {
it(`should not match "${test.uri}"`, () => {
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be.false
expect(doipjs.serviceproviders.data[sp].reURI.test(test.uri)).to.be
.false
})
}
})
})
})

View file

@ -24,12 +24,18 @@ describe('utils.generateClaim', () => {
expect(doipjs.utils.generateClaim).to.have.length(2)
})
it('should generate a correct "uri" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'uri')).to.equal('openpgp4fpr:123456789')
expect(doipjs.utils.generateClaim('123456789', 'uri')).to.equal(
'openpgp4fpr:123456789'
)
})
it('should generate a correct "message" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'message')).to.equal('[Verifying my OpenPGP key: openpgp4fpr:123456789]')
expect(doipjs.utils.generateClaim('123456789', 'message')).to.equal(
'[Verifying my OpenPGP key: openpgp4fpr:123456789]'
)
})
it('should generate a correct "fingerprint" claim', () => {
expect(doipjs.utils.generateClaim('123456789', 'fingerprint')).to.equal('123456789')
expect(doipjs.utils.generateClaim('123456789', 'fingerprint')).to.equal(
'123456789'
)
})
})

View file

@ -17,7 +17,7 @@ const chai = require('chai')
const expect = chai.expect
const chaiMatchPattern = require('chai-match-pattern')
chai.use(chaiMatchPattern)
chai.use(require("chai-as-promised"))
chai.use(require('chai-as-promised'))
const _ = chaiMatchPattern.getLodashModule()
const doipjs = require('../src')
@ -25,26 +25,36 @@ const doipjs = require('../src')
const pattern = {
serviceprovider: {
type: _.isString,
name: _.isString
name: _.isString,
},
profile: {
display: _.isString,
uri: _.isString,
qr: (x) => { return _.isString(x) || _.isNull(x) }
qr: (x) => {
return _.isString(x) || _.isNull(x)
},
},
proof: {
uri: (x) => { return _.isString(x) || _.isNull(x) },
fetch: (x) => { return _.isString(x) || _.isNull(x) },
uri: (x) => {
return _.isString(x) || _.isNull(x)
},
fetch: (x) => {
return _.isString(x) || _.isNull(x)
},
useProxy: _.isBoolean,
format: _.isString
format: _.isString,
},
claim: {
fingerprint: (x) => { return _.isString(x) || _.isNull(x) },
fingerprint: (x) => {
return _.isString(x) || _.isNull(x)
},
format: _.isString,
path: _.isArray,
relation: _.isString
relation: _.isString,
},
customRequestHandler: (x) => {
return _.isFunction(x) || _.isNull(x) || _.isUndefined(x)
},
customRequestHandler: (x) => { return _.isFunction(x) || _.isNull(x) || _.isUndefined(x) }
}
describe('verify', () => {
@ -53,17 +63,25 @@ describe('verify', () => {
expect(doipjs.verify).to.have.length(3)
})
it('should throw an error for non-valid URIs', () => {
return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith('Not a valid URI')
return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith('Not a valid URI')
return expect(doipjs.verify('noURI')).to.eventually.be.rejectedWith(
'Not a valid URI'
)
return expect(doipjs.verify('domain.org')).to.eventually.be.rejectedWith(
'Not a valid URI'
)
})
doipjs.serviceproviders.list.forEach((spName, i) => {
const sp = doipjs.serviceproviders.data[spName]
if (sp.tests.length == 0) { return }
if (sp.tests.length == 0) {
return
}
it(`should return a valid object for the "${spName}" service provider`, async () => {
const matches = await doipjs.verify(sp.tests[0].uri, null, {returnMatchesOnly: true})
const matches = await doipjs.verify(sp.tests[0].uri, null, {
returnMatchesOnly: true,
})
expect(matches).to.be.a('array')
expect(matches).to.be.length.above(0)
expect(matches[0].serviceprovider.name).to.be.equal(spName)