Skip to content

Commit 01d607c

Browse files
committed
✨ authorize call returns auth response with JWT token
1 parent 42c09a2 commit 01d607c

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

ios/OAuthManager/OAuth1Client.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ - (void) authorizeWithUrl:(NSString *)providerName
4848
return;
4949
}
5050

51-
onSuccess(account);
51+
onSuccess(account, responses[1]);
5252
}];
5353
return;
5454
}

ios/OAuthManager/OAuth2Client.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ - (void) authorizeWithUrl:(NSString *)providerName
5454
}
5555

5656
NSLog(@"Success!: %@", account);
57-
onSuccess(account);
57+
58+
onSuccess(account, responses[1]);
5859
}];
5960
}
6061

@@ -80,7 +81,7 @@ - (void) reauthenticateWithHandler:(NSString *) providerName
8081
return;
8182
}
8283

83-
onSuccess(account);
84+
onSuccess(account, response);
8485
}];
8586
}
8687

ios/OAuthManager/OAuthClient.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ - (void) clearPendingAccount
7070
return;
7171
}
7272

73-
onSuccess(account);
73+
onSuccess(account, response);
7474
};
7575
}
7676

ios/OAuthManager/OAuthManager.m

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,25 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
360360
[client authorizeWithUrl:providerName
361361
url:callbackUrl
362362
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);
365366
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+
366373
_pendingAuthentication = NO;
367374
[manager removePending:client];
368375
[[manager accountStore] saveAccount:account]; // <~
369376

370377
callback(@[[NSNull null], @{
371-
@"status": @"ok",
372-
@"response": accountResponse
378+
@"authResponse": authResponse,
379+
@"idToken": idToken,
380+
@"response": accountResponse,
381+
@"status": @"ok"
373382
}]);
374383
} onError:^(NSError *error) {
375384
NSLog(@"Error in authorizeWithUrl: %@", error);
@@ -526,6 +535,42 @@ - (DCTAuthAccount *) accountForProvider:(NSString *) providerName
526535
}
527536
}
528537

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+
529574
- (NSDictionary *) credentialForAccount:(NSString *)providerName
530575
cfg:(NSDictionary *)cfg
531576
{

ios/OAuthManager/OAuthManagerConstants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum _runtime_error
2020

2121
#define QUICK_ERROR(error_code, error_description) [NSError errorWithDomain:NSStringFromClass([self class]) code:error_code userInfo:[NSDictionary dictionaryWithObject:error_description forKey:NSLocalizedDescriptionKey]];
2222

23-
typedef void(^AuthManagerCompletionBlock)(DCTAuthAccount *account);
23+
typedef void(^AuthManagerCompletionBlock)(DCTAuthAccount *account, DCTAuthResponse *response);
2424
typedef void(^AuthManagerErrorBlock)(NSError *error);
2525

2626
#endif /* OAuthManagerConstants_h */

0 commit comments

Comments
 (0)