Skip to content

Commit

Permalink
Support OAuth2 tokens too
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerhu committed Aug 19, 2019
1 parent d916ca2 commit f531e52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {

GROUP = 'com.codepath.libraries'
BASE_VERSION = "2.0"
VERSION_NAME = "2.0.1"
VERSION_NAME = "2.1.0"
POM_PACKAGING = "aar"
POM_DESCRIPTION = "CodePath OAuth Handler"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import com.codepath.asynchttpclient.AsyncHttpClient;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth2AccessToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

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

OAuthAsyncHttpClient asyncHttpClient = new OAuthAsyncHttpClient(httpClient);
return asyncHttpClient;
}

public static OAuthAsyncHttpClient create(final OAuth2AccessToken token) {
final String bearer = String.format("Bearer %s", token.getAccessToken());

OkHttpClient httpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.addInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request originalRequest = chain.request();
Request authedRequest = originalRequest.newBuilder().header("Authorization", bearer).build();
return chain.proceed(authedRequest);
}
}).build();

OAuthAsyncHttpClient asyncHttpClient = new OAuthAsyncHttpClient(httpClient);
return asyncHttpClient;
}
}
15 changes: 9 additions & 6 deletions library/src/main/java/com/codepath/oauth/OAuthBaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.SharedPreferences;
import android.net.Uri;

import androidx.annotation.Nullable;

import com.github.scribejava.core.builder.api.BaseApi;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
Expand Down Expand Up @@ -46,11 +48,11 @@ public static OAuthBaseClient getInstance(Class<? extends OAuthBaseClient> klass
return instance;
}

public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, String callbackUrl) {
public OAuthBaseClient(Context c, final BaseApi apiInstance, String consumerUrl, final String consumerKey, final String consumerSecret, @Nullable String scope, String callbackUrl) {
this.baseUrl = consumerUrl;
this.callbackUrl = callbackUrl;
tokenClient = new OAuthTokenClient(apiInstance, consumerKey,
consumerSecret, callbackUrl, new OAuthTokenClient.OAuthTokenHandler() {
consumerSecret, callbackUrl, scope, new OAuthTokenClient.OAuthTokenHandler() {

// Store request token and launch the authorization URL in the browser
@Override
Expand Down Expand Up @@ -86,8 +88,7 @@ public void onReceivedAccessToken(Token accessToken, String oAuthVersion) {
editor.commit();
} else if (oAuthVersion == OAUTH2_VERSION) {
OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) accessToken;

//TODO(rhu) - create client for OAuth2 cases
instantiateClient(consumerKey, consumerSecret, oAuth2AccessToken);
tokenClient.setAccessToken(accessToken);
editor.putString(OAuthConstants.TOKEN, oAuth2AccessToken.getAccessToken());
editor.putString(OAuthConstants.SCOPE, oAuth2AccessToken.getScope());
Expand Down Expand Up @@ -122,8 +123,10 @@ public void instantiateClient(String consumerKey, String consumerSecret, Token t

if (token instanceof OAuth1AccessToken) {
client = OAuthAsyncHttpClient.create(consumerKey, consumerSecret, (OAuth1AccessToken)(token));
} else if (token instanceof OAuth2AccessToken){
client = OAuthAsyncHttpClient.create((OAuth2AccessToken) token);
} else {

throw new IllegalStateException("unrecognized token type" + token);
}

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

} else if (checkAccessToken() != null) { // already have access token
this.accessHandler.onLoginSuccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ public class OAuthTokenClient {

// Requires the apiClass, consumerKey, consumerSecret and callbackUrl along with the TokenHandler
public OAuthTokenClient(BaseApi apiInstance, String consumerKey, String consumerSecret, String callbackUrl,
OAuthTokenHandler handler) {
String scope, OAuthTokenHandler handler) {
this.apiInstance = apiInstance;
this.handler = handler;
if (callbackUrl == null) { callbackUrl = OAuthConstants.OUT_OF_BAND; };
this.service = new ServiceBuilder()
.apiKey(consumerKey)
.apiSecret(consumerSecret).callback(callbackUrl)
.httpClientConfig(OkHttpHttpClientConfig.defaultConfig())
.scope(scope) // OAuth2 requires scope
.build(apiInstance);
}

Expand Down

0 comments on commit f531e52

Please sign in to comment.