@@ -88,9 +88,13 @@ + (BOOL)setupOAuthHandler:(UIApplication *)application
8888 [authPlatform setURLOpener: ^void (NSURL *URL, DCTAuthPlatformCompletion completion) {
8989 // [sharedManager setPendingAuthentication:YES];
9090 if ([SFSafariViewController class ] != nil ) {
91- safariViewController = [[SFSafariViewController alloc ] initWithURL: URL];
92- UIViewController *viewController = application.keyWindow .rootViewController ;
93- [viewController presentViewController: safariViewController animated: YES completion: nil ];
91+ dispatch_async (dispatch_get_main_queue (), ^{
92+ safariViewController = [[SFSafariViewController alloc ] initWithURL: URL];
93+ UIViewController *viewController = application.keyWindow .rootViewController ;
94+ dispatch_async (dispatch_get_main_queue (), ^{
95+ [viewController presentViewController: safariViewController animated: YES completion: nil ];
96+ });
97+ });
9498 } else {
9599 [application openURL: URL];
96100 }
@@ -153,7 +157,7 @@ - (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)c
153157 _callbackUrls = [arr copy ];
154158 NSLog (@" Saved callback url: %@ in %@ " , saveCallbackUrl, _callbackUrls);
155159 }
156-
160+
157161
158162 // Convert objects of url type
159163 for (NSString *name in [config allKeys ]) {
@@ -303,7 +307,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
303307 NSMutableDictionary *cfg = [[manager getConfigForProvider: providerName] mutableCopy ];
304308
305309 DCTAuthAccount *existingAccount = [manager accountForProvider: providerName];
306- NSString *clientID = ((DCTOAuth2Credential *) existingAccount).clientID ;
310+ NSString *clientID = ([providerName isEqualToString: @" google" ]) ? ((DCTOAuth2Credential *) existingAccount).clientID : (NSString *)nil ;
311+
307312 if (([providerName isEqualToString: @" google" ] && existingAccount && clientID != nil )
308313 || (![providerName isEqualToString: @" google" ] && existingAccount != nil )) {
309314 if ([existingAccount isAuthorized ]) {
@@ -354,14 +359,14 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
354359
355360 [manager addPending: client];
356361 _pendingAuthentication = YES ;
357-
362+
358363 NSLog (@" Calling authorizeWithUrl: %@ with callbackURL: %@ \n %@ " , providerName, callbackUrl, cfg);
359364
360365 [client authorizeWithUrl: providerName
361366 url: callbackUrl
362367 cfg: cfg
363368 onSuccess: ^(DCTAuthAccount *account) {
364- NSLog (@" on success called with account: %@ " , account);
369+ NSLog (@" on success called with account: %@ " , account);
365370 NSDictionary *accountResponse = [manager getAccountResponse: account cfg: cfg];
366371 _pendingAuthentication = NO ;
367372 [manager removePending: client];
@@ -443,16 +448,27 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
443448 URL: apiUrl
444449 items: items];
445450
451+ // Allow json body in POST / PUT requests
446452 NSDictionary *body = [opts objectForKey: @" body" ];
447453 if (body != nil ) {
454+ NSMutableArray *items = [NSMutableArray array ];
455+
448456 for (NSString *key in body) {
449- NSData *data = [[NSString stringWithFormat: @" %@ " , [body valueForKey: key]] dataUsingEncoding: NSUTF8StringEncoding];
450- [request addMultiPartData: data withName: key type: @" application/json" ]; // TODO: How should we handle different body types?
457+ NSString *value = [body valueForKey: key];
458+
459+ DCTAuthContentItem *item = [[DCTAuthContentItem alloc ] initWithName: key value: value];
460+
461+ if (item != nil ) {
462+ [items addObject: item];
463+ }
451464 }
465+
466+ DCTAuthContent *content = [[DCTAuthContent alloc ] initWithEncoding: NSUTF8StringEncoding
467+ type: DCTAuthContentTypeJSON
468+ items: items];
469+ [request setContent: content];
452470 }
453471
454- request.account = existingAccount;
455-
456472 // If there are headers
457473 NSDictionary *headers = [opts objectForKey: @" headers" ];
458474 if (headers != nil ) {
@@ -463,6 +479,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
463479 request.HTTPHeaders = existingHeaders;
464480 }
465481
482+ request.account = existingAccount;
483+
466484 [request performRequestWithHandler: ^(DCTAuthResponse *response, NSError *error) {
467485 if (error != nil ) {
468486 NSDictionary *errorDict = @{
@@ -473,9 +491,12 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
473491 } else {
474492 NSInteger statusCode = response.statusCode ;
475493 NSData *rawData = response.data ;
494+ NSDictionary *headers = response.HTTPHeaders ;
476495
477496 NSError *err;
478497 NSArray *data;
498+
499+
479500
480501 // Check if returned data is a valid JSON
481502 // != nil returned if the rawdata is not a valid JSON
@@ -487,20 +508,21 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
487508
488509 // Parse XML
489510 data = [XMLReader dictionaryForXMLData: rawData
490- options: XMLReaderOptionsProcessNamespaces
491- error: &err];
511+ options: XMLReaderOptionsProcessNamespaces
512+ error: &err];
492513 }
493-
494514 if (err != nil ) {
495515 NSDictionary *errResp = @{
496516 @" status" : @" error" ,
497517 @" msg" : [NSString stringWithFormat: @" JSON parsing error: %@ " , [err localizedDescription ]]
498518 };
499519 callback (@[errResp]);
500520 } else {
521+
501522 NSDictionary *resp = @{
502523 @" status" : @(statusCode),
503- @" data" : data
524+ @" data" : data != nil ? data : @[],
525+ @" headers" : headers,
504526 };
505527 callback (@[[NSNull null ], resp]);
506528 }
@@ -527,7 +549,7 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
527549}
528550
529551- (NSDictionary *) credentialForAccount : (NSString *)providerName
530- cfg : (NSDictionary *)cfg
552+ cfg : (NSDictionary *)cfg
531553{
532554 DCTAuthAccount *account = [self accountForProvider: providerName];
533555 if (!account) {
@@ -715,3 +737,4 @@ - (NSString *) stringHost:(NSURL *)url
715737}
716738
717739@end
740+
0 commit comments