Skip to content

Commit

Permalink
Use token name to check if the token is generated from oAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Nov 27, 2024
1 parent 5f2169d commit 41eb64d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
gitOauthErrorAction,
gitOauthReceiveAction,
gitOauthRequestAction,
isTokenGitProvider,
isOauthToken,
skipOauthReceiveAction,
} from '@/store/GitOauthConfig/actions';
import { IGitOauth } from '@/store/GitOauthConfig/reducer';
Expand Down Expand Up @@ -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-<user-id>-<${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-<user-id>-<invalid-hostname>`;
const result = isTokenGitProvider(gitProvider);
const result = isOauthToken(gitProvider);
expect(result).toBe(false);
});
});
Expand All @@ -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-<user-id>-<${window.location.hostname}>`,
gitProviderEndpoint: 'https://bitbucket-server.com/',
gitProvider: 'bitbucket-server',
tokenName: `che-token-<user-id>-<${window.location.hostname}>`,
},
] as unknown as api.PersonalAccessToken[];

Expand All @@ -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',
Expand All @@ -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,
];

Expand All @@ -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-<user-id>-<${window.location.hostname}>`,
gitProviderEndpoint: 'https://bitbucket-server.org/',
gitProvider: 'bitbucket-server',
tokenName: `che-token-<user-id>-<${window.location.hostname}>`,
} as unknown as api.PersonalAccessToken,
];

Expand Down
14 changes: 7 additions & 7 deletions packages/dashboard-frontend/src/store/GitOauthConfig/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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-<user id>-<che hostname>'
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-<user id>-<che hostname>'
new RegExp(`^che-token-<.*>-<${window.location.hostname}>$`).test(tokenName)
);
}

0 comments on commit 41eb64d

Please sign in to comment.