Skip to content

Commit 8f9bfc7

Browse files
authored
Merge pull request #1520 from AzureAD/antonioalwan/17_handle_account_removed_artifacts
Handle account removed suberror in brokers and MSALs
2 parents 1ba9013 + 614cca4 commit 8f9bfc7

File tree

11 files changed

+201
-0
lines changed

11 files changed

+201
-0
lines changed

IdentityCore/IdentityCore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,7 @@
18971897
E656E07A2C2627B80011FB23 /* MSIDWebUpgradeRegResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = E656E0792C2627B80011FB23 /* MSIDWebUpgradeRegResponse.m */; };
18981898
E656E07B2C2627B80011FB23 /* MSIDWebUpgradeRegResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = E656E0792C2627B80011FB23 /* MSIDWebUpgradeRegResponse.m */; };
18991899
E656E07D2C2627FB0011FB23 /* MSIDWebUpgradeRegResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = E656E07C2C2627FB0011FB23 /* MSIDWebUpgradeRegResponse.h */; };
1900+
E6B895AD2DD2AEE1000879AE /* MSIDSilentTokenRequest+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = E6B895AC2DD2AEC2000879AE /* MSIDSilentTokenRequest+Internal.h */; };
19001901
E70C49F1258D662F00A7A07E /* MSIDLRUCache.m in Sources */ = {isa = PBXBuildFile; fileRef = E70C49F0258D662F00A7A07E /* MSIDLRUCache.m */; };
19011902
E70C49F2258D662F00A7A07E /* MSIDLRUCache.m in Sources */ = {isa = PBXBuildFile; fileRef = E70C49F0258D662F00A7A07E /* MSIDLRUCache.m */; };
19021903
E70C4A30258D68D900A7A07E /* MSIDThrottlingCacheRecord.m in Sources */ = {isa = PBXBuildFile; fileRef = E70C4A2F258D68D900A7A07E /* MSIDThrottlingCacheRecord.m */; };
@@ -3379,6 +3380,7 @@
33793380
D6D9A4BB1FBE712900EFA430 /* MSIDStringExtensionsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSIDStringExtensionsTests.m; sourceTree = "<group>"; };
33803381
E656E0792C2627B80011FB23 /* MSIDWebUpgradeRegResponse.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSIDWebUpgradeRegResponse.m; sourceTree = "<group>"; };
33813382
E656E07C2C2627FB0011FB23 /* MSIDWebUpgradeRegResponse.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSIDWebUpgradeRegResponse.h; sourceTree = "<group>"; };
3383+
E6B895AC2DD2AEC2000879AE /* MSIDSilentTokenRequest+Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MSIDSilentTokenRequest+Internal.h"; sourceTree = "<group>"; };
33823384
E6FABC0A2C2A110900611378 /* MSIDWebWPJResponse+Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MSIDWebWPJResponse+Internal.h"; sourceTree = "<group>"; };
33833385
E70C49F0258D662F00A7A07E /* MSIDLRUCache.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSIDLRUCache.m; sourceTree = "<group>"; };
33843386
E70C4A2F258D68D900A7A07E /* MSIDThrottlingCacheRecord.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSIDThrottlingCacheRecord.m; sourceTree = "<group>"; };
@@ -5062,6 +5064,7 @@
50625064
23B01884235548C000207FEC /* MSIDInteractiveTokenRequest+Internal.h */,
50635065
B2AF1D2D218BCEDE0080C1A0 /* MSIDInteractiveTokenRequest.m */,
50645066
B2F671EB2467AB4500649855 /* MSIDInteractiveRequestControlling.h */,
5067+
E6B895AC2DD2AEC2000879AE /* MSIDSilentTokenRequest+Internal.h */,
50655068
B2AF1D31218BCEEB0080C1A0 /* MSIDSilentTokenRequest.h */,
50665069
B2AF1D32218BCEEB0080C1A0 /* MSIDSilentTokenRequest.m */,
50675070
B286B9542385F01A007833AD /* MSIDOIDCSignoutRequest.h */,
@@ -6147,6 +6150,7 @@
61476150
23C898162C8928DD00071482 /* MSIDBrowserNativeMessageGetSupportedContractsRequest.h in Headers */,
61486151
238E19E12086FE28004DF483 /* MSIDAuthorizationCodeGrantRequest.h in Headers */,
61496152
238EF038208FDBA20035ABE6 /* MSIDAADV1RefreshTokenGrantRequest.h in Headers */,
6153+
E6B895AD2DD2AEE1000879AE /* MSIDSilentTokenRequest+Internal.h in Headers */,
61506154
589BDB272718F18800BF3799 /* MSIDCredentialHeader.h in Headers */,
61516155
235480C620DDF81000246F72 /* MSIDADFSAuthority.h in Headers */,
61526156
96CE53E920C73E3A002E24C9 /* MSIDWebviewFactory.h in Headers */,

