From be2cf693b1794cd59997bff34f6b97d50b907711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Simi=C4=87?= Date: Wed, 19 Aug 2020 20:05:41 +0200 Subject: [PATCH] Check for repo instead of domain When checking if url is GitLab proof, check if it points to a gitlab_proof repo instad of the gitlab.com domain. This way self hosted instances could be added. Also go back to searching for user first and then for the repo since repo search is unreliable. --- static/scripts.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/static/scripts.js b/static/scripts.js index 7281794..b344fca 100644 --- a/static/scripts.js +++ b/static/scripts.js @@ -616,24 +616,38 @@ async function verifyProof(url, fingerprint) { } } // GitLab - if (/^https:\/\/gitlab.com/.test(url)) { + if (/\/gitlab_proof$/.test(url)) { output.type = "gitlab"; - match = url.match(/https:\/\/gitlab.com\/(.*)\/(.*)/); - output.display = match[1]; - output.url = `https://gitlab.com/${match[1]}`; - output.proofUrlFetch = `https://gitlab.com/api/v4/projects?custom_attributes[search]=${match[1]}/${match[2]}&custom_attributes[search_namespaces]=true`; + match = url.match(/https:\/\/(.*)\/(.*)\/gitlab_proof/); + output.display = match[2]; + output.url = `https://${match[1]}/${match[2]}`; + output.proofUrlFetch = `https://gitlab.com/api/v4/users?username=${match[2]}`; + // output.proofUrlFetch = `https://gitlab.com/api/v4/projects?custom_attributes[search]=${match[2]}/gitlab_proof&custom_attributes[search_namespaces]=true`; try { - response = await fetch(output.proofUrlFetch, { + const opts = { headers: { Accept: 'application/json' }, credentials: 'omit' - }); + }; + // Get user + response = await fetch(output.proofUrlFetch, opts); if (!response.ok) { throw new Error('Response failed: ' + response.status); } json = await response.json(); - let project = json.find(proj => proj.web_url === url); + const user = json.find(user => user.username === match[2]); + if (!user) { + throw new Error('No user with username ' + match[2]); + } + // Get project + output.proofUrlFetch = `https://gitlab.com/api/v4/users/${user.id}/projects`; + response = await fetch(output.proofUrlFetch, opts); + if (!response.ok) { + throw new Error('Response failed: ' + response.status); + } + json = await response.json(); + const project = json.find(proj => proj.path === 'gitlab_proof'); if (!project) { throw new Error('No project at ' + url); }