From a0dd695128cfc555300a13f91dd2683a5fcf11e3 Mon Sep 17 00:00:00 2001 From: Gabriel Villenave Date: Mon, 12 Jun 2017 15:37:44 -0700 Subject: [PATCH 1/3] Fix scope string format According to the OAuth spec, the scopes have to be in a space separated string. Fixing this resolves failing Google login. --- .../main/java/io/fullstack/oauth/OAuthManagerProviders.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java index 86c64c3..05c3479 100644 --- a/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java @@ -264,13 +264,13 @@ private static ServiceBuilder _oauth2ServiceBuilder( String scopes = ""; if (cfg.containsKey("scopes")) { scopes = (String) cfg.get("scopes"); - String scopeStr = OAuthManagerProviders.getScopeString(scopes, ","); + String scopeStr = OAuthManagerProviders.getScopeString(scopes, " "); builder.scope(scopeStr); } if (opts != null && opts.hasKey("scopes")) { scopes = (String) opts.getString("scopes"); - String scopeStr = OAuthManagerProviders.getScopeString(scopes, ","); + String scopeStr = OAuthManagerProviders.getScopeString(scopes, " "); builder.scope(scopeStr); } @@ -288,7 +288,7 @@ private static String getScopeString( final String scopes, final String joinBy ) { - List array = Arrays.asList(scopes.replaceAll("\\s", "").split("[ ,]+")); + List array = Arrays.asList(scopes.replaceAll("\\s", "").split(",")); Log.d(TAG, "array: " + array + " (" + array.size() + ") from " + scopes); return TextUtils.join(joinBy, array); } From db7ae5e1776ff8fcc770963df570fa93fbcb5a8d Mon Sep 17 00:00:00 2001 From: Gabriel Villenave Date: Mon, 12 Jun 2017 15:56:18 -0700 Subject: [PATCH 2/3] Fix OAuth token parsing and add token secret The underlying ScribeJava library already does the parsing of the OAuth tokens, so just use that instead of relying on JSON parsing (especially when the response is not JSON!). Also add the access token secret in the response since it's needed for Twitter. --- .../fullstack/oauth/OAuthManagerModule.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java index 16f818a..272a910 100644 --- a/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java @@ -6,8 +6,6 @@ import android.support.annotation.Nullable; import android.util.Log; -import com.google.gson.Gson; - import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; @@ -400,17 +398,17 @@ private WritableMap accessTokenResponse( ) { WritableMap resp = Arguments.createMap(); WritableMap response = Arguments.createMap(); - Map accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class); Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse()); resp.putString("status", "ok"); resp.putBoolean("authorized", true); resp.putString("provider", providerName); - String uuid = (String) accessTokenMap.get("user_id"); + + String uuid = accessToken.getParameter("user_id"); response.putString("uuid", uuid); - String tokenType = (String) accessTokenMap.get("token_type"); + String tokenType = (String) accessToken.getParameter("token_type"); if (tokenType == null) { tokenType = "Bearer"; } @@ -419,8 +417,8 @@ private WritableMap accessTokenResponse( WritableMap credentials = Arguments.createMap(); credentials.putString("accessToken", accessToken.getToken()); + credentials.putString("accessTokenSecret", accessToken.getTokenSecret()); credentials.putString("type", tokenType); - // credentials.putString("scope", accessToken.getScope()); credentials.putString("consumerKey", consumerKey); response.putMap("credentials", credentials); @@ -438,18 +436,13 @@ private WritableMap accessTokenResponse( ) { WritableMap resp = Arguments.createMap(); WritableMap response = Arguments.createMap(); - Map accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class); resp.putString("status", "ok"); resp.putBoolean("authorized", true); resp.putString("provider", providerName); - try { - String uuid = (String) accessTokenMap.get("user_id"); - response.putString("uuid", uuid); - } catch (Exception ex) { - Log.e(TAG, "Exception while getting the access token"); - ex.printStackTrace(); - } + + String uuid = accessToken.getParameter("user_id"); + response.putString("uuid", uuid); WritableMap credentials = Arguments.createMap(); Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse()); @@ -457,7 +450,7 @@ private WritableMap accessTokenResponse( credentials.putString("accessToken", accessToken.getAccessToken()); String authHeader; - String tokenType = (String) accessTokenMap.get("token_type"); + String tokenType = accessToken.getTokenType(); if (tokenType == null) { tokenType = "Bearer"; } @@ -468,7 +461,7 @@ private WritableMap accessTokenResponse( } String clientID = (String) cfg.get("client_id"); - String idToken = (String) accessTokenMap.get("id_token"); + String idToken = accessToken.getParameter("id_token"); authHeader = tokenType + " " + accessToken.getAccessToken(); credentials.putString("authorizationHeader", authHeader); From 8b3f8623dea0e87ce10afc302f27aa7f02f07355 Mon Sep 17 00:00:00 2001 From: Gabriel Villenave Date: Mon, 12 Jun 2017 17:56:22 -0700 Subject: [PATCH 3/3] Fix for OAuth 2 scopes Scopes should be space separated, not comma --- ios/OAuthManager/OAuth2Client.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/OAuthManager/OAuth2Client.m b/ios/OAuthManager/OAuth2Client.m index 092a255..3e3f84e 100644 --- a/ios/OAuthManager/OAuth2Client.m +++ b/ios/OAuthManager/OAuth2Client.m @@ -95,7 +95,7 @@ - (DCTOAuth2Account *) getAccount:(NSString *)providerName NSString *scopeStr = [cfg valueForKey:@"scopes"]; // NSArray *scopes = [scopeStr componentsSeparatedByString:@","]; - NSString *sep = @", "; + NSString *sep = @" "; NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep]; NSArray *scopes = [scopeStr componentsSeparatedByCharactersInSet:set];