Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #26 from auth0/bugfix-delegation-ios
Browse files Browse the repository at this point in the history
Add support for refresh_token in delegation method
  • Loading branch information
hzalaz committed Apr 4, 2016
2 parents c1e8ae6 + ad8488f commit 6404124
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions A0RNLock/A0LockReactModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ @implementation A0LockReactModule
});
}

RCT_EXPORT_METHOD(getDelegationToken:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
RCT_EXPORT_METHOD(delegation:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
dispatch_async(dispatch_get_main_queue(), ^{
[[A0LockReact sharedInstance] delegationWithOptions:options callback:callback];
});
Expand All @@ -109,4 +109,4 @@ @implementation A0LockReactModule
});
}

@end
@end
34 changes: 26 additions & 8 deletions A0RNLock/Core/A0LockReact.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,32 @@ - (void)delegationWithOptions:(NSDictionary *)options callback:(A0LockCallback)c
callback(@[@"Please configure Lock before using it"]);
return;
}
if (!options[@"api"]) {
callback(@[@"Must specify api in options."]);
return;

NSString *api = options[@"api"];
if (!api) {
api = @"app";
}
if (!options[@"id_token"]) {
callback(@[@"Must specify id_token in options"]);

NSString *token = options[@"token"];
NSString *refreshToken = options[@"refresh_token"];

if (token && refreshToken) {
callback(@[@"Must specify either a token or a refreshToken in options"]);
return;
}
if (!options[@"scope"]) {
callback(@[@"Must specify scope in options"]);

if (!token && !refreshToken) {
callback(@[@"Must specify at least a token or a refreshToken in options"]);
return;
}

NSString *scope = options[@"scope"];
if (!scope) {
scope = @"openid";
}

NSString *target = options[@"target"];

A0APIClient *client = [self.lock apiClient];

void(^success)(NSDictionary *) = ^(NSDictionary *credentials) {
Expand All @@ -171,11 +184,16 @@ - (void)delegationWithOptions:(NSDictionary *)options callback:(A0LockCallback)c
callback(@[[error localizedDescription]]);
};

NSString *tokenKey = refreshToken ? @"refresh_token" : @"id_token";
NSString *tokenValue = refreshToken ? refreshToken : token;
A0AuthParameters *parameters = [A0AuthParameters newWithDictionary:@{
A0ParameterAPIType: options[@"api"],
@"id_token": options[@"id_token"],
tokenKey: tokenValue,
A0ParameterScope: [self scopeParamterFromOptions:options],
}];
if (target) {
parameters[@"target"] = target;
}
[client fetchDelegationTokenWithParameters:parameters success:success failure:failure];
}

Expand Down

0 comments on commit 6404124

Please sign in to comment.