Skip to content

Commit a3ff12d

Browse files
committed
add response_type parameter to the ServiceBuilder/OAuthConfig to use not only "code" for authorization code
1 parent 75779ef commit a3ff12d

7 files changed

Lines changed: 33 additions & 21 deletions

File tree

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* mark Facebook doesn't support refresh token by throwing UnsupportedOperationException
44
* make JSON Access Token Extractor be the default for OAuth 2.0 (according to RFC 6749)
55
* drop Google OAuth 1.0 support (OAuth 1.0 was officially deprecated by Google)
6+
* add response_type parameter to the ServiceBuilder/OAuthConfig to use not only "code" for authorization code
67

78
[2.3.0]
89
* Stack Exchange authentication via OAuth 2.0 (stackoverflow.com, askubuntu.com, etc.).

scribejava-apis/src/main/java/com/github/scribejava/apis/GoogleApi20.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class GoogleApi20 extends DefaultApi20 {
1515

1616
private static final String AUTHORIZE_URL
17-
= "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%s&redirect_uri=%s&scope=%s";
17+
= "https://accounts.google.com/o/oauth2/auth?response_type=%s&client_id=%s&redirect_uri=%s&scope=%s";
1818

1919
protected GoogleApi20() {
2020
}
@@ -39,8 +39,8 @@ public String getAccessTokenEndpoint() {
3939

4040
@Override
4141
public String getAuthorizationUrl(OAuthConfig config) {
42-
final StringBuilder sb = new StringBuilder(String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(
43-
config.getCallback()), OAuthEncoder.encode(config.getScope())));
42+
final StringBuilder sb = new StringBuilder(String.format(AUTHORIZE_URL, config.getResponseType(),
43+
config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope())));
4444

4545
final String state = config.getState();
4646
if (state != null) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ abstract class AbstractServiceBuilder<T extends AbstractServiceBuilder<T>> {
2020
private SignatureType signatureType;
2121
private OutputStream debugStream;
2222
private String grantType;
23+
private String responseType = "code";
2324

2425
AbstractServiceBuilder() {
2526
this.callback = OAuthConstants.OUT_OF_BAND;
@@ -118,6 +119,13 @@ public T grantType(String grantType) {
118119
return (T) this;
119120
}
120121

122+
@SuppressWarnings("unchecked")
123+
public T responseType(String responseType) {
124+
Preconditions.checkEmptyString(responseType, "Invalid OAuth responseType");
125+
this.responseType = responseType;
126+
return (T) this;
127+
}
128+
121129
@SuppressWarnings("unchecked")
122130
public T debug() {
123131
debugStream(System.out);
@@ -161,6 +169,10 @@ public String getGrantType() {
161169
return grantType;
162170
}
163171

172+
public String getResponseType() {
173+
return responseType;
174+
}
175+
164176
protected abstract OAuthConfig createConfig();
165177

166178
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public ServiceBuilder readTimeout(Integer readTimeout) {
2828
protected OAuthConfig createConfig() {
2929
super.checkPreconditions();
3030
final OAuthConfig config = new OAuthConfig(getApiKey(), getApiSecret(), getCallback(), getSignatureType(),
31-
getScope(), getDebugStream(), connectTimeout, readTimeout, getGrantType());
32-
config.setState(getState());
31+
getScope(), getDebugStream(), connectTimeout, readTimeout, getGrantType(), getState(),
32+
getResponseType());
3333
return config;
3434
}
3535
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public void checkPreconditions() {
2525
protected OAuthConfigAsync createConfig() {
2626
checkPreconditions();
2727
final OAuthConfigAsync configAsync = new OAuthConfigAsync(getApiKey(), getApiSecret(), getCallback(),
28-
getSignatureType(), getScope(), getGrantType(), getDebugStream(), asyncHttpClientConfig);
29-
configAsync.setState(getState());
28+
getSignatureType(), getScope(), getGrantType(), getState(), getResponseType(), getDebugStream(),
29+
asyncHttpClientConfig);
3030
configAsync.setAsyncHttpProviderClassName(asyncHttpProviderClassName);
3131
return configAsync;
3232
}

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConfig.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ public class OAuthConfig {
1717
private final OutputStream debugStream;
1818
private final Integer connectTimeout;
1919
private final Integer readTimeout;
20-
private String state;
20+
private final String state;
21+
private final String responseType;
2122

2223
public OAuthConfig(String key, String secret) {
23-
this(key, secret, null, null, null, null, null, null, null);
24+
this(key, secret, null, null, null, null, null, null, null, null, null);
2425
}
2526

2627
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope,
27-
OutputStream stream, Integer connectTimeout, Integer readTimeout, String grantType) {
28+
OutputStream stream, Integer connectTimeout, Integer readTimeout, String grantType, String state,
29+
String responseType) {
2830
this.apiKey = key;
2931
this.apiSecret = secret;
3032
this.callback = callback;
@@ -34,6 +36,8 @@ public OAuthConfig(String key, String secret, String callback, SignatureType typ
3436
this.connectTimeout = connectTimeout;
3537
this.readTimeout = readTimeout;
3638
this.grantType = grantType;
39+
this.state = state;
40+
this.responseType = responseType;
3741
}
3842

3943
public String getApiKey() {
@@ -87,17 +91,11 @@ public void log(String message) {
8791
}
8892
}
8993

90-
/**
91-
* Sets optional value used by some provider implementations that is exchanged with provider to avoid CSRF attacks.
92-
*
93-
* @param state some secret key that client side shall never receive
94-
*/
95-
public void setState(String state) {
96-
this.state = state;
97-
}
98-
9994
public String getState() {
10095
return state;
10196
}
10297

98+
public String getResponseType() {
99+
return responseType;
100+
}
103101
}

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConfigAsync.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public OAuthConfigAsync(String key, String secret) {
1313
}
1414

1515
public OAuthConfigAsync(String key, String secret, String callback, SignatureType type, String scope,
16-
String grantType, OutputStream stream, AsyncHttpClientConfig asyncHttpClientConfig) {
17-
super(key, secret, callback, type, scope, stream, null, null, grantType);
16+
String grantType, String state, String responseType, OutputStream stream,
17+
AsyncHttpClientConfig asyncHttpClientConfig) {
18+
super(key, secret, callback, type, scope, stream, null, null, grantType, state, responseType);
1819
this.asyncHttpClientConfig = asyncHttpClientConfig;
1920
}
2021

0 commit comments

Comments
 (0)