Skip to content

Commit 7792bfb

Browse files
authored
Add STS error codes to MSAL public error (#2319)
* add new error codes key and parse msid error codes * add new error converter tests * update changelog file * add new unit test to check specific scenario when user info is nil
1 parent 13e7df2 commit 7792bfb

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## [TBD]:
2+
* Parse and add STS error codes in token error result (#2319)
23
* VisionOS support added (#2139)
34

45
## [1.5.0]

MSAL/IdentityCore

Submodule IdentityCore updated 41 files

MSAL/src/MSALError.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
NSString *MSALOAuthErrorKey = @"MSALOAuthErrorKey";
3232
NSString *MSALOAuthSubErrorKey = @"MSALOAuthSubErrorKey";
3333
NSString *MSALErrorDescriptionKey = @"MSALErrorDescriptionKey";
34+
NSString *MSALSTSErrorCodesKey = @"MSALSTSErrorCodesKey";
3435
NSString *MSALInternalErrorCodeKey = @"MSALInternalErrorCodeKey";
3536
NSString *MSALHTTPHeadersKey = @"MSALHTTPHeadersKey";
3637
NSString *MSALHTTPResponseCodeKey = @"MSALHTTPResponseCodeKey";

MSAL/src/MSALErrorConverter.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ + (void)initialize
139139
MSIDHTTPResponseCodeKey : MSALHTTPResponseCodeKey,
140140
MSIDCorrelationIdKey : MSALCorrelationIDKey,
141141
MSIDErrorDescriptionKey : MSALErrorDescriptionKey,
142+
MSIDSTSErrorCodesKey : MSALSTSErrorCodesKey,
142143
MSIDOAuthErrorKey: MSALOAuthErrorKey,
143144
MSIDOAuthSubErrorKey: MSALOAuthSubErrorKey,
144145
MSIDDeclinedScopesKey: MSALDeclinedScopesKey,
@@ -249,8 +250,12 @@ + (NSError *)errorWithDomain:(NSString *)domain
249250
if (errorDescription) msalUserInfo[MSALErrorDescriptionKey] = errorDescription;
250251
if (oauthError) msalUserInfo[MSALOAuthErrorKey] = oauthError;
251252
if (subError) msalUserInfo[MSALOAuthSubErrorKey] = subError;
252-
253-
if (underlyingError) msalUserInfo[NSUnderlyingErrorKey] = [MSALErrorConverter msalErrorFromMsidError:underlyingError];
253+
254+
if (underlyingError) {
255+
msalUserInfo[NSUnderlyingErrorKey] = [MSALErrorConverter msalErrorFromMsidError:underlyingError];
256+
NSArray<NSNumber *>* stsErrorCodes = underlyingError.userInfo[MSIDSTSErrorCodesKey];
257+
if (stsErrorCodes) msalUserInfo[MSALSTSErrorCodesKey] = stsErrorCodes;
258+
}
254259

255260
msalUserInfo[MSALInternalErrorCodeKey] = internalCode;
256261

MSAL/src/public/MSALError.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ extern NSString *MSALOAuthSubErrorKey;
6262
*/
6363
extern NSString *MSALErrorDescriptionKey;
6464

65+
/**
66+
A list of STS-specific error codes returned by the service that can help in diagnostics. Note that error codes can change and should
67+
not be relied upon for any error handling logic.
68+
*/
69+
extern NSString *MSALSTSErrorCodesKey;
70+
6571
/**
6672
Internal error code returned together with MSALErrorInternal error.
6773
*/

MSAL/test/unit/MSALErrorConverterTests.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ - (void)testErrorConversion_whenOnlyErrorDomainIsMapped_ErrorCodeShouldBeKept {
8989
NSUUID *correlationId = [NSUUID UUID];
9090
NSDictionary *httpHeaders = @{@"fake header key" : @"fake header value"};
9191
NSString *httpResponseCode = @"-99999";
92+
NSArray<NSNumber *> *stsErrorCodes = @[@123];
9293

9394
NSError *msalError = [MSALErrorConverter errorWithDomain:MSIDKeychainErrorDomain
9495
code:errorCode
@@ -100,6 +101,7 @@ - (void)testErrorConversion_whenOnlyErrorDomainIsMapped_ErrorCodeShouldBeKept {
100101
userInfo:@{MSIDHTTPHeadersKey : httpHeaders,
101102
MSIDHTTPResponseCodeKey : httpResponseCode,
102103
MSIDThrottlingCacheHitKey : @1,
104+
MSIDSTSErrorCodesKey : stsErrorCodes,
103105
@"additional_user_info": @"unmapped_userinfo"}
104106
classifyErrors:YES
105107
msalOauth2Provider:nil
@@ -123,6 +125,29 @@ - (void)testErrorConversion_whenOnlyErrorDomainIsMapped_ErrorCodeShouldBeKept {
123125
XCTAssertNil(msalError.userInfo[MSIDHTTPResponseCodeKey]);
124126
XCTAssertEqualObjects(msalError.userInfo[@"additional_user_info"], @"unmapped_userinfo");
125127
XCTAssertTrue(msalError.userInfo[MSALThrottlingCacheHitKey]);
128+
XCTAssertEqualObjects(msalError.userInfo[MSALSTSErrorCodesKey], stsErrorCodes);
129+
}
130+
131+
- (void)testErrorConversion_ErrorCodesAreAlsoRetrievedFromUnderlyingError_ErrorShouldBeParsedCorrectly {
132+
NSArray<NSNumber *> *stsErrorCodes = @[@123];
133+
NSError *underlyingError = [NSError errorWithDomain:NSOSStatusErrorDomain code:errSecItemNotFound userInfo:@{MSIDSTSErrorCodesKey : stsErrorCodes}];
134+
135+
136+
NSError *msalError = [MSALErrorConverter errorWithDomain:MSIDKeychainErrorDomain
137+
code:1
138+
errorDescription:@"description"
139+
oauthError:@"oauthError"
140+
subError:@"subError"
141+
underlyingError:underlyingError
142+
correlationId:[NSUUID UUID]
143+
userInfo:nil
144+
classifyErrors:YES
145+
msalOauth2Provider:nil
146+
authScheme:[MSALAuthenticationSchemeBearer new]
147+
popManager:nil];
148+
149+
XCTAssertNotNil(msalError);
150+
XCTAssertEqualObjects(msalError.userInfo[MSALSTSErrorCodesKey], stsErrorCodes);
126151
}
127152

128153
- (void)testErrorConversion_whenUnclassifiedInternalMSALErrorPassed_shouldMapToInternal
@@ -153,6 +178,7 @@ - (void)testErrorConversion_whenUnclassifiedInternalMSALErrorPassed_shouldMapToI
153178
XCTAssertEqualObjects(msalError.userInfo[MSALOAuthErrorKey], oauthError);
154179
XCTAssertEqualObjects(msalError.userInfo[MSALOAuthSubErrorKey], subError);
155180
XCTAssertEqualObjects(msalError.userInfo[MSALInternalErrorCodeKey], @(-42400));
181+
XCTAssertFalse([msalError.userInfo.allKeys containsObject: MSALSTSErrorCodesKey]);
156182
}
157183

158184
- (void)testErrorConversion_whenUnclassifiedInternalMSALErrorPassed_andErrorDescriptionPassedInDictionary_shouldMapToInternal_andPreserveErrorDescription

0 commit comments

Comments
 (0)