Skip to content

Commit f531e52

Browse files
committedAug 19, 2019
Support OAuth2 tokens too
1 parent d916ca2 commit f531e52

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed
 

‎library/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ext {
88

99
GROUP = 'com.codepath.libraries'
1010
BASE_VERSION = "2.0"
11-
VERSION_NAME = "2.0.1"
11+
VERSION_NAME = "2.1.0"
1212
POM_PACKAGING = "aar"
1313
POM_DESCRIPTION = "CodePath OAuth Handler"
1414

‎library/src/main/java/com/codepath/oauth/OAuthAsyncHttpClient.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
import com.codepath.asynchttpclient.AsyncHttpClient;
44
import com.facebook.stetho.okhttp3.StethoInterceptor;
55
import com.github.scribejava.core.model.OAuth1AccessToken;
6+
import com.github.scribejava.core.model.OAuth2AccessToken;
67

8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.io.IOException;
11+
12+
import okhttp3.Interceptor;
713
import okhttp3.OkHttpClient;
14+
import okhttp3.Request;
15+
import okhttp3.Response;
816
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
917
import se.akerfeldt.okhttp.signpost.SigningInterceptor;
1018

@@ -17,10 +25,30 @@ protected OAuthAsyncHttpClient(OkHttpClient httpClient) {
1725
public static OAuthAsyncHttpClient create(String consumerKey, String consumerSecret, OAuth1AccessToken token) {
1826
OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(consumerKey, consumerSecret);
1927
consumer.setTokenWithSecret(token.getToken(), token.getTokenSecret());
20-
OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).addInterceptor(new SigningInterceptor(consumer)).build();
28+
OkHttpClient httpClient = new OkHttpClient.Builder()
29+
.addNetworkInterceptor(new StethoInterceptor())
30+
.addInterceptor(new SigningInterceptor(consumer)).build();
2131

2232
OAuthAsyncHttpClient asyncHttpClient = new OAuthAsyncHttpClient(httpClient);
2333
return asyncHttpClient;
2434
}
2535

36+
public static OAuthAsyncHttpClient create(final OAuth2AccessToken token) {
37+
final String bearer = String.format("Bearer %s", token.getAccessToken());
38+
39+
OkHttpClient httpClient = new OkHttpClient.Builder()
40+
.addNetworkInterceptor(new StethoInterceptor())
41+
.addInterceptor(new Interceptor() {
42+
@NotNull
43+
@Override
44+
public Response intercept(@NotNull Chain chain) throws IOException {
45+
Request originalRequest = chain.request();
46+
Request authedRequest = originalRequest.newBuilder().header("Authorization", bearer).build();
47+
return chain.proceed(authedRequest);
48+
}
49+
}).build();
50+
51+
OAuthAsyncHttpClient asyncHttpClient = new OAuthAsyncHttpClient(httpClient);
52+
return asyncHttpClient;
53+
}
2654
}

‎library/src/main/java/com/codepath/oauth/OAuthBaseClient.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.content.SharedPreferences;
66
import android.net.Uri;
77

8+
import androidx.annotation.Nullable;
9+
810
import com.github.scribejava.core.builder.api.BaseApi;
911
import com.github.scribejava.core.model.OAuth1AccessToken;
1012
import com.github.scribejava.core.model.OAuth1RequestToken;
@@ -46,11 +48,11 @@ public static OAuthBaseClient getInstance(Class<? extends OAuthBaseClient> klass
4648
return instance;
4749
}
4850

49-
public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, String callbackUrl) {
51+
public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, @Nullable String scope, String callbackUrl) {
5052
this.baseUrl = consumerUrl;
5153
this.callbackUrl = callbackUrl;
5254
tokenClient = new OAuthTokenClient(apiInstance, consumerKey,
53-
consumerSecret, callbackUrl, new OAuthTokenClient.OAuthTokenHandler() {
55+
consumerSecret, callbackUrl, scope, new OAuthTokenClient.OAuthTokenHandler() {
5456

5557
// Store request token and launch the authorization URL in the browser
5658
@Override
@@ -86,8 +88,7 @@ public void onReceivedAccessToken(Token accessToken, String oAuthVersion) {
8688
editor.commit();
8789
} else if (oAuthVersion == OAUTH2_VERSION) {
8890
OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) accessToken;
89-
90-
//TODO(rhu) - create client for OAuth2 cases
91+
instantiateClient(consumerKey, consumerSecret, oAuth2AccessToken);
9192
tokenClient.setAccessToken(accessToken);
9293
editor.putString(OAuthConstants.TOKEN, oAuth2AccessToken.getAccessToken());
9394
editor.putString(OAuthConstants.SCOPE, oAuth2AccessToken.getScope());
@@ -122,8 +123,10 @@ public void instantiateClient(String consumerKey, String consumerSecret, Token t
122123

123124
if (token instanceof OAuth1AccessToken) {
124125
client = OAuthAsyncHttpClient.create(consumerKey, consumerSecret, (OAuth1AccessToken)(token));
126+
} else if (token instanceof OAuth2AccessToken){
127+
client = OAuthAsyncHttpClient.create((OAuth2AccessToken) token);
125128
} else {
126-
129+
throw new IllegalStateException("unrecognized token type" + token);
127130
}
128131

129132
}
@@ -138,7 +141,7 @@ public void authorize(Uri uri, OAuthAccessHandler handler) {
138141
this.accessHandler = handler;
139142
if (checkAccessToken() == null && uri != null) {
140143
// TODO: check UriServiceCallback with intent:// scheme
141-
tokenClient.fetchAccessToken(getOAuth1RequestToken(), uri);
144+
tokenClient.fetchAccessToken(checkAccessToken(), uri);
142145

143146
} else if (checkAccessToken() != null) { // already have access token
144147
this.accessHandler.onLoginSuccess();

‎library/src/main/java/com/codepath/oauth/OAuthTokenClient.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ public class OAuthTokenClient {
3030

3131
// Requires the apiClass, consumerKey, consumerSecret and callbackUrl along with the TokenHandler
3232
public OAuthTokenClient(BaseApi apiInstance, String consumerKey, String consumerSecret, String callbackUrl,
33-
OAuthTokenHandler handler) {
33+
String scope, OAuthTokenHandler handler) {
3434
this.apiInstance = apiInstance;
3535
this.handler = handler;
3636
if (callbackUrl == null) { callbackUrl = OAuthConstants.OUT_OF_BAND; };
3737
this.service = new ServiceBuilder()
3838
.apiKey(consumerKey)
3939
.apiSecret(consumerSecret).callback(callbackUrl)
4040
.httpClientConfig(OkHttpHttpClientConfig.defaultConfig())
41+
.scope(scope) // OAuth2 requires scope
4142
.build(apiInstance);
4243
}
4344

0 commit comments

Comments
 (0)
Please sign in to comment.