Skip to content

Commit af1fdbe

Browse files
authored
Improve iOS error handling (#654)
* Fix an error handling case * Pull 'error_description' from userInfo dictionary provided by AppAuth-iOS
1 parent a2e0f04 commit af1fdbe

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

ios/RNAppAuth.m

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (dispatch_queue_t)methodQueue
3535
static NSUInteger const kCodeVerifierBytes = 32;
3636

3737
RCT_EXPORT_MODULE()
38-
38+
3939
RCT_REMAP_METHOD(register,
4040
issuer: (NSString *) issuer
4141
redirectUrls: (NSArray *) redirectUrls
@@ -221,7 +221,7 @@ + (nullable NSString *)codeChallengeS256ForVerifier:(NSString *)codeVerifier {
221221
return [OIDTokenUtilities encodeBase64urlNoPadding:sha256Verifier];
222222
}
223223

224-
224+
225225
/*
226226
* Perform dynamic client registration with provided OIDServiceConfiguration
227227
*/
@@ -239,7 +239,7 @@ - (void)registerWithConfiguration: (OIDServiceConfiguration *) configuration
239239
for (NSString *urlString in redirectUrlStrings) {
240240
[redirectUrls addObject:[NSURL URLWithString:urlString]];
241241
}
242-
242+
243243
OIDRegistrationRequest *request =
244244
[[OIDRegistrationRequest alloc] initWithConfiguration:configuration
245245
redirectURIs:redirectUrls
@@ -248,19 +248,19 @@ - (void)registerWithConfiguration: (OIDServiceConfiguration *) configuration
248248
subjectType:subjectType
249249
tokenEndpointAuthMethod:tokenEndpointAuthMethod
250250
additionalParameters:additionalParameters];
251-
251+
252252
[OIDAuthorizationService performRegistrationRequest:request
253253
completion:^(OIDRegistrationResponse *_Nullable response,
254254
NSError *_Nullable error) {
255255
if (response) {
256256
resolve([self formatRegistrationResponse:response]);
257257
} else {
258258
reject([self getErrorCode: error defaultCode:@"registration_failed"],
259-
[error localizedDescription], error);
259+
[self getErrorMessage: error], error);
260260
}
261261
}];
262262
}
263-
263+
264264
/*
265265
* Authorize a user in exchange for a token with provided OIDServiceConfiguration
266266
*/
@@ -324,7 +324,7 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
324324
resolve([self formatAuthorizationResponse:authorizationResponse withCodeVerifier:codeVerifier]);
325325
} else {
326326
reject([self getErrorCode: error defaultCode:@"authentication_failed"],
327-
[error localizedDescription], error);
327+
[self getErrorMessage: error], error);
328328
}
329329
}]; // end [OIDAuthState presentAuthorizationRequest:request
330330
} else {
@@ -340,7 +340,8 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
340340
resolve([self formatResponse:authState.lastTokenResponse
341341
withAuthResponse:authState.lastAuthorizationResponse]);
342342
} else {
343-
reject(@"authentication_failed", [error localizedDescription], error);
343+
reject([self getErrorCode: error defaultCode:@"authentication_failed"],
344+
[self getErrorMessage: error], error);
344345
}
345346
}]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
346347
}
@@ -378,7 +379,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
378379
resolve([self formatResponse:response]);
379380
} else {
380381
reject([self getErrorCode: error defaultCode:@"token_refresh_failed"],
381-
[error localizedDescription], error);
382+
[self getErrorMessage: error], error);
382383
}
383384
}];
384385
}
@@ -389,7 +390,7 @@ - (void) configureUrlSession: (NSDictionary*) headers {
389390
if (headers != nil) {
390391
configuration.HTTPAdditionalHeaders = headers;
391392
}
392-
393+
393394
NSURLSession* session = [NSURLSession sessionWithConfiguration:configuration];
394395
[OIDURLSessionProvider setSession:session];
395396
}
@@ -466,13 +467,13 @@ - (NSDictionary*)formatResponse: (OIDTokenResponse*) response
466467
@"scopes": authResponse.scope ? [authResponse.scope componentsSeparatedByString:@" "] : [NSArray new],
467468
};
468469
}
469-
470+
470471
- (NSDictionary*)formatRegistrationResponse: (OIDRegistrationResponse*) response {
471472
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
472473
dateFormat.timeZone = [NSTimeZone timeZoneWithAbbreviation: @"UTC"];
473474
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
474475
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
475-
476+
476477
return @{@"clientId": response.clientID,
477478
@"additionalParameters": response.additionalParameters,
478479
@"clientIdIssuedAt": response.clientIDIssuedAt ? [dateFormat stringFromDate:response.clientIDIssuedAt] : @"",
@@ -531,4 +532,16 @@ - (NSString*)getErrorCode: (NSError*) error defaultCode: (NSString *) defaultCod
531532
return defaultCode;
532533
}
533534

535+
- (NSString*)getErrorMessage: (NSError*) error {
536+
NSDictionary * userInfo = [error userInfo];
537+
538+
if (userInfo &&
539+
userInfo[OIDOAuthErrorResponseErrorKey] &&
540+
userInfo[OIDOAuthErrorResponseErrorKey][OIDOAuthErrorFieldErrorDescription]) {
541+
return userInfo[OIDOAuthErrorResponseErrorKey][OIDOAuthErrorFieldErrorDescription];
542+
} else {
543+
return [error localizedDescription];
544+
}
545+
}
546+
534547
@end

0 commit comments

Comments
 (0)