Skip to content

Commit

Permalink
Add OAuth token to http clone link (#810)
Browse files Browse the repository at this point in the history
Problem was introduced in #796. Fixes #808.
  • Loading branch information
andrey-fomin committed Feb 15, 2024
1 parent 0ed2592 commit 659ab70
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
Expand Down Expand Up @@ -536,7 +537,7 @@ public BitbucketRepositoryType getRepositoryType() throws IOException, Interrupt
repositoryType = BitbucketRepositoryType.fromString(r.getScm());
Map<String, List<BitbucketHref>> links = r.getLinks();
if (links != null && links.containsKey("clone")) {
primaryCloneLinks = links.get("clone");
setPrimaryCloneLinks(links.get("clone"));
}
}
return repositoryType;
Expand Down Expand Up @@ -1037,6 +1038,15 @@ public SCM build(SCMHead head, SCMRevision revision) {
}
}

private void setPrimaryCloneLinks(List<BitbucketHref> links) {
BitbucketAuthenticator authenticator = authenticator();
if (authenticator == null) {

Check warning on line 1043 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1043 is only partially covered, one branch is missing
primaryCloneLinks = links;
} else {
primaryCloneLinks = links.stream().map(authenticator::addAuthToken).collect(Collectors.toList());

Check warning on line 1046 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1046 is not covered by tests
}
}

@NonNull
@Override
public SCMRevision getTrustedRevision(@NonNull SCMRevision revision, @NonNull TaskListener listener)
Expand Down Expand Up @@ -1092,7 +1102,7 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
BitbucketRepository r = bitbucket.getRepository();
Map<String, List<BitbucketHref>> links = r.getLinks();
if (links != null && links.containsKey("clone")) {
primaryCloneLinks = links.get("clone");
setPrimaryCloneLinks(links.get("clone"));
}
result.add(new BitbucketRepoMetadataAction(r));
String defaultBranch = bitbucket.getDefaultBranch();
Expand Down Expand Up @@ -1288,7 +1298,7 @@ private List<BitbucketHref> getCloneLinksFromMirror(

private void initPrimaryCloneLinks(BitbucketApi bitbucket) {
try {
primaryCloneLinks = getCloneLinksFromPrimary(bitbucket);
setPrimaryCloneLinks(getCloneLinksFromPrimary(bitbucket));
} catch (Exception e) {
throw new IllegalStateException(
"Could not determine clone links of " + getRepoOwner() + "/" + getRepository()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ public void configureRequest(HttpRequest request) {
}

/**
* Return the user to be used in the clone Uri. Override this if your
* authentication method needs to set the user in the repository Uri
* Add authentication token to clone link if
* authentication method requires it
*
* @return user name to use in the repository Uri
* @return updated clone link
*/
public String getUserUri() {
// override to return a user
return "";
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
return bitbucketHref;

Check warning on line 117 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 117 is not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpRequest;
import org.scribe.model.OAuthConfig;
import org.scribe.model.OAuthConstants;
Expand Down Expand Up @@ -35,8 +38,27 @@ public void configureRequest(HttpRequest request) {
}

@Override
public String getUserUri() {
return "x-token-auth:{" + token.getToken() + "}";
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
String link = bitbucketHref.getHref();
if (!link.startsWith("http")) {
return bitbucketHref;
}
try {
URI uri = new URI(link);
String userInfo = "x-token-auth:{" + token.getToken() + "}";
String newLink = new URI(
uri.getScheme(),
userInfo,
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment()
).toString();
return new BitbucketHref(bitbucketHref.getName(), newLink);
} catch (URISyntaxException e) {
throw new RuntimeException(e);

Check warning on line 60 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketOAuthAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 42-60 are not covered by tests
}
}

}

0 comments on commit 659ab70

Please sign in to comment.