Skip to content

Add ConfigurableApi to support custom providers for Android #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.scribejava.apis.GoogleApi20;
import com.github.scribejava.apis.GitHubApi;

import com.github.scribejava.apis.ConfigurableApi;
import com.github.scribejava.apis.SlackApi;

import com.facebook.react.bridge.ReadableMap;
Expand Down Expand Up @@ -59,15 +60,25 @@ static public OAuth20Service getApiFor20Provider(
) {
if (providerName.equalsIgnoreCase("facebook")) {
return OAuthManagerProviders.facebookService(params, opts, callbackUrl);
} else if (providerName.equalsIgnoreCase("google")) {
}

if (providerName.equalsIgnoreCase("google")) {
return OAuthManagerProviders.googleService(params, opts, callbackUrl);
} else if (providerName.equalsIgnoreCase("github")) {
}

if (providerName.equalsIgnoreCase("github")) {
return OAuthManagerProviders.githubService(params, opts, callbackUrl);
} else if (providerName.equalsIgnoreCase("slack")) {
}

if (providerName.equalsIgnoreCase("slack")) {
return OAuthManagerProviders.slackService(params, opts, callbackUrl);
} else {
return null;
}

if (params.containsKey("access_token_url") && params.containsKey("authorize_url")) {
return OAuthManagerProviders.configurableService(params, opts, callbackUrl);
}

return null;
}

static public OAuthRequest getRequestForProvider(
Expand All @@ -78,9 +89,9 @@ static public OAuthRequest getRequestForProvider(
final HashMap<String,Object> cfg,
@Nullable final ReadableMap params
) {
final OAuth10aService service =
final OAuth10aService service =
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null);

String token = oa1token.getToken();
OAuthConfig config = service.getConfig();
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
Expand All @@ -100,14 +111,14 @@ static public OAuthRequest getRequestForProvider(
) {
final OAuth20Service service =
OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null, null);

OAuthConfig config = service.getConfig();
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
String token = oa2token.getAccessToken();

request = OAuthManagerProviders.addParametersToRequest(request, token, params);

//
//
Log.d(TAG, "Making request for " + providerName + " to add token " + token);
// Need a way to standardize this, but for now
if (providerName.equalsIgnoreCase("slack")) {
Expand Down Expand Up @@ -146,12 +157,12 @@ static private OAuthRequest addParametersToRequest(
}

private static OAuth10aService twitterService(
final HashMap cfg,
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl) {
String consumerKey = (String) cfg.get("consumer_key");
String consumerSecret = (String) cfg.get("consumer_secret");

ServiceBuilder builder = new ServiceBuilder()
.apiKey(consumerKey)
.apiSecret(consumerSecret)
Expand All @@ -167,20 +178,20 @@ private static OAuth10aService twitterService(
if (callbackUrl != null) {
builder.callback(callbackUrl);
}

return builder.build(TwitterApi.instance());
}

private static OAuth20Service facebookService(
final HashMap cfg,
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl) {
ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl);
return builder.build(FacebookApi.instance());
}

private static OAuth20Service googleService(
final HashMap cfg,
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl)
{
Expand All @@ -189,7 +200,7 @@ private static OAuth20Service googleService(
}

private static OAuth20Service githubService(
final HashMap cfg,
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl)
{
Expand All @@ -198,8 +209,28 @@ private static OAuth20Service githubService(
return builder.build(GitHubApi.instance());
}

private static OAuth20Service configurableService(
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl
) {
ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl);
Log.d(TAG, "Creating ConfigurableApi");
//Log.d(TAG, " authorize_url: " + cfg.get("authorize_url"));
//Log.d(TAG, " access_token_url: " + cfg.get("access_token_url"));
ConfigurableApi api = ConfigurableApi.instance()
.setAccessTokenEndpoint((String) cfg.get("access_token_url"))
.setAuthorizationBaseUrl((String) cfg.get("authorize_url"));
if (cfg.containsKey("access_token_verb")) {
//Log.d(TAG, " access_token_verb: " + cfg.get("access_token_verb"));
api.setAccessTokenVerb((String) cfg.get("access_token_verb"));
}

return builder.build(api);
}

private static OAuth20Service slackService(
final HashMap cfg,
final HashMap cfg,
@Nullable final ReadableMap opts,
final String callbackUrl
) {
Expand Down Expand Up @@ -236,13 +267,13 @@ private static ServiceBuilder _oauth2ServiceBuilder(
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
builder.scope(scopeStr);
}

if (opts != null && opts.hasKey("scopes")) {
scopes = (String) opts.getString("scopes");
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
builder.scope(scopeStr);
}

if (callbackUrl != null) {
builder.callback(callbackUrl);
}
Expand All @@ -261,4 +292,4 @@ private static String getScopeString(
Log.d(TAG, "array: " + array + " (" + array.size() + ") from " + scopes);
return TextUtils.join(joinBy, array);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.scribejava.apis;

import android.util.Log;

import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.Verb;

public class ConfigurableApi extends DefaultApi20 {

private String accessTokenEndpoint;

private String authorizationBaseUrl;

private Verb accessTokenVerb = Verb.GET;

protected ConfigurableApi() {
}

private static class InstanceHolder {
private static final ConfigurableApi INSTANCE = new ConfigurableApi();
}

public static ConfigurableApi instance() {
return InstanceHolder.INSTANCE;
}

public ConfigurableApi setAccessTokenEndpoint(String endpoint) {
accessTokenEndpoint = endpoint;
return this;
}

public ConfigurableApi setAuthorizationBaseUrl(String baseUrl) {
authorizationBaseUrl = baseUrl;
return this;
}

public ConfigurableApi setAccessTokenVerb(String verb) {
if (verb.equalsIgnoreCase("GET")) {
accessTokenVerb = Verb.GET;
} else if (verb.equalsIgnoreCase("POST")) {
accessTokenVerb = Verb.POST;
} else {
Log.e("ConfigurableApi", "Expected GET or POST string values for accessTokenVerb.");
}

return this;
}

@Override
public Verb getAccessTokenVerb() {
return accessTokenVerb;
}

@Override
public String getAccessTokenEndpoint() {
return accessTokenEndpoint;
}

@Override
protected String getAuthorizationBaseUrl() {
return authorizationBaseUrl;
}
}
4 changes: 2 additions & 2 deletions bin/cocoapods.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## https://github.com/auth0/react-native-lock/blob/master/bin/cocoapods.sh

ios_dir=`pwd`/ios
if [ -d ios_dir ]
if [ ! -d $ios_dir ]
then
exit 0
fi
Expand Down Expand Up @@ -45,4 +45,4 @@ cd ..

echo "Installing Pods"

pod install --project-directory=ios
pod install --project-directory=ios