IdentityCore/src/MSIDConstants.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,15 @@ extern NSString * _Nonnull const MSID_FLIGHT_USE_V2_WEB_RESPONSE_FACTORY;
215215
extern NSString * _Nonnull const MSID_FLIGHT_SUPPORT_DUNA_CBA;
216216
extern NSString * _Nonnull const MSID_FLIGHT_CLIENT_SFRT_STATUS;
217217

218+
/**
219+
* Flight to indicate if remove account artifacts should be disabled
220+
* Owner: Antonio
221+
* Link: N/A - No ECS flag created as this is a disable flight to be created on demand
222+
* Created Date: N/A
223+
* Status: Not started
224+
* Fully Allocated: Not Started
225+
* WorkItem: 3168316
226+
*/
227+
extern NSString * _Nonnull const MSID_FLIGHT_DISABLE_REMOVE_ACCOUNT_ARTIFACTS;
228+
218229
#define METHODANDLINE [NSString stringWithFormat:@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__]

IdentityCore/src/MSIDConstants.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,8 @@
8787
NSString *const MSID_FLIGHT_SUPPORT_DUNA_CBA = @"support_duna_cba_v2";
8888
NSString *const MSID_FLIGHT_CLIENT_SFRT_STATUS = @"sfrt_status";
8989

90+
// Making the flight string short to avoid legacy broker url size limit
91+
NSString *const MSID_FLIGHT_DISABLE_REMOVE_ACCOUNT_ARTIFACTS = @"disable_rm_metadata";
92+
9093

9194
#define METHODANDLINE [NSString stringWithFormat:@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__]

IdentityCore/src/MSIDOAuth2Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ extern NSString *const MSID_PREFERRED_USERNAME_MISSING;
170170

171171
extern NSString *const MSIDServerErrorClientMismatch;
172172
extern NSString *const MSIDServerErrorBadToken;
173+
extern NSString *const MSIDServerErrorUserAccountDeleted;
173174

174175
extern NSString *const MSID_CCS_REQUEST_ID_KEY;
175176
extern NSString *const MSID_CCS_REQUEST_ID_RESPONSE;

IdentityCore/src/MSIDOAuth2Constants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170

171171
NSString *const MSIDServerErrorClientMismatch = @"client_mismatch";
172172
NSString *const MSIDServerErrorBadToken = @"bad_token";
173+
NSString *const MSIDServerErrorUserAccountDeleted = @"user_account_deleted";
173174

174175
NSString *const MSID_CCS_REQUEST_ID_KEY = @"x-ms-ccs-requestid";
175176
NSString *const MSID_CCS_REQUEST_ID_RESPONSE = @"ccs-requestid";

IdentityCore/src/cache/metadata/MSIDAccountMetadataCacheAccessor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@
7777
- (MSIDAccountMetadataCacheItem *)retrieveAccountMetadataCacheItemForClientId:(NSString *)clientId
7878
context:(id<MSIDRequestContext>)context
7979
error:(NSError *__autoreleasing*)error;
80+
81+
- (BOOL)removeAccountMetadataForHomeAccountId:(NSString *)homeAccountId
82+
context:(id<MSIDRequestContext>)context
83+
error:(NSError *__autoreleasing*)error;
84+
8085
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
#import <Foundation/Foundation.h>
25+
#import "MSIDCacheAccessor.h"
26+
#import "MSIDConstants.h"
27+
#import "MSIDThrottlingService.h"
28+
29+
@interface MSIDSilentTokenRequest (Internal)
30+
31+
- (BOOL)shouldRemoveAccountArtifacts:(nonnull NSError *)serverError;
32+
33+
@end
34+

IdentityCore/src/requests/MSIDSilentTokenRequest.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#import "MSIDTokenResponseHandler.h"
4141
#import "MSIDLastRequestTelemetry.h"
4242
#import "MSIDCurrentRequestTelemetry.h"
43+
#import "MSIDAccountMetadataCacheItem.h"
44+
#import "MSIDFlightManager.h"
4345

4446
#if TARGET_OS_OSX && !EXCLUDE_FROM_MSALCPP
4547
#import "MSIDExternalAADCacheSeeder.h"
@@ -617,10 +619,43 @@ - (void)sendTokenRequestImpl:(MSIDRequestCompletionBlock)completionBlock
617619
}
618620
}
619621

