@@ -360,16 +360,25 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
360
360
[client authorizeWithUrl: providerName
361
361
url: callbackUrl
362
362
cfg: cfg
363
- onSuccess: ^(DCTAuthAccount *account) {
364
- NSLog (@" on success called with account: %@ " , account);
363
+ onSuccess: ^(DCTAuthAccount *account, DCTAuthResponse *response) {
364
+ NSLog (@" on success called with account: %@ " , account);
365
+ NSLog (@" on success called with response: %@ " , response);
365
366
NSDictionary *accountResponse = [manager getAccountResponse: account cfg: cfg];
367
+
368
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData: response.data options: kNilOptions error: nil ];
369
+ NSString *idToken = [json valueForKey: @" id_token" ];
370
+ NSDictionary *authResponse = [OAuthManager decodeWithToken: idToken];
371
+ NSLog (@" auth response data: %@ " , authResponse);
372
+
366
373
_pendingAuthentication = NO ;
367
374
[manager removePending: client];
368
375
[[manager accountStore ] saveAccount: account]; // <~
369
376
370
377
callback (@[[NSNull null ], @{
371
- @" status" : @" ok" ,
372
- @" response" : accountResponse
378
+ @" authResponse" : authResponse,
379
+ @" idToken" : idToken,
380
+ @" response" : accountResponse,
381
+ @" status" : @" ok"
373
382
}]);
374
383
} onError: ^(NSError *error) {
375
384
NSLog (@" Error in authorizeWithUrl: %@ " , error);
@@ -526,6 +535,42 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
526
535
}
527
536
}
528
537
538
+ +(NSData *) base64DecodeWithString : (NSString *) string {
539
+ string = [[string stringByReplacingOccurrencesOfString: @" -" withString: @" +" ]
540
+ stringByReplacingOccurrencesOfString: @" _" withString: @" /" ];
541
+
542
+ int size = [string length ] % 4 ;
543
+ NSMutableString *segment = [[NSMutableString alloc ] initWithString: string];
544
+ for (int i = 0 ; i < size; i++) {
545
+ [segment appendString: @" =" ];
546
+ }
547
+
548
+ return [[NSData alloc ] initWithBase64EncodedString: segment options: 0 ];
549
+ }
550
+
551
+ +(NSDictionary *) decodeWithToken : (NSString *)token {
552
+ NSArray *segments = [token componentsSeparatedByString: @" ." ];
553
+ if ([segments count ] != 3 ) {
554
+ return nil ;
555
+ }
556
+
557
+ // All segments should be base64
558
+ NSString *headerSeg = segments[0 ];
559
+ NSString *payloadSeg = segments[1 ];
560
+
561
+ // Decode and parse header and payload JSON
562
+ NSDictionary *header = [NSJSONSerialization JSONObjectWithData: [self base64DecodeWithString: headerSeg] options: NSJSONReadingMutableLeaves error: nil ];
563
+ if (header == nil ) {
564
+ return nil ;
565
+ }
566
+ NSDictionary *payload = [NSJSONSerialization JSONObjectWithData: [self base64DecodeWithString: payloadSeg] options: NSJSONReadingMutableLeaves error: nil ];
567
+ if (payload == nil ) {
568
+ return nil ;
569
+ }
570
+
571
+ return payload;
572
+ }
573
+
529
574
- (NSDictionary *) credentialForAccount : (NSString *)providerName
530
575
cfg : (NSDictionary *)cfg
531
576
{
0 commit comments