Skip to content

Commit 0641727

Browse files
author
Valery Buchinsky
committed
Get ConfigurableApi in;
Also allow configuring the access_token_verb; refs fullstackreact#109, fullstackreact#102, fullstackreact#64
1 parent f9ee811 commit 0641727

File tree

2 files changed

+95
-24
lines changed

2 files changed

+95
-24
lines changed

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

+29-24
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,23 @@ static public OAuth20Service getApiFor20Provider(
6161
if (providerName.equalsIgnoreCase("facebook")) {
6262
return OAuthManagerProviders.facebookService(params, opts, callbackUrl);
6363
}
64-
64+
6565
if (providerName.equalsIgnoreCase("google")) {
6666
return OAuthManagerProviders.googleService(params, opts, callbackUrl);
6767
}
68-
68+
6969
if (providerName.equalsIgnoreCase("github")) {
7070
return OAuthManagerProviders.githubService(params, opts, callbackUrl);
7171
}
72-
72+
7373
if (providerName.equalsIgnoreCase("slack")) {
7474
return OAuthManagerProviders.slackService(params, opts, callbackUrl);
75-
} else {
76-
return null;
7775
}
78-
76+
7977
if (params.containsKey("access_token_url") && params.containsKey("authorize_url")) {
8078
return OAuthManagerProviders.configurableService(params, opts, callbackUrl);
8179
}
82-
80+
8381
return null;
8482
}
8583

@@ -91,9 +89,9 @@ static public OAuthRequest getRequestForProvider(
9189
final HashMap<String,Object> cfg,
9290
@Nullable final ReadableMap params
9391
) {
94-
final OAuth10aService service =
92+
final OAuth10aService service =
9593
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null);
96-
94+
9795
String token = oa1token.getToken();
9896
OAuthConfig config = service.getConfig();
9997
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
@@ -113,14 +111,14 @@ static public OAuthRequest getRequestForProvider(
113111
) {
114112
final OAuth20Service service =
115113
OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null, null);
116-
114+
117115
OAuthConfig config = service.getConfig();
118116
OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config);
119117
String token = oa2token.getAccessToken();
120118

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

123-
//
121+
//
124122
Log.d(TAG, "Making request for " + providerName + " to add token " + token);
125123
// Need a way to standardize this, but for now
126124
if (providerName.equalsIgnoreCase("slack")) {
@@ -159,12 +157,12 @@ static private OAuthRequest addParametersToRequest(
159157
}
160158

161159
private static OAuth10aService twitterService(
162-
final HashMap cfg,
160+
final HashMap cfg,
163161
@Nullable final ReadableMap opts,
164162
final String callbackUrl) {
165163
String consumerKey = (String) cfg.get("consumer_key");
166164
String consumerSecret = (String) cfg.get("consumer_secret");
167-
165+
168166
ServiceBuilder builder = new ServiceBuilder()
169167
.apiKey(consumerKey)
170168
.apiSecret(consumerSecret)
@@ -180,20 +178,20 @@ private static OAuth10aService twitterService(
180178
if (callbackUrl != null) {
181179
builder.callback(callbackUrl);
182180
}
183-
181+
184182
return builder.build(TwitterApi.instance());
185183
}
186184

187185
private static OAuth20Service facebookService(
188-
final HashMap cfg,
186+
final HashMap cfg,
189187
@Nullable final ReadableMap opts,
190188
final String callbackUrl) {
191189
ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl);
192190
return builder.build(FacebookApi.instance());
193191
}
194192

195193
private static OAuth20Service googleService(
196-
final HashMap cfg,
194+
final HashMap cfg,
197195
@Nullable final ReadableMap opts,
198196
final String callbackUrl)
199197
{
@@ -202,7 +200,7 @@ private static OAuth20Service googleService(
202200
}
203201

204202
private static OAuth20Service githubService(
205-
final HashMap cfg,
203+
final HashMap cfg,
206204
@Nullable final ReadableMap opts,
207205
final String callbackUrl)
208206
{
@@ -212,20 +210,27 @@ private static OAuth20Service githubService(
212210
}
213211

214212
private static OAuth20Service configurableService(
215-
final HashMap cfg,
213+
final HashMap cfg,
216214
@Nullable final ReadableMap opts,
217215
final String callbackUrl
218216
) {
219217
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"));
220221
ConfigurableApi api = ConfigurableApi.instance()
221222
.setAccessTokenEndpoint((String) cfg.get("access_token_url"))
222223
.setAuthorizationBaseUrl((String) cfg.get("authorize_url"));
223-
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+
224229
return builder.build(api);
225230
}
226-
231+
227232
private static OAuth20Service slackService(
228-
final HashMap cfg,
233+
final HashMap cfg,
229234
@Nullable final ReadableMap opts,
230235
final String callbackUrl
231236
) {
@@ -262,13 +267,13 @@ private static ServiceBuilder _oauth2ServiceBuilder(
262267
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
263268
builder.scope(scopeStr);
264269
}
265-
270+
266271
if (opts != null && opts.hasKey("scopes")) {
267272
scopes = (String) opts.getString("scopes");
268273
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
269274
builder.scope(scopeStr);
270275
}
271-
276+
272277
if (callbackUrl != null) {
273278
builder.callback(callbackUrl);
274279
}
@@ -287,4 +292,4 @@ private static String getScopeString(
287292
Log.d(TAG, "array: " + array + " (" + array.size() + ") from " + scopes);
288293
return TextUtils.join(joinBy, array);
289294
}
290-
}
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+
}

0 commit comments

Comments
 (0)