622+
BOOL disableRemoveAccountArtifacts = [MSIDFlightManager.sharedInstance boolForKey:MSID_FLIGHT_DISABLE_REMOVE_ACCOUNT_ARTIFACTS];
623+
624+
// remove account artifacts only if we test flight feature is not disabled
625+
if (!result && !disableRemoveAccountArtifacts && [self shouldRemoveAccountArtifacts:localError])
626+
{
627+
MSID_LOG_WITH_CTX(MSIDLogLevelInfo, self.requestParameters, @"Account deleted, Removing any user account artifacts from device...");
628+
[self removeAccountArtifacts:self.requestParameters];
629+
}
630+
620631
completionBlock(result, localError);
621632
}];
622633
}];
623634
}
635+
636+
- (void)removeAccountArtifacts:(MSIDRequestParameters *)requestParameters
637+
{
638+
NSError *removalError = nil;
639+
BOOL removalResult = [self.tokenCache clearCacheForAccount:requestParameters.accountIdentifier
640+
authority:requestParameters.authority
641+
clientId:requestParameters.clientId
642+
familyId:nil
643+
context:requestParameters
644+
error:&removalError];
645+
if (!removalResult)
646+
{
647+
MSID_LOG_WITH_CTX_PII(MSIDLogLevelWarning, requestParameters, @"Failed to clear cache with error %@", MSID_PII_LOG_MASKABLE(removalError));
648+
}
649+
650+
NSError *metadataRemovalError = nil;
651+
[[self metadataCache] removeAccountMetadataForHomeAccountId:requestParameters.accountIdentifier.homeAccountId
652+
context:requestParameters
653+
error:&metadataRemovalError];
654+
if (metadataRemovalError)
655+
{
656+
MSID_LOG_WITH_CTX_PII(MSIDLogLevelWarning, requestParameters, @"Failed to remove account artifacts with error %@", MSID_PII_LOG_MASKABLE(metadataRemovalError));
657+
}
658+
}
624659
#endif
625660

626661
#pragma mark - Abstract
@@ -664,6 +699,14 @@ - (BOOL)shouldRemoveRefreshToken:(__unused NSError *)serverError
664699
return NO;
665700
}
666701

702+
- (BOOL)shouldRemoveAccountArtifacts:(nonnull NSError *)serverError
703+
{
704+
// Removing account artifacts on invalid_grant + user_deleted_account suberror combination
705+
MSIDErrorCode oauthError = MSIDErrorCodeForOAuthError(serverError.msidOauthError, MSIDErrorInternal);
706+
NSString *subError = serverError.msidSubError;
707+
return oauthError == MSIDErrorServerInvalidGrant && [subError isEqualToString:MSIDServerErrorUserAccountDeleted];
708+
}
709+
667710
- (id<MSIDCacheAccessor>)tokenCache
668711
{
669712
NSAssert(NO, @"Abstract method. Should be implemented in a subclass");

IdentityCore/src/requests/sdk/adal/MSIDLegacySilentTokenRequest.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "MSIDAccessToken.h"
2929
#import "NSError+MSIDExtensions.h"
3030
#import "MSIDAccountMetadataCacheAccessor.h"
31+
#import "MSIDSilentTokenRequest+Internal.h"
3132

3233
@interface MSIDLegacySilentTokenRequest()
3334

@@ -100,6 +101,11 @@ - (BOOL)shouldRemoveRefreshToken:(NSError *)serverError
100101
return oauthError == MSIDErrorServerInvalidGrant;
101102
}
102103

104+
- (BOOL)shouldRemoveAccountArtifacts:(NSError *)serverError
105+
{
106+
return [super shouldRemoveAccountArtifacts:serverError];
107+
}
108+
103109
- (id<MSIDCacheAccessor>)tokenCache
104110
{
105111
return self.legacyAccessor;

IdentityCore/src/requests/sdk/msal/MSIDDefaultSilentTokenRequest.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#import "MSIDAccountMetadataCacheAccessor.h"
3838
#import "MSIDTokenResponse.h"
3939
#import "MSIDThrottlingService.h"
40+
#import "MSIDSilentTokenRequest+Internal.h"
4041

4142
@interface MSIDDefaultSilentTokenRequest()
4243

@@ -226,6 +227,11 @@ - (BOOL)shouldRemoveRefreshToken:(NSError *)serverError
226227
return oauthError == MSIDErrorServerInvalidGrant && [subError isEqualToString:MSIDServerErrorBadToken];
227228
}
228229

230+
- (BOOL)shouldRemoveAccountArtifacts:(NSError *)serverError
231+
{
232+
return [super shouldRemoveAccountArtifacts:serverError];
233+
}
234+
229235
- (id<MSIDCacheAccessor>)tokenCache
230236
{
231237
return self.defaultAccessor;

0 commit comments

Comments
 (0)