Skip to content

Commit a71d0e0

Browse files
authored
Fix git clone of repository using OAuth2 authenticator (#926)
This issue is caused by removing user from clone link. Using "x-token-auth" as user tells bitbucket that the specified password is an OAuth2 credential token. Reference https://developer.atlassian.com/cloud/bitbucket/oauth-2/#repository-cloning
1 parent 6a215c8 commit a71d0e0

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
3737
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
3838
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam;
39+
import com.cloudbees.jenkins.plugins.bitbucket.api.credentials.BitbucketUsernamePasswordAuthenticator;
3940
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient;
4041
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository;
4142
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
@@ -48,7 +49,6 @@
4849
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
4950
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
5051
import com.cloudbees.plugins.credentials.common.StandardCredentials;
51-
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
5252
import com.damnhandy.uri.template.UriTemplate;
5353
import com.fasterxml.jackson.databind.util.StdDateFormat;
5454
import edu.umd.cs.findbugs.annotations.CheckForNull;
@@ -1015,14 +1015,13 @@ public SCM build(SCMHead head, SCMRevision revision) {
10151015
// trait will do the magic
10161016
scmCredentialsId = null;
10171017
scmExtension = new GitClientAuthenticatorExtension(null);
1018+
} else if (authenticator instanceof BitbucketUsernamePasswordAuthenticator) {
1019+
scmExtension = new GitClientAuthenticatorExtension(null);
10181020
} else {
1019-
StandardUsernameCredentials scmCredentials = authenticator.getCredentialsForSCM();
10201021
// extension overrides the configured credentialsId with a custom StandardUsernameCredentials provided by the Authenticator
1021-
scmExtension = new GitClientAuthenticatorExtension(scmCredentials);
1022-
if (scmCredentials != null) {
1023-
// will be overridden by git extension
1024-
scmCredentialsId = null;
1025-
}
1022+
scmExtension = new GitClientAuthenticatorExtension(authenticator.getCredentialsForSCM());
1023+
// will be overridden by git extension
1024+
scmCredentialsId = null;
10261025
}
10271026
} else {
10281027
scmExtension = new GitClientAuthenticatorExtension(null);

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketOAuthAuthenticator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.concurrent.ExecutionException;
1616
import jenkins.authentication.tokens.api.AuthenticationTokenException;
1717
import jenkins.util.SetContextClassLoader;
18-
import org.apache.commons.lang.StringUtils;
1918
import org.apache.http.HttpRequest;
2019

2120
public class BitbucketOAuthAuthenticator extends BitbucketAuthenticator {
@@ -54,7 +53,7 @@ public void configureRequest(HttpRequest request) {
5453
public StandardUsernameCredentials getCredentialsForSCM() {
5554
try {
5655
return new UsernamePasswordCredentialsImpl(
57-
CredentialsScope.GLOBAL, getId(), null, StringUtils.EMPTY, token.getAccessToken());
56+
CredentialsScope.GLOBAL, getId(), null, "x-token-auth", token.getAccessToken());
5857
} catch (FormException e) {
5958
throw new RuntimeException(e);
6059
}

0 commit comments

Comments
 (0)