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

Do not pass oauth2 as a username to Git credentials for Bitbucket #662

Merged
merged 2 commits into from
Mar 7, 2024
Merged
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 @@ -111,7 +111,7 @@ public Response callback(UriInfo uriInfo, @Nullable List<String> errorValues)
EnvironmentContext.getCurrent().getSubject().getUserId(),
null,
null,
NameGenerator.generate(OAUTH_2_PREFIX, 5),
generateTokenName(providerName),
NameGenerator.generate("id-", 5),
token));
} catch (OAuthAuthenticationException e) {
Expand All @@ -135,6 +135,18 @@ public Response callback(UriInfo uriInfo, @Nullable List<String> errorValues)
return Response.temporaryRedirect(uri).build();
}

/*
* This value is used for generating git credentials. Most of the git providers work with git
* credentials with OAuth token in format "ouath2:<oauth token>" but bitbucket requires username
* to be explicitly set: "<username>:<oauth token>, see {@link
* GitCredentialManager#createOrReplace}
* TODO: needs to be moved to the specific bitbucket implementation.
*/
private String generateTokenName(String providerName) {
return NameGenerator.generate(
"bitbucket".equals(providerName) ? providerName + "-" : OAUTH_2_PREFIX, 5);
}

/**
* Encode the redirect URL query parameters to avoid the error when the redirect URL contains
* JSON, as a query parameter. This prevents passing unsupported characters, like '{' and '}' to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ public void shouldStoreTokenOnCallback() throws Exception {
assertEquals(token.getToken(), "token");
}

@Test
public void shouldStoreBitbucketTokenOnCallback() throws Exception {
// given
UriInfo uriInfo = mock(UriInfo.class);
OAuthAuthenticator authenticator = mock(OAuthAuthenticator.class);
when(authenticator.getEndpointUrl()).thenReturn("http://eclipse.che");
when(authenticator.callback(any(URL.class), anyList())).thenReturn("token");
when(uriInfo.getRequestUri())
.thenReturn(
new URI(
"http://eclipse.che?state=oauth_provider%3Dbitbucket%26redirect_after_login%3DredirectUrl"));
when(oauth2Providers.getAuthenticator("bitbucket")).thenReturn(authenticator);
ArgumentCaptor<PersonalAccessToken> tokenCapture =
ArgumentCaptor.forClass(PersonalAccessToken.class);

// when
embeddedOAuthAPI.callback(uriInfo, emptyList());

// then
verify(personalAccessTokenManager).store(tokenCapture.capture());
PersonalAccessToken token = tokenCapture.getValue();
assertEquals(token.getScmProviderUrl(), "http://eclipse.che");
assertEquals(token.getCheUserId(), "0000-00-0000");
assertTrue(token.getScmTokenId().startsWith("id-"));
assertTrue(token.getScmTokenName().startsWith("bitbucket-"));
assertEquals(token.getToken(), "token");
}

@Test
public void shouldEncodeRedirectUrl() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2024 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -16,7 +16,8 @@

public interface GitCredentialManager {
/**
* Persists PersonalAccessToken for the future usage.
* Propagates git credentials in format: "username:<oauth token>" if the token is Personal Access
* Token or "oauth2:<oauth token> if oAuth token.
*
* @param personalAccessToken
* @throws UnsatisfiedScmPreconditionException - some storage preconditions aren't met.
Expand Down
Loading