@@ -88,9 +88,13 @@ + (BOOL)setupOAuthHandler:(UIApplication *)application
88
88
[authPlatform setURLOpener: ^void (NSURL *URL, DCTAuthPlatformCompletion completion) {
89
89
// [sharedManager setPendingAuthentication:YES];
90
90
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
+ });
94
98
} else {
95
99
[application openURL: URL];
96
100
}
@@ -153,7 +157,7 @@ - (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)c
153
157
_callbackUrls = [arr copy ];
154
158
NSLog (@" Saved callback url: %@ in %@ " , saveCallbackUrl, _callbackUrls);
155
159
}
156
-
160
+
157
161
158
162
// Convert objects of url type
159
163
for (NSString *name in [config allKeys ]) {
@@ -303,7 +307,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
303
307
NSMutableDictionary *cfg = [[manager getConfigForProvider: providerName] mutableCopy ];
304
308
305
309
DCTAuthAccount *existingAccount = [manager accountForProvider: providerName];
306
- NSString *clientID = ((DCTOAuth2Credential *) existingAccount).clientID ;
310
+ NSString *clientID = ([providerName isEqualToString: @" google" ]) ? ((DCTOAuth2Credential *) existingAccount).clientID : (NSString *)nil ;
311
+
307
312
if (([providerName isEqualToString: @" google" ] && existingAccount && clientID != nil )
308
313
|| (![providerName isEqualToString: @" google" ] && existingAccount != nil )) {
309
314
if ([existingAccount isAuthorized ]) {
@@ -354,14 +359,14 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
354
359
355
360
[manager addPending: client];
356
361
_pendingAuthentication = YES ;
357
-
362
+
358
363
NSLog (@" Calling authorizeWithUrl: %@ with callbackURL: %@ \n %@ " , providerName, callbackUrl, cfg);
359
364
360
365
[client authorizeWithUrl: providerName
361
366
url: callbackUrl
362
367
cfg: cfg
363
368
onSuccess: ^(DCTAuthAccount *account) {
364
- NSLog (@" on success called with account: %@ " , account);
369
+ NSLog (@" on success called with account: %@ " , account);
365
370
NSDictionary *accountResponse = [manager getAccountResponse: account cfg: cfg];
366
371
_pendingAuthentication = NO ;
367
372
[manager removePending: client];
@@ -443,16 +448,27 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
443
448
URL: apiUrl
444
449
items: items];
445
450
451
+ // Allow json body in POST / PUT requests
446
452
NSDictionary *body = [opts objectForKey: @" body" ];
447
453
if (body != nil ) {
454
+ NSMutableArray *items = [NSMutableArray array ];
455
+
448
456
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
+ }
451
464
}
465
+
466
+ DCTAuthContent *content = [[DCTAuthContent alloc ] initWithEncoding: NSUTF8StringEncoding
467
+ type: DCTAuthContentTypeJSON
468
+ items: items];
469
+ [request setContent: content];
452
470
}
453
471
454
- request.account = existingAccount;
455
-
456
472
// If there are headers
457
473
NSDictionary *headers = [opts objectForKey: @" headers" ];
458
474
if (headers != nil ) {
@@ -463,6 +479,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
463
479
request.HTTPHeaders = existingHeaders;
464
480
}
465
481
482
+ request.account = existingAccount;
483
+
466
484
[request performRequestWithHandler: ^(DCTAuthResponse *response, NSError *error) {
467
485
if (error != nil ) {
468
486
NSDictionary *errorDict = @{
@@ -473,9 +491,12 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
473
491
} else {
474
492
NSInteger statusCode = response.statusCode ;
475
493
NSData *rawData = response.data ;
494
+ NSDictionary *headers = response.HTTPHeaders ;
476
495
477
496
NSError *err;
478
497
NSArray *data;
498
+
499
+
479
500
480
501
// Check if returned data is a valid JSON
481
502
// != nil returned if the rawdata is not a valid JSON
@@ -487,20 +508,21 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
487
508
488
509
// Parse XML
489
510
data = [XMLReader dictionaryForXMLData: rawData
490
- options: XMLReaderOptionsProcessNamespaces
491
- error: &err];
511
+ options: XMLReaderOptionsProcessNamespaces
512
+ error: &err];
492
513
}
493
-
494
514
if (err != nil ) {
495
515
NSDictionary *errResp = @{
496
516
@" status" : @" error" ,
497
517
@" msg" : [NSString stringWithFormat: @" JSON parsing error: %@ " , [err localizedDescription ]]
498
518
};
499
519
callback (@[errResp]);
500
520
} else {
521
+
501
522
NSDictionary *resp = @{
502
523
@" status" : @(statusCode),
503
- @" data" : data
524
+ @" data" : data != nil ? data : @[],
525
+ @" headers" : headers,
504
526
};
505
527
callback (@[[NSNull null ], resp]);
506
528
}
@@ -527,7 +549,7 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
527
549
}
528
550
529
551
- (NSDictionary *) credentialForAccount : (NSString *)providerName
530
- cfg : (NSDictionary *)cfg
552
+ cfg : (NSDictionary *)cfg
531
553
{
532
554
DCTAuthAccount *account = [self accountForProvider: providerName];
533
555
if (!account) {
@@ -715,3 +737,4 @@ - (NSString *) stringHost:(NSURL *)url
715
737
}
716
738
717
739
@end
740
+
0 commit comments