Skip to content

Commit 6e4a1c4

Browse files
Merge fullstackreact/react-native-oauth master branch into shalin-jasani/fullstackreact/react-native-oauth master branch (#1)
* Added fullstackreact#171 and my NSString *clientID fix, which might not be as good as https://github.com/fullstackreact/react-native-oauth/blob/2f8c8d1483526bbc8a6ca72183c6d11a71538ad3/ios/OAuthManager/OAuthManager.m * Added fullstackreact#171 and my NSString *clientID fix, which might not be as good as https://github.com/fullstackreact/react-native-oauth/blob/2f8c8d1483526bbc8a6ca72183c6d11a71538ad3/ios/OAuthManager/OAuthManager.m * Added fullstackreact#171 and my NSString *clientID fix, which might not be as good as https://github.com/fullstackreact/react-native-oauth/blob/2f8c8d1483526bbc8a6ca72183c6d11a71538ad3/ios/OAuthManager/OAuthManager.m * Merge PR 121, fix user agent, fix full screen webview * Changed if/else statement to avoid React error. * Fix duplicate RCTMethodInfo import (facebook/react-native#15775) (zoontek/react-native-permissions#137) * Fix duplicate React library import error conflict w/certain pods * Pass back response headers over javascript bridge * Dispatch safariViewController on main queue The safariViewController dispatch was occuring on another thread. This sometimes caused app crashes when the view was presented, in particular if the keyboard had been presented via a TextInput or other component. The resulting crash complained about _cachedSystemAnimationFence and the main thread. This has been with other React Native apps that load a viewController. Dispatching to present the viewController on the main thread fixes this issue. * Fix build issue * Remove deprecated @OverRide * Fix: Duplicate RCTMethodInfo while building iOS app * Fix error for redefinition of RCTMethodInfo * Ignored dist/ * 2.1.16 * 2.1.17 * 2.1.18
1 parent ad01a3e commit 6e4a1c4

10 files changed

+861
-38
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["env"],
3+
"plugins": ["transform-async-to-generator", "transform-object-rest-spread"]
4+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ xcuserdata/
1717
.idea
1818
.vscode
1919
javac-services.0.log*
20+
dist/

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
109109
mWebView.setVisibility(View.VISIBLE);
110110
mWebView.getSettings().setJavaScriptEnabled(true);
111111
mWebView.getSettings().setDomStorageEnabled(true);
112-
mWebView.getSettings().setUserAgentString("Mozilla/5.0 Google");
113112

114113

115114
LayoutParams layoutParams = this.getFullscreenLayoutParams(context);
@@ -172,7 +171,7 @@ private LayoutParams getFullscreenLayoutParams(Context context) {
172171
realHeight = display.getHeight();
173172
}
174173

175-
return new LayoutParams(realWidth, realHeight-convertDpToPixel(50f,context));
174+
return new LayoutParams(realWidth, realHeight);
176175
}
177176

178177

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

+29-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88

99
import com.google.gson.Gson;
10+
import com.google.gson.JsonSyntaxException;
1011

1112
import com.facebook.react.bridge.Arguments;
1213
import com.facebook.react.bridge.Callback;
@@ -400,18 +401,36 @@ private WritableMap accessTokenResponse(
400401
) {
401402
WritableMap resp = Arguments.createMap();
402403
WritableMap response = Arguments.createMap();
403-
Map accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class);
404404

405405
Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse());
406-
406+
407+
/* Some things return as JSON, some as x-www-form-urlencoded (querystring) */
408+
409+
Map accessTokenMap = null;
410+
try {
411+
accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class);
412+
} catch (JsonSyntaxException e) {
413+
/*
414+
failed to parse as JSON, so turn it into a HashMap which looks like the one we'd
415+
get back from the JSON parser, so the rest of the code continues unchanged.
416+
*/
417+
Log.d(TAG, "Credential looks like a querystring; parsing as such");
418+
accessTokenMap = new HashMap();
419+
accessTokenMap.put("user_id", accessToken.getParameter("user_id"));
420+
accessTokenMap.put("oauth_token_secret", accessToken.getParameter("oauth_token_secret"));
421+
accessTokenMap.put("token_type", accessToken.getParameter("token_type"));
422+
}
423+
424+
407425
resp.putString("status", "ok");
408426
resp.putBoolean("authorized", true);
409427
resp.putString("provider", providerName);
410-
String uuid = (String) accessTokenMap.get("user_id");
428+
429+
String uuid = accessToken.getParameter("user_id");
411430
response.putString("uuid", uuid);
412-
String oauthTokenSecret = (String) accessTokenMap.get("oauth_token_secret");
431+
String oauthTokenSecret = (String) accessToken.getParameter("oauth_token_secret");
413432

414-
String tokenType = (String) accessTokenMap.get("token_type");
433+
String tokenType = (String) accessToken.getParameter("token_type");
415434
if (tokenType == null) {
416435
tokenType = "Bearer";
417436
}
@@ -422,7 +441,6 @@ private WritableMap accessTokenResponse(
422441
credentials.putString("access_token", accessToken.getToken());
423442
credentials.putString("access_token_secret", oauthTokenSecret);
424443
credentials.putString("type", tokenType);
425-
// credentials.putString("scope", accessToken.getScope());
426444
credentials.putString("consumerKey", consumerKey);
427445

428446
response.putMap("credentials", credentials);
@@ -440,26 +458,21 @@ private WritableMap accessTokenResponse(
440458
) {
441459
WritableMap resp = Arguments.createMap();
442460
WritableMap response = Arguments.createMap();
443-
Map accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class);
444461

445462
resp.putString("status", "ok");
446463
resp.putBoolean("authorized", true);
447464
resp.putString("provider", providerName);
448-
try {
449-
String uuid = (String) accessTokenMap.get("user_id");
450-
response.putString("uuid", uuid);
451-
} catch (Exception ex) {
452-
Log.e(TAG, "Exception while getting the access token");
453-
ex.printStackTrace();
454-
}
465+
466+
String uuid = accessToken.getParameter("user_id");
467+
response.putString("uuid", uuid);
455468

456469
WritableMap credentials = Arguments.createMap();
457470
Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse());
458471

459472
credentials.putString("accessToken", accessToken.getAccessToken());
460473
String authHeader;
461474

462-
String tokenType = (String) accessTokenMap.get("token_type");
475+
String tokenType = accessToken.getTokenType();
463476
if (tokenType == null) {
464477
tokenType = "Bearer";
465478
}
@@ -470,7 +483,7 @@ private WritableMap accessTokenResponse(
470483
}
471484

472485
String clientID = (String) cfg.get("client_id");
473-
String idToken = (String) accessTokenMap.get("id_token");
486+
String idToken = accessToken.getParameter("id_token");
474487

475488
authHeader = tokenType + " " + accessToken.getAccessToken();
476489
credentials.putString("authorizationHeader", authHeader);

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

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public OAuthManagerPackage() {
2222
* @param reactContext react application context that can be used to create modules
2323
* @return list of native modules to register with the newly created catalyst instance
2424
*/
25-
@Override
2625
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
2726
List<NativeModule> modules = new ArrayList<>();
2827
modules.add(new OAuthManagerModule(reactContext));
@@ -45,7 +44,6 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
4544
* @param reactContext
4645
* @return a list of view managers that should be registered with {@link UIManagerModule}
4746
*/
48-
@Override
4947
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
5048
return Collections.emptyList();
5149
}

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,11 @@ private static ServiceBuilder _oauth2ServiceBuilder(
268268
builder.scope(scopeStr);
269269
}
270270

271-
boolean rawScopes = (cfg.containsKey("rawScopes") && ((String)cfg.get("rawScopes")).equalsIgnoreCase("true"));
272-
273271
if (opts != null && opts.hasKey("scopes")) {
274272
scopes = (String) opts.getString("scopes");
275-
String scopeStr = null;
276-
277-
if (!rawScopes)
278-
scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
279-
else
280-
scopeStr = scopes;
281-
273+
String scopeStr = OAuthManagerProviders.getScopeString(scopes, ",");
282274
builder.scope(scopeStr);
283-
}
275+
}
284276

285277
if (callbackUrl != null) {
286278
builder.callback(callbackUrl);

ios/OAuthManager/OAuthManager.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#import "RCTBridgeModule.h"
1414
#endif
1515

16-
#if __has_include("RCTLinkingManager.h")
17-
#import "RCTLinkingManager.h"
18-
#else
16+
#if __has_include(<React/RCTLinkingManager.h>)
1917
#import <React/RCTLinkingManager.h>
18+
#else
19+
#import "RCTLinkingManager.h"
2020
#endif
2121

2222

ios/OAuthManager/OAuthManager.m

+11-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ + (BOOL)setupOAuthHandler:(UIApplication *)application
9191
dispatch_async(dispatch_get_main_queue(), ^{
9292
safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
9393
UIViewController *viewController = application.keyWindow.rootViewController;
94-
[viewController presentViewController:safariViewController animated:YES completion: nil];
94+
dispatch_async(dispatch_get_main_queue(), ^{
95+
[viewController presentViewController:safariViewController animated:YES completion: nil];
96+
});
9597
});
9698
} else {
9799
[application openURL:URL];
@@ -305,7 +307,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
305307
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];
306308

307309
DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
308-
NSString *clientID = ((DCTOAuth2Credential *) existingAccount).clientID;
310+
NSString *clientID = ([providerName isEqualToString:@"google"]) ? ((DCTOAuth2Credential *) existingAccount).clientID : (NSString *)nil;
311+
309312
if (([providerName isEqualToString:@"google"] && existingAccount && clientID != nil)
310313
|| (![providerName isEqualToString:@"google"] && existingAccount != nil)) {
311314
if ([existingAccount isAuthorized]) {
@@ -488,9 +491,12 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
488491
} else {
489492
NSInteger statusCode = response.statusCode;
490493
NSData *rawData = response.data;
494+
NSDictionary *headers = response.HTTPHeaders;
491495

492496
NSError *err;
493497
NSArray *data;
498+
499+
494500

495501
// Check if returned data is a valid JSON
496502
// != nil returned if the rawdata is not a valid JSON
@@ -512,9 +518,11 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
512518
};
513519
callback(@[errResp]);
514520
} else {
521+
515522
NSDictionary *resp = @{
516523
@"status": @(statusCode),
517-
@"data": data != nil ? data : @[]
524+
@"data": data != nil ? data : @[],
525+
@"headers": headers,
518526
};
519527
callback(@[[NSNull null], resp]);
520528
}

0 commit comments

Comments
 (0)