From 7767df92c72608365208841ad19c4dfc03961312 Mon Sep 17 00:00:00 2001 From: shivam-bit Date: Wed, 24 Apr 2024 12:57:45 +0530 Subject: [PATCH] Add getMissingPATScopes function to auth.ts for checking missing scopes in personal access token --- web-server/src/utils/auth.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/web-server/src/utils/auth.ts b/web-server/src/utils/auth.ts index 6083dc6a0..85646e3f7 100644 --- a/web-server/src/utils/auth.ts +++ b/web-server/src/utils/auth.ts @@ -33,3 +33,22 @@ export async function checkGitHubValidity( return false; } } + +const PAT_SCOPES = ['read:org', 'read:user', 'repo', 'workflow', 'xzczcx']; +export const getMissingPATScopes = async (pat: string) => { + try { + const response = await axios.get('https://api.github.com', { + headers: { + Authorization: `token ${pat}` + } + }); + + const scopesString = response.headers['x-oauth-scopes']; + if (!scopesString) return PAT_SCOPES; + + const userScopes = scopesString.split(',').map((scope) => scope.trim()); + return PAT_SCOPES.filter((scope) => !userScopes.includes(scope)); + } catch (error) { + return PAT_SCOPES; + } +};