Skip to content

Commit edf430c

Browse files
authored
Update OAuth2 scribe dependency to the new scribejava (#900)
1 parent cb6984f commit edf430c

7 files changed

+47
-96
lines changed

pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,9 @@
145145
<artifactId>plain-credentials</artifactId>
146146
</dependency>
147147
<dependency>
148-
<groupId>org.scribe</groupId>
149-
<artifactId>scribe</artifactId>
150-
<version>1.3.3</version>
151-
<exclusions>
152-
<exclusion>
153-
<groupId>commons-codec</groupId>
154-
<artifactId>commons-codec</artifactId>
155-
</exclusion>
156-
</exclusions>
148+
<groupId>com.github.scribejava</groupId>
149+
<artifactId>scribejava-httpclient-apache</artifactId>
150+
<version>8.3.3</version>
157151
</dependency>
158152
</dependencies>
159153

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public BitbucketAccessTokenAuthenticator convert(@NonNull StringCredentials cred
3939
* @return whether this can authenticate given the context
4040
*/
4141
@Override
42-
public boolean isFit(AuthenticationTokenContext ctx) {
42+
protected boolean isFit(AuthenticationTokenContext<? super BitbucketAccessTokenAuthenticator> ctx) {
4343
return ctx.mustHave(BitbucketAuthenticator.SCHEME, "https")
4444
&& (ctx.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE, BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_SERVER)
4545
|| ctx.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE, BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_CLOUD));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public BitbucketClientCertificateAuthenticator convert(@NonNull StandardCertific
6363
* @return whether this can authenticate given the context
6464
*/
6565
@Override
66-
public boolean isFit(AuthenticationTokenContext ctx) {
66+
protected boolean isFit(AuthenticationTokenContext<? super BitbucketClientCertificateAuthenticator> ctx) {
6767
return ctx.mustHave(BitbucketAuthenticator.SCHEME, "https")
6868
&& ctx.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE, BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_SERVER);
6969
}
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
22

3-
import org.scribe.builder.api.DefaultApi20;
4-
import org.scribe.extractors.AccessTokenExtractor;
5-
import org.scribe.extractors.JsonTokenExtractor;
6-
import org.scribe.model.OAuthConfig;
7-
import org.scribe.model.Verb;
8-
import org.scribe.oauth.OAuthService;
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
94

105
public class BitbucketOAuth extends DefaultApi20 {
11-
public static final String OAUTH_ENDPOINT = "https://bitbucket.org/site/oauth2/";
6+
private static final String OAUTH_ENDPOINT = "https://bitbucket.org/site/oauth2/";
127

13-
@Override
14-
public String getAccessTokenEndpoint() {
15-
return OAUTH_ENDPOINT + "access_token";
8+
protected BitbucketOAuth() {
169
}
1710

18-
@Override
19-
public String getAuthorizationUrl(OAuthConfig config) {
20-
return OAUTH_ENDPOINT + "authorize";
11+
private static class InstanceHolder {
12+
private static final BitbucketOAuth INSTANCE = new BitbucketOAuth();
2113
}
2214

23-
@Override
24-
public Verb getAccessTokenVerb() {
25-
return Verb.POST;
15+
public static BitbucketOAuth instance() {
16+
return InstanceHolder.INSTANCE;
2617
}
2718

2819
@Override
29-
public AccessTokenExtractor getAccessTokenExtractor() {
30-
return new JsonTokenExtractor();
20+
public String getAccessTokenEndpoint() {
21+
return OAUTH_ENDPOINT + "access_token";
3122
}
3223

3324
@Override
34-
public OAuthService createService(OAuthConfig config) {
35-
return new BitbucketOAuthService(this, config);
25+
protected String getAuthorizationBaseUrl() {
26+
return OAUTH_ENDPOINT + "authorize";
3627
}
28+
3729
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,60 @@
55
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
66
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
77
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
8+
import com.github.scribejava.core.builder.ServiceBuilder;
9+
import com.github.scribejava.core.httpclient.HttpClient;
10+
import com.github.scribejava.core.model.OAuth2AccessToken;
11+
import com.github.scribejava.core.model.OAuthConstants;
12+
import com.github.scribejava.core.oauth.OAuth20Service;
13+
import com.github.scribejava.httpclient.apache.ApacheHttpClientConfig;
14+
import com.github.scribejava.httpclient.apache.ApacheProvider;
815
import hudson.model.Descriptor.FormException;
16+
import java.io.IOException;
17+
import java.util.concurrent.ExecutionException;
18+
import jenkins.authentication.tokens.api.AuthenticationTokenException;
919
import org.apache.commons.lang.StringUtils;
1020
import org.apache.http.HttpRequest;
11-
import org.scribe.model.OAuthConfig;
12-
import org.scribe.model.OAuthConstants;
13-
import org.scribe.model.Token;
1421

1522
public class BitbucketOAuthAuthenticator extends BitbucketAuthenticator {
1623

17-
private Token token;
24+
private OAuth2AccessToken token;
1825

1926
/**
2027
* Constructor.
2128
*
2229
* @param credentials the key/pass that will be used
30+
* @throws AuthenticationTokenException
2331
*/
24-
public BitbucketOAuthAuthenticator(StandardUsernamePasswordCredentials credentials) {
32+
public BitbucketOAuthAuthenticator(StandardUsernamePasswordCredentials credentials) throws AuthenticationTokenException {
2533
super(credentials);
2634

27-
OAuthConfig config = new OAuthConfig(credentials.getUsername(), credentials.getPassword().getPlainText());
35+
HttpClient httpClient = new ApacheProvider().createClient(ApacheHttpClientConfig.defaultConfig());
36+
OAuth20Service service = new ServiceBuilder(credentials.getUsername())
37+
.apiSecret(credentials.getPassword().getPlainText())
38+
.httpClient(httpClient)
39+
// .httpClientConfig(ApacheHttpClientConfig.defaultConfig()) the ServiceLoader does not work well with Jenkins plugin classloader
40+
.build(BitbucketOAuth.instance());
2841

29-
BitbucketOAuthService OAuthService = (BitbucketOAuthService) new BitbucketOAuth().createService(config);
30-
31-
token = OAuthService.getAccessToken(OAuthConstants.EMPTY_TOKEN, null);
42+
try {
43+
token = service.getAccessTokenClientCredentialsGrant();
44+
} catch (IOException | InterruptedException | ExecutionException e) {
45+
throw new AuthenticationTokenException(e);
46+
}
3247
}
3348

3449
/**
3550
* Set up request with token in header
3651
*/
3752
@Override
3853
public void configureRequest(HttpRequest request) {
39-
request.addHeader(OAuthConstants.HEADER, "Bearer " + this.token.getToken());
54+
request.addHeader(OAuthConstants.HEADER, "Bearer " + this.token.getAccessToken());
4055
}
4156

4257
@Override
4358
public StandardUsernameCredentials getCredentialsForSCM() {
4459
try {
4560
return new UsernamePasswordCredentialsImpl(
46-
CredentialsScope.GLOBAL, getId(), null, StringUtils.EMPTY, token.getToken());
61+
CredentialsScope.GLOBAL, getId(), null, StringUtils.EMPTY, token.getAccessToken());
4762
} catch (FormException e) {
4863
throw new RuntimeException(e);
4964
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import edu.umd.cs.findbugs.annotations.NonNull;
77
import hudson.Extension;
88
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
9+
import jenkins.authentication.tokens.api.AuthenticationTokenException;
910
import jenkins.authentication.tokens.api.AuthenticationTokenSource;
1011

1112

@@ -27,11 +28,12 @@ public BitbucketOAuthAuthenticatorSource() {
2728
*
2829
* @param standardUsernamePasswordCredentials the username/password combo
2930
* @return an authenticator that will use them.
31+
* @throws AuthenticationTokenException if the specific credentials could not be converted.
3032
*/
3133
@NonNull
3234
@Override
3335
public BitbucketOAuthAuthenticator convert(
34-
@NonNull StandardUsernamePasswordCredentials standardUsernamePasswordCredentials) {
36+
@NonNull StandardUsernamePasswordCredentials standardUsernamePasswordCredentials) throws AuthenticationTokenException {
3537
return new BitbucketOAuthAuthenticator(standardUsernamePasswordCredentials);
3638
}
3739

@@ -43,7 +45,7 @@ public BitbucketOAuthAuthenticator convert(
4345
* @return whether this can authenticate given the context
4446
*/
4547
@Override
46-
public boolean isFit(AuthenticationTokenContext ctx) {
48+
protected boolean isFit(AuthenticationTokenContext<? super BitbucketOAuthAuthenticator> ctx) {
4749
return ctx.mustHave(BitbucketAuthenticator.SCHEME, "https") && ctx.mustHave(
4850
BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE, BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_CLOUD);
4951
}

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

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)