From 41eb64df69e3eaa3de7b4823783554e81ab256cd Mon Sep 17 00:00:00 2001 From: ivinokur Date: Wed, 27 Nov 2024 11:13:29 +0200 Subject: [PATCH] Use token name to check if the token is generated from oAuth --- .../GitOauthConfig/__tests__/actions.spec.ts | 43 +++++++++++++------ .../src/store/GitOauthConfig/actions.ts | 14 +++--- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/actions.spec.ts b/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/actions.spec.ts index ede4d4cda..6a0b76603 100644 --- a/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/actions.spec.ts +++ b/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/actions.spec.ts @@ -31,7 +31,7 @@ import { gitOauthErrorAction, gitOauthReceiveAction, gitOauthRequestAction, - isTokenGitProvider, + isOauthToken, skipOauthReceiveAction, } from '@/store/GitOauthConfig/actions'; import { IGitOauth } from '@/store/GitOauthConfig/reducer'; @@ -297,25 +297,25 @@ describe('GitOauthConfig', () => { describe('isTokenGitProvider', () => { it('should return true for oauth2 git provider', () => { const gitProvider = 'oauth2-provider'; - const result = isTokenGitProvider(gitProvider); + const result = isOauthToken(gitProvider); expect(result).toBe(true); }); it('should return true for bitbucket-server token format', () => { const gitProvider = `che-token--<${window.location.hostname}>`; - const result = isTokenGitProvider(gitProvider); + const result = isOauthToken(gitProvider); expect(result).toBe(true); }); it('should return false for non-oauth2 and non-bitbucket-server token format', () => { const gitProvider = 'github'; - const result = isTokenGitProvider(gitProvider); + const result = isOauthToken(gitProvider); expect(result).toBe(false); }); it('should return false for invalid bitbucket-server token format', () => { const gitProvider = `che-token--`; - const result = isTokenGitProvider(gitProvider); + const result = isOauthToken(gitProvider); expect(result).toBe(false); }); }); @@ -330,15 +330,18 @@ describe('GitOauthConfig', () => { const mockTokens = [ { gitProviderEndpoint: 'https://github.com/', - gitProvider: 'oauth2-provider', + gitProvider: 'github', + tokenName: 'oauth2-provider', }, { gitProviderEndpoint: 'https://bitbucket.org/', - gitProvider: 'oauth2-provider', + gitProvider: 'bitbucket', + tokenName: 'oauth2-provider', }, { - gitProviderEndpoint: 'https://github.com/', - gitProvider: `che-token--<${window.location.hostname}>`, + gitProviderEndpoint: 'https://bitbucket-server.com/', + gitProvider: 'bitbucket-server', + tokenName: `che-token--<${window.location.hostname}>`, }, ] as unknown as api.PersonalAccessToken[]; @@ -357,6 +360,19 @@ describe('GitOauthConfig', () => { expect(result).toEqual([]); }); + it('should return an empty array with PAT', () => { + const mockTokens = [ + { + gitProviderEndpoint: 'https://github.com/', + gitProvider: 'github', + tokenName: 'token-name', + }, + ] as unknown as api.PersonalAccessToken[]; + + const result = findUserToken(mockGitOauth, mockTokens); + expect(result).toEqual([]); + }); + it('should normalize endpoint URLs before comparison', () => { const mockGitOauthWithTrailingSlash = { name: 'github', @@ -369,7 +385,7 @@ describe('GitOauthConfig', () => { gitProvider: 'oauth2-provider', cheUserId: 'test-user', tokenData: 'test-token-data', - tokenName: 'test-token', + tokenName: 'oauth2-token-name', } as unknown as api.PersonalAccessToken, ]; @@ -380,13 +396,14 @@ describe('GitOauthConfig', () => { it('should handle bitbucket-server token format', () => { const mockGitOauthBitbucket = { name: 'bitbucket', - endpointUrl: 'https://bitbucket.org/', + endpointUrl: 'https://bitbucket-server.org/', } as IGitOauth; const mockTokensBitbucket = [ { - gitProviderEndpoint: 'https://bitbucket.org/', - gitProvider: `che-token--<${window.location.hostname}>`, + gitProviderEndpoint: 'https://bitbucket-server.org/', + gitProvider: 'bitbucket-server', + tokenName: `che-token--<${window.location.hostname}>`, } as unknown as api.PersonalAccessToken, ]; diff --git a/packages/dashboard-frontend/src/store/GitOauthConfig/actions.ts b/packages/dashboard-frontend/src/store/GitOauthConfig/actions.ts index b4f023959..fcbd3df99 100644 --- a/packages/dashboard-frontend/src/store/GitOauthConfig/actions.ts +++ b/packages/dashboard-frontend/src/store/GitOauthConfig/actions.ts @@ -175,9 +175,9 @@ export function findUserToken(gitOauth: IGitOauth, tokens: api.PersonalAccessTok : token.gitProviderEndpoint; // compare Git OAuth Endpoint url ONLY with OAuth tokens - const gitProvider = token.gitProvider; + const tokenName = token.tokenName; if ( - isTokenGitProvider(gitProvider) && + isOauthToken(tokenName) && normalizedGitOauthEndpoint === normalizedTokenGitProviderEndpoint ) { providersWithToken.push(gitOauth.name); @@ -188,12 +188,12 @@ export function findUserToken(gitOauth: IGitOauth, tokens: api.PersonalAccessTok } /** - * For compatibility with the old format of the git provider value + * Check if the token name is generated by the oAuth2 API. */ -export function isTokenGitProvider(gitProvider: string): boolean { +export function isOauthToken(tokenName: string): boolean { return ( - gitProvider.startsWith('oauth2') || - // The git provider value format of a bitbucket-server token is 'che-token--' - new RegExp(`^che-token-<.*>-<${window.location.hostname}>$`).test(gitProvider) + tokenName.startsWith('oauth2') || + // The token name value format of a bitbucket-server token is 'che-token--' + new RegExp(`^che-token-<.*>-<${window.location.hostname}>$`).test(tokenName) ); }