Skip to content

Commit

Permalink
Do not pass oauth2 as a username to Git credentials for Bitbucket (#662)
Browse files Browse the repository at this point in the history
Set bitbucket-***** as a token name annotation for bitbucket token secret. This is needed to pass username instead of oauth2 for bitbucket credentials
  • Loading branch information
vinokurig authored Mar 7, 2024
1 parent 7dc7a61 commit 6cdf6f6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
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

0 comments on commit 6cdf6f6

Please sign in to comment.