2020-10-26 15:33:05 -06:00
|
|
|
/*
|
|
|
|
Copyright 2020 Yarmo Mackenbach
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const bent = require('bent')
|
|
|
|
const req = bent('GET')
|
|
|
|
|
|
|
|
const reURI = /^https:\/\/(.*)\/(.*)\/gitlab_proof\/?/
|
|
|
|
|
|
|
|
const customRequestHandler = async (spData, opts) => {
|
|
|
|
const match = spData.proof.uri.match(reURI)
|
|
|
|
|
|
|
|
const urlUser = `https://${match[1]}/api/v4/users?username=${match[2]}`
|
2020-12-10 15:23:43 -07:00
|
|
|
let resUser
|
|
|
|
try {
|
|
|
|
resUser = await req(urlUser, null, { Accept: 'application/json' })
|
|
|
|
} catch (e) {
|
2020-12-20 15:19:07 -07:00
|
|
|
resUser = await req(utils.generateProxyURL('web', urlUser, opts), null, {
|
|
|
|
Accept: 'application/json',
|
|
|
|
})
|
2020-12-10 15:23:43 -07:00
|
|
|
}
|
2020-10-26 15:33:05 -06:00
|
|
|
const jsonUser = await resUser.json()
|
|
|
|
|
2020-11-07 18:07:02 -07:00
|
|
|
const user = jsonUser.find((user) => user.username === match[2])
|
2020-10-26 15:33:05 -06:00
|
|
|
if (!user) {
|
2020-11-07 18:07:02 -07:00
|
|
|
throw new Error(`No user with username ${match[2]}`)
|
2020-10-26 15:33:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const urlProject = `https://${match[1]}/api/v4/users/${user.id}/projects`
|
2020-12-10 15:23:43 -07:00
|
|
|
let resProject
|
|
|
|
try {
|
|
|
|
resProject = await req(urlProject, null, { Accept: 'application/json' })
|
|
|
|
} catch (e) {
|
|
|
|
resProject = await req(
|
|
|
|
utils.generateProxyURL('web', urlProject, opts),
|
|
|
|
null,
|
|
|
|
{ Accept: 'application/json' }
|
|
|
|
)
|
|
|
|
}
|
2020-10-26 15:33:05 -06:00
|
|
|
const jsonProject = await resProject.json()
|
|
|
|
|
2020-11-07 18:07:02 -07:00
|
|
|
const project = jsonProject.find((proj) => proj.path === 'gitlab_proof')
|
2020-10-26 15:33:05 -06:00
|
|
|
if (!project) {
|
2020-11-07 18:07:02 -07:00
|
|
|
throw new Error(`No project at ${spData.proof.uri}`)
|
2020-10-26 15:33:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return project
|
|
|
|
}
|
|
|
|
|
|
|
|
const processURI = (uri, opts) => {
|
2020-11-07 18:07:02 -07:00
|
|
|
if (!opts) {
|
|
|
|
opts = {}
|
|
|
|
}
|
2020-10-26 15:33:05 -06:00
|
|
|
const match = uri.match(reURI)
|
|
|
|
|
|
|
|
return {
|
|
|
|
serviceprovider: {
|
|
|
|
type: 'web',
|
2020-11-07 18:07:02 -07:00
|
|
|
name: 'gitlab',
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
|
|
|
profile: {
|
|
|
|
display: `${match[2]}@${match[1]}`,
|
2020-11-05 18:27:56 -07:00
|
|
|
uri: `https://${match[1]}/${match[2]}`,
|
2020-11-07 18:07:02 -07:00
|
|
|
qr: null,
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
|
|
|
proof: {
|
|
|
|
uri: uri,
|
|
|
|
fetch: null,
|
|
|
|
useProxy: false,
|
2020-11-07 18:07:02 -07:00
|
|
|
format: 'json',
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
|
|
|
claim: {
|
|
|
|
fingerprint: null,
|
|
|
|
format: 'message',
|
|
|
|
path: ['description'],
|
2020-11-07 18:07:02 -07:00
|
|
|
relation: 'equals',
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
2020-11-07 18:07:02 -07:00
|
|
|
customRequestHandler: customRequestHandler,
|
2020-10-26 15:33:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
2020-10-26 15:55:27 -06:00
|
|
|
uri: 'https://gitlab.domain.org/alice/gitlab_proof',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: true,
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
|
|
|
{
|
2020-10-26 15:55:27 -06:00
|
|
|
uri: 'https://gitlab.domain.org/alice/gitlab_proof/',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: true,
|
2020-10-26 15:33:05 -06:00
|
|
|
},
|
|
|
|
{
|
2020-10-26 15:55:27 -06:00
|
|
|
uri: 'https://domain.org/alice/other_proof',
|
2020-11-07 18:07:02 -07:00
|
|
|
shouldMatch: false,
|
|
|
|
},
|
2020-10-26 15:33:05 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
exports.reURI = reURI
|
|
|
|
exports.processURI = processURI
|
|
|
|
exports.tests = tests
|