Skip to content

Commit 54407f0

Browse files
committed
add ScopeBuilder to easily specify multiple scopes while requesting OAuth2.0 Access Tokens
1 parent 0b3c0b7 commit 54407f0

File tree

8 files changed

+80
-4
lines changed

8 files changed

+80
-4
lines changed

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[SNAPSHOT]
2+
* add ScopeBuilder to easily specify multiple scopes while requesting OAuth2.0 Access Tokens
3+
14
[8.1.0]
25
* add raw Response (with HTTP response code and body) as member to the OAuth2AccessTokenErrorResponse
36
* add possibility to set "" (empty string) as apiSecret

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<dependency>
5555
<groupId>com.fasterxml.jackson.core</groupId>
5656
<artifactId>jackson-databind</artifactId>
57-
<version>2.12.0</version>
57+
<version>2.12.1</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>junit</groupId>
@@ -104,7 +104,7 @@
104104
<dependency>
105105
<groupId>com.puppycrawl.tools</groupId>
106106
<artifactId>checkstyle</artifactId>
107-
<version>8.38</version>
107+
<version>8.39</version>
108108
</dependency>
109109
</dependencies>
110110
</plugin>
@@ -188,6 +188,7 @@
188188
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
189189
<encoding>UTF-8</encoding>
190190
<additionalOptions>-html5</additionalOptions>
191+
<doclint>all,-missing</doclint>
191192
</configuration>
192193
<executions>
193194
<execution>

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/LinkedIn20Example.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Scanner;
44
import com.github.scribejava.core.builder.ServiceBuilder;
55
import com.github.scribejava.apis.LinkedInApi20;
6+
import com.github.scribejava.core.builder.ScopeBuilder;
67
import com.github.scribejava.core.model.OAuth2AccessToken;
78
import com.github.scribejava.core.model.OAuthRequest;
89
import com.github.scribejava.core.model.Response;
@@ -29,7 +30,7 @@ public static void main(String... args) throws IOException, InterruptedException
2930
final String clientSecret = "your client secret";
3031
final OAuth20Service service = new ServiceBuilder(clientId)
3132
.apiSecret(clientSecret)
32-
.defaultScope("r_liteprofile r_emailaddress") // replace with desired scope
33+
.defaultScope(new ScopeBuilder("r_liteprofile", "r_emailaddress")) // replace with desired scope
3334
.callback("http://example.com/callback")
3435
.build(LinkedInApi20.instance());
3536
final Scanner in = new Scanner(System.in);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.scribejava.core.builder;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/**
9+
* OAuth2.0 Scope Builder. It allows specifying multiple scopes one by one. It will combine them in the single
10+
* space-delimited string. OAuth 2.0 standard specifies space as a delimiter for scopes
11+
* (https://tools.ietf.org/html/rfc6749#section-3.3). If you found API, that do not support spaces, but support
12+
* something else, let ScribeJava know (submit the issue here https://github.com/scribejava/scribejava/issues) and use
13+
* your own concatenated string as a temporary workaround.
14+
*/
15+
public class ScopeBuilder {
16+
17+
private final Set<String> scopes = new HashSet<>();
18+
19+
public ScopeBuilder() {
20+
}
21+
22+
public ScopeBuilder(String scope) {
23+
withScope(scope);
24+
}
25+
26+
public ScopeBuilder(String... scopes) {
27+
withScopes(scopes);
28+
}
29+
30+
public ScopeBuilder(Collection<String> scopes) {
31+
withScopes(scopes);
32+
}
33+
34+
public final ScopeBuilder withScope(String scope) {
35+
scopes.add(scope);
36+
return this;
37+
}
38+
39+
public final ScopeBuilder withScopes(String... scopes) {
40+
this.scopes.addAll(Arrays.asList(scopes));
41+
return this;
42+
}
43+
44+
public final ScopeBuilder withScopes(Collection<String> scopes) {
45+
this.scopes.addAll(scopes);
46+
return this;
47+
}
48+
49+
public String build() {
50+
final StringBuilder scopeBuilder = new StringBuilder();
51+
for (String scope : scopes) {
52+
scopeBuilder.append(' ').append(scope);
53+
}
54+
return scopeBuilder.substring(1);
55+
}
56+
}

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public ServiceBuilderOAuth20 defaultScope(String defaultScope) {
6868
return setScope(defaultScope);
6969
}
7070

71+
@Override
72+
public ServiceBuilderOAuth20 defaultScope(ScopeBuilder scopeBuilder) {
73+
return setScope(scopeBuilder.build());
74+
}
75+
7176
@Override
7277
public ServiceBuilderOAuth10a withScope(String scope) {
7378
return setScope(scope);

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilderOAuth20.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,7 @@ public interface ServiceBuilderOAuth20 extends ServiceBuilderCommon {
5151
*/
5252
ServiceBuilderOAuth20 defaultScope(String defaultScope);
5353

54+
ServiceBuilderOAuth20 defaultScope(ScopeBuilder scopeBuilder);
55+
5456
OAuth20Service build(DefaultApi20 api);
5557
}

scribejava-core/src/main/java/com/github/scribejava/core/oauth/AccessTokenRequestParams.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.github.scribejava.core.oauth;
22

3+
import com.github.scribejava.core.builder.ScopeBuilder;
4+
35
public class AccessTokenRequestParams {
6+
47
private final String code;
58
private String pkceCodeVerifier;
69
private String scope;
@@ -23,6 +26,11 @@ public AccessTokenRequestParams scope(String scope) {
2326
return this;
2427
}
2528

29+
public AccessTokenRequestParams scope(ScopeBuilder scope) {
30+
this.scope = scope.build();
31+
return this;
32+
}
33+
2634
public String getCode() {
2735
return code;
2836
}

scribejava-httpclient-ahc/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<dependency>
2424
<groupId>org.asynchttpclient</groupId>
2525
<artifactId>async-http-client</artifactId>
26-
<version>2.12.1</version>
26+
<version>2.12.2</version>
2727
</dependency>
2828
<dependency>
2929
<groupId>com.github.scribejava</groupId>

0 commit comments

Comments
 (0)