Skip to content

Commit 9de2267

Browse files
authored
Merge pull request fullstackreact#111 from vbuch/master
Add ConfigurableApi to support custom providers for Android
2 parents 93bc4be + c87965a commit 9de2267

File tree

3 files changed

+118
-21
lines changed

3 files changed

+118
-21
lines changed

android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java

+50-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.github.scribejava.apis.GoogleApi20;
2929
import com.github.scribejava.apis.GitHubApi;
3030

31+
import com.github.scribejava.apis.ConfigurableApi;
3132
import com.github.scribejava.apis.SlackApi;
3233

3334
import com.facebook.react.bridge.ReadableMap;
@@ -59,15 +60,25 @@ static public OAuth20Service getApiFor20Provider(
5960
) {
6061
if (providerName.equalsIgnoreCase("facebook")) {
6162
return OAuthManagerProviders.facebookService(params, opts, callbackUrl);
62-
} else if (providerName.equalsIgnoreCase("google")) {
63+
}
64+
65+
if (providerName.equalsIgnoreCase("google")) {
6366
return OAuthManagerProviders.googleService(params, opts, callbackUrl);
64-
} else if (providerName.equalsIgnoreCase("github")) {
67+
}
68+
69+
if (providerName.equalsIgnoreCase("github")) {
6570
return OAuthManagerProviders.githubService(params, opts, callbackUrl);
66-
} else if (providerName.equalsIgnoreCase("slack")) {
71+
}
72+
73+
if (providerName.equalsIgnoreCase("slack")) {
6774
return OAuthManagerProviders.slackService(params, opts, callbackUrl);
68-
} else {
69-
return null;
7075
}
76+
77+
if (params.containsKey("access_token_url") && params.containsKey("authorize_url")) {
78+
return OAuthManagerProviders.configurableService(params, opts, callbackUrl);
79+
}
80+
81+
return null;
7182
}
7283

7384
static public OAuthRequest getRequestForProvider(
@@ -78,9 +89,9 @@ static public OAuthRequest getRequestForProvider(
7889
final HashMap<String,Object> cfg,
7990
@Nullable final ReadableMap params
8091
) {
81-
final OAuth10aService service =
92+
final OAuth10aService service =
8293
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null);
83-
94+
8495
String token = oa1token.getToken();
8596
OAuthConfig config = service.getConfig();
8697
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
@@ -100,14 +111,14 @@ static public OAuthRequest getRequestForProvider(
100111
) {
101112
final OAuth20Service service =
102113
OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null, null);
103-
114+
104115
OAuthConfig config = service.getConfig();
105116
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
106117
String token = oa2token.getAccessToken();
107118

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

110-
//
121+
//
111122
Log.d(TAG, "Making request for " + providerName + " to add token " + token);
112123
// Need a way to standardize this, but for now
113124
if (providerName.equalsIgnoreCase("slack")) {
@@ -146,12 +157,12 @@ static private OAuthRequest addParametersToRequest(
146157
}
147158

148159
private static OAuth10aService twitterService(
149-
final HashMap cfg,
160+
final HashMap cfg,
150161
@Nullable final ReadableMap opts,
151162
final String callbackUrl) {
152163
String consumerKey = (String) cfg.get("consumer_key");
153164
String consumerSecret = (String) cfg.get("consumer_secret");
154-
165+
155166
ServiceBuilder builder = new ServiceBuilder()
156167
.apiKey(consumerKey)
157168
.apiSecret(consumerSecret)
@@ -167,20 +178,20 @@ private static OAuth10aService twitterService(
167178
if (callbackUrl != null) {
168179
builder.callback(callbackUrl);
169180
}
170-
181+
171182
return builder.build(TwitterApi.instance());
172183
}
173184

174185
private static OAuth20Service facebookService(
175-
final HashMap cfg,
186+
final HashMap cfg,
176187
@Nullable final ReadableMap opts,
177188
final String callbackUrl) {
178189
ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl);
179190
return builder.build(FacebookApi.instance());
180191
}
181192

182193
private static OAuth20Service googleService(
183-
final HashMap cfg,
194+
final HashMap cfg,
184195
@Nullable final ReadableMap opts,
185196
final String callbackUrl)
186197
{
@@ -189,7 +200,7 @@ private static OAuth20Service googleService(
189200
}
190201

191202
private static OAuth20Service githubService(
192-
final HashMap cfg,
203+
final HashMap cfg,
193204
@Nullable final ReadableMap opts,
194205
final String callbackUrl)
195206
{
@@ -198,8 +209,28 @@ private static OAuth20Service githubService(
198209
return builder.build(GitHubApi.instance());
199210
}
200211

212+
private static OAuth20Service configurableService(
213+
final HashMap cfg,
214+
@Nullable final ReadableMap opts,
215+
final String callbackUrl
216+
) {
217+
ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl);
218+
Log.d(TAG, "Creating ConfigurableApi");
219+
//Log.d(TAG, " authorize_url: " + cfg.get("authorize_url"));
220+
//Log.d(TAG, " access_token_url: " + cfg.get("access_token_url"));
221+
ConfigurableApi api = ConfigurableApi.instance()
222+
.setAccessTokenEndpoint((String) cfg.get("access_token_url"))
223+
.setAuthorizationBaseUrl((String) cfg.get("authorize_url"));
224+
if (cfg.containsKey("access_token_verb")) {
225+
//Log.d(TAG, " access_token_verb: " + cfg.get("access_token_verb"));
226+
api.setAccessTokenVerb((String) cfg.get("access_token_verb"));
227+
}
228+
229+
return builder.build(api);
230+
}
231+
201232
private static OAuth20Service slackService(
202-
final HashMap cfg,
233+
final HashMap cfg,
203234
@Nullable final ReadableMap opts,
204235
final String callbackUrl
205236
) {
@@ -236,13 +267,13 @@ private static ServiceBuilder _oauth2ServiceBuilder(
236267
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
237268
builder.scope(scopeStr);
238269
}
239-
270+
240271
if (opts != null && opts.hasKey("scopes")) {
241272
scopes = (String) opts.getString("scopes");
242273
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
243274
builder.scope(scopeStr);
244275
}
245-
276+
246277
if (callbackUrl != null) {
247278
builder.callback(callbackUrl);
248279
}
@@ -261,4 +292,4 @@ private static String getScopeString(
261292
Log.d(TAG, "array: " + array + " (" + array.size() + ") from " + scopes);
262293
return TextUtils.join(joinBy, array);
263294
}
264-
}
295+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.github.scribejava.apis;
2+
3+
import android.util.Log;
4+
5+
import com.github.scribejava.core.builder.api.DefaultApi20;
6+
import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor;
7+
import com.github.scribejava.core.extractors.TokenExtractor;
8+
import com.github.scribejava.core.model.OAuth2AccessToken;
9+
import com.github.scribejava.core.model.Verb;
10+
11+
public class ConfigurableApi extends DefaultApi20 {
12+
13+
private String accessTokenEndpoint;
14+
15+
private String authorizationBaseUrl;
16+
17+
private Verb accessTokenVerb = Verb.GET;
18+
19+
protected ConfigurableApi() {
20+
}
21+
22+
private static class InstanceHolder {
23+
private static final ConfigurableApi INSTANCE = new ConfigurableApi();
24+
}
25+
26+
public static ConfigurableApi instance() {
27+
return InstanceHolder.INSTANCE;
28+
}
29+
30+
public ConfigurableApi setAccessTokenEndpoint(String endpoint) {
31+
accessTokenEndpoint = endpoint;
32+
return this;
33+
}
34+
35+
public ConfigurableApi setAuthorizationBaseUrl(String baseUrl) {
36+
authorizationBaseUrl = baseUrl;
37+
return this;
38+
}
39+
40+
public ConfigurableApi setAccessTokenVerb(String verb) {
41+
if (verb.equalsIgnoreCase("GET")) {
42+
accessTokenVerb = Verb.GET;
43+
} else if (verb.equalsIgnoreCase("POST")) {
44+
accessTokenVerb = Verb.POST;
45+
} else {
46+
Log.e("ConfigurableApi", "Expected GET or POST string values for accessTokenVerb.");
47+
}
48+
49+
return this;
50+
}
51+
52+
@Override
53+
public Verb getAccessTokenVerb() {
54+
return accessTokenVerb;
55+
}
56+
57+
@Override
58+
public String getAccessTokenEndpoint() {
59+
return accessTokenEndpoint;
60+
}
61+
62+
@Override
63+
protected String getAuthorizationBaseUrl() {
64+
return authorizationBaseUrl;
65+
}
66+
}

bin/cocoapods.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## https://github.com/auth0/react-native-lock/blob/master/bin/cocoapods.sh
44

55
ios_dir=`pwd`/ios
6-
if [ -d ios_dir ]
6+
if [ ! -d $ios_dir ]
77
then
88
exit 0
99
fi
@@ -45,4 +45,4 @@ cd ..
4545

4646
echo "Installing Pods"
4747

48-
pod install --project-directory=ios
48+
pod install --project-directory=ios

0 commit comments

Comments
 (0)