Skip to content

Commit 659ab70

Browse files
authored
Add OAuth token to http clone link (#810)
Problem was introduced in #796. Fixes #808.
1 parent 0ed2592 commit 659ab70

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.concurrent.ConcurrentHashMap;
8787
import java.util.logging.Level;
8888
import java.util.logging.Logger;
89+
import java.util.stream.Collectors;
8990
import jenkins.authentication.tokens.api.AuthenticationTokens;
9091
import jenkins.model.Jenkins;
9192
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
@@ -536,7 +537,7 @@ public BitbucketRepositoryType getRepositoryType() throws IOException, Interrupt
536537
repositoryType = BitbucketRepositoryType.fromString(r.getScm());
537538
Map<String, List<BitbucketHref>> links = r.getLinks();
538539
if (links != null && links.containsKey("clone")) {
539-
primaryCloneLinks = links.get("clone");
540+
setPrimaryCloneLinks(links.get("clone"));
540541
}
541542
}
542543
return repositoryType;
@@ -1037,6 +1038,15 @@ public SCM build(SCMHead head, SCMRevision revision) {
10371038
}
10381039
}
10391040

1041+
private void setPrimaryCloneLinks(List<BitbucketHref> links) {
1042+
BitbucketAuthenticator authenticator = authenticator();
1043+
if (authenticator == null) {
1044+
primaryCloneLinks = links;
1045+
} else {
1046+
primaryCloneLinks = links.stream().map(authenticator::addAuthToken).collect(Collectors.toList());
1047+
}
1048+
}
1049+
10401050
@NonNull
10411051
@Override
10421052
public SCMRevision getTrustedRevision(@NonNull SCMRevision revision, @NonNull TaskListener listener)
@@ -1092,7 +1102,7 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
10921102
BitbucketRepository r = bitbucket.getRepository();
10931103
Map<String, List<BitbucketHref>> links = r.getLinks();
10941104
if (links != null && links.containsKey("clone")) {
1095-
primaryCloneLinks = links.get("clone");
1105+
setPrimaryCloneLinks(links.get("clone"));
10961106
}
10971107
result.add(new BitbucketRepoMetadataAction(r));
10981108
String defaultBranch = bitbucket.getDefaultBranch();
@@ -1288,7 +1298,7 @@ private List<BitbucketHref> getCloneLinksFromMirror(
12881298

12891299
private void initPrimaryCloneLinks(BitbucketApi bitbucket) {
12901300
try {
1291-
primaryCloneLinks = getCloneLinksFromPrimary(bitbucket);
1301+
setPrimaryCloneLinks(getCloneLinksFromPrimary(bitbucket));
12921302
} catch (Exception e) {
12931303
throw new IllegalStateException(
12941304
"Could not determine clone links of " + getRepoOwner() + "/" + getRepository()

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketAuthenticator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,13 @@ public void configureRequest(HttpRequest request) {
108108
}
109109

110110
/**
111-
* Return the user to be used in the clone Uri. Override this if your
112-
* authentication method needs to set the user in the repository Uri
111+
* Add authentication token to clone link if
112+
* authentication method requires it
113113
*
114-
* @return user name to use in the repository Uri
114+
* @return updated clone link
115115
*/
116-
public String getUserUri() {
117-
// override to return a user
118-
return "";
116+
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
117+
return bitbucketHref;
119118
}
120119

121120
/**

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
22

33
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
4+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
45
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
58
import org.apache.http.HttpRequest;
69
import org.scribe.model.OAuthConfig;
710
import org.scribe.model.OAuthConstants;
@@ -35,8 +38,27 @@ public void configureRequest(HttpRequest request) {
3538
}
3639

3740
@Override
38-
public String getUserUri() {
39-
return "x-token-auth:{" + token.getToken() + "}";
41+
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
42+
String link = bitbucketHref.getHref();
43+
if (!link.startsWith("http")) {
44+
return bitbucketHref;
45+
}
46+
try {
47+
URI uri = new URI(link);
48+
String userInfo = "x-token-auth:{" + token.getToken() + "}";
49+
String newLink = new URI(
50+
uri.getScheme(),
51+
userInfo,
52+
uri.getHost(),
53+
uri.getPort(),
54+
uri.getPath(),
55+
uri.getQuery(),
56+
uri.getFragment()
57+
).toString();
58+
return new BitbucketHref(bitbucketHref.getName(), newLink);
59+
} catch (URISyntaxException e) {
60+
throw new RuntimeException(e);
61+
}
4062
}
4163

4264
}

0 commit comments

Comments
 (0)