Skip to content

Commit 291d2bf

Browse files
committed
Addresses #29 - Bring the iOS and Android response objects on authorized closer together
1 parent ac06e92 commit 291d2bf

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

Diff for: android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java

+44-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void authorize(
126126
{
127127
try {
128128
final OAuthManagerModule self = this;
129-
HashMap<String,Object> cfg = this.getConfiguration(providerName);
129+
final HashMap<String,Object> cfg = this.getConfiguration(providerName);
130130
final String authVersion = (String) cfg.get("auth_version");
131131
Activity activity = mReactContext.getCurrentActivity();
132132
FragmentManager fragmentManager = activity.getFragmentManager();
@@ -141,14 +141,14 @@ public void onOAuth1AccessToken(final OAuth1AccessToken accessToken) {
141141
_credentialsStore.store(providerName, accessToken);
142142
_credentialsStore.commit();
143143

144-
WritableMap resp = self.accessTokenResponse(providerName, accessToken, authVersion);
144+
WritableMap resp = self.accessTokenResponse(providerName, cfg, accessToken, authVersion);
145145
callback.invoke(null, resp);
146146
}
147147
public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) {
148148
_credentialsStore.store(providerName, accessToken);
149149
_credentialsStore.commit();
150150

151-
WritableMap resp = self.accessTokenResponse(providerName, accessToken, authVersion);
151+
WritableMap resp = self.accessTokenResponse(providerName, cfg, accessToken, authVersion);
152152
callback.invoke(null, resp);
153153
}
154154
};
@@ -364,15 +364,15 @@ public void getSavedAccount(
364364
throw new Exception("No token found");
365365
}
366366

367-
WritableMap resp = this.accessTokenResponse(providerName, token, authVersion);
367+
WritableMap resp = this.accessTokenResponse(providerName, cfg, token, authVersion);
368368
onComplete.invoke(null, resp);
369369
} else if (authVersion.equals("2.0")) {
370370
OAuth2AccessToken token = _credentialsStore.get(providerName, OAuth2AccessToken.class);
371371

372372
if (token == null || token.equals("")) {
373373
throw new Exception("No token found");
374374
}
375-
WritableMap resp = this.accessTokenResponse(providerName, token, authVersion);
375+
WritableMap resp = this.accessTokenResponse(providerName, cfg, token, authVersion);
376376
onComplete.invoke(null, resp);
377377
} else {
378378

@@ -420,19 +420,33 @@ private HashMap<String,Object> getConfiguration(
420420

421421
private WritableMap accessTokenResponse(
422422
final String providerName,
423+
final HashMap<String,Object> cfg,
423424
final OAuth1AccessToken accessToken,
424425
final String oauthVersion
425426
) {
426427
WritableMap resp = Arguments.createMap();
427428
WritableMap response = Arguments.createMap();
428429

430+
Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse());
431+
429432
resp.putString("status", "ok");
433+
resp.putBoolean("authorized", true);
430434
resp.putString("provider", providerName);
431435
response.putString("uuid", accessToken.getParameter("user_id"));
432436

437+
String tokenType = accessToken.getParameter("token_type");
438+
if (tokenType == null) {
439+
tokenType = "Bearer";
440+
}
441+
442+
String consumerKey = (String) cfg.get("consumer_key");
443+
433444
WritableMap credentials = Arguments.createMap();
434-
credentials.putString("oauth_token", accessToken.getToken());
435-
credentials.putString("oauth_secret", accessToken.getTokenSecret());
445+
credentials.putString("accessToken", accessToken.getToken());
446+
credentials.putString("type", tokenType);
447+
// credentials.putString("scope", accessToken.getScope());
448+
credentials.putString("consumerKey", consumerKey);
449+
436450
response.putMap("credentials", credentials);
437451

438452
resp.putMap("response", response);
@@ -442,13 +456,15 @@ private WritableMap accessTokenResponse(
442456

443457
private WritableMap accessTokenResponse(
444458
final String providerName,
459+
final HashMap<String,Object> cfg,
445460
final OAuth2AccessToken accessToken,
446461
final String oauthVersion
447462
) {
448463
WritableMap resp = Arguments.createMap();
449464
WritableMap response = Arguments.createMap();
450465

451466
resp.putString("status", "ok");
467+
resp.putBoolean("authorized", true);
452468
resp.putString("provider", providerName);
453469
try {
454470
response.putString("uuid", accessToken.getParameter("user_id"));
@@ -458,9 +474,27 @@ private WritableMap accessTokenResponse(
458474
}
459475

460476
WritableMap credentials = Arguments.createMap();
461-
credentials.putString("oauth_token", accessToken.getAccessToken());
462-
credentials.putString("oauth_secret", "");
463-
credentials.putString("scope", accessToken.getScope());
477+
Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse());
478+
479+
credentials.putString("accessToken", accessToken.getAccessToken());
480+
String authHeader;
481+
482+
String tokenType = accessToken.getParameter("token_type");
483+
if (tokenType == null) {
484+
tokenType = "Bearer";
485+
}
486+
String scope = accessToken.getScope();
487+
if (scope == null) {
488+
scope = (String) cfg.get("scopes");
489+
}
490+
491+
String clientID = (String) cfg.get("client_id");
492+
493+
authHeader = tokenType + " " + accessToken.getAccessToken();
494+
credentials.putString("authorizationHeader", authHeader);
495+
credentials.putString("type", tokenType);
496+
credentials.putString("scope", scope);
497+
credentials.putString("clientID", clientID);
464498
response.putMap("credentials", credentials);
465499

466500
resp.putMap("response", response);

0 commit comments

Comments
 (0)