Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use token name instead of provider name to check if the token is generated from oAuth #1267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
);
}
Loading