3939#import " MSIDDefaultAccountCacheQuery.h"
4040#import " MSIDAccountIdentifier.h"
4141#import " MSIDTelemetry+Cache.h"
42+ #import " MSIDAuthority.h"
43+ #import " MSIDAuthorityFactory.h"
4244
4345@interface MSIDDefaultTokenCacheAccessor ()
4446{
@@ -137,8 +139,8 @@ - (MSIDRefreshToken *)getRefreshTokenWithAccount:(MSIDAccountIdentifier *)accoun
137139
138140 MSIDDefaultCredentialCacheQuery *query = [MSIDDefaultCredentialCacheQuery new ];
139141 query.homeAccountId = account.homeAccountId ;
140- query.environment = configuration.authority .msidHostWithPortIfNecessary ;
141- query.clientId = configuration.clientId ;
142+ query.environmentAliases = [_factory defaultCacheAliasesForEnvironment: configuration.authority.environment] ;
143+ query.clientId = familyId ? nil : configuration.clientId ;
142144 query.familyId = familyId;
143145 query.credentialType = MSIDRefreshTokenType;
144146
@@ -219,8 +221,8 @@ - (MSIDAccessToken *)getAccessTokenForAccount:(MSIDAccountIdentifier *)account
219221
220222 MSIDDefaultCredentialCacheQuery *query = [MSIDDefaultCredentialCacheQuery new ];
221223 query.homeAccountId = account.homeAccountId ;
222- query.environment = configuration.authority .msidHostWithPortIfNecessary ;
223- query.realm = configuration.authority .msidTenant ;
224+ query.environmentAliases = [_factory defaultCacheAliasesForEnvironment: configuration.authority.environment] ;
225+ query.realm = configuration.authority .url . msidTenant ;
224226 query.clientId = configuration.clientId ;
225227 query.target = configuration.target ;
226228 query.targetMatchingOptions = MSIDSubSet;
@@ -239,8 +241,8 @@ - (MSIDIdToken *)getIDTokenForAccount:(MSIDAccountIdentifier *)account
239241{
240242 MSIDDefaultCredentialCacheQuery *query = [MSIDDefaultCredentialCacheQuery new ];
241243 query.homeAccountId = account.homeAccountId ;
242- query.environment = configuration.authority .msidHostWithPortIfNecessary ;
243- query.realm = configuration.authority .msidTenant ;
244+ query.environmentAliases = [_factory defaultCacheAliasesForEnvironment: configuration.authority.environment] ;
245+ query.realm = configuration.authority .url . msidTenant ;
244246 query.clientId = configuration.clientId ;
245247 query.credentialType = MSIDIDTokenType;
246248
@@ -265,11 +267,11 @@ - (MSIDIdToken *)getIDTokenForAccount:(MSIDAccountIdentifier *)account
265267 NSArray <NSString *> *environmentAliases = [_factory defaultCacheAliasesForEnvironment: environment];
266268 __auto_type accountsPerUserId = [self getAccountsPerUserIdForAliases: environmentAliases context: context error: error];
267269
268- if (!accountsPerUserId)
270+ if (![ accountsPerUserId count ] )
269271 {
270272 MSID_LOG_INFO (context, @" No accounts found, returning!" );
271273 [MSIDTelemetry stopCacheEvent: event withItem: nil success: NO context: context];
272- return nil ;
274+ return @[] ;
273275 }
274276
275277 MSIDDefaultCredentialCacheQuery *credentialsQuery = [MSIDDefaultCredentialCacheQuery new ];
@@ -321,7 +323,7 @@ - (MSIDAccount *)accountForIdentifier:(MSIDAccountIdentifier *)accountIdentifier
321323
322324 MSIDDefaultAccountCacheQuery *cacheQuery = [MSIDDefaultAccountCacheQuery new ];
323325 cacheQuery.homeAccountId = accountIdentifier.homeAccountId ;
324- cacheQuery.environmentAliases = [_factory defaultCacheAliasesForEnvironment: configuration.authority.msidHostWithPortIfNecessary ];
326+ cacheQuery.environmentAliases = [_factory defaultCacheAliasesForEnvironment: configuration.authority.environment ];
325327 cacheQuery.accountType = MSIDAccountTypeMSSTS;
326328
327329 NSArray <MSIDAccountCacheItem *> *accountCacheItems = [_accountCredentialCache getAccountsWithQuery: cacheQuery context: context error: error];
@@ -440,7 +442,7 @@ - (BOOL)validateAndRemoveRefreshToken:(MSIDRefreshToken *)token
440442 MSID_LOG_VERBOSE (context, @" Removing refresh token with clientID %@ , authority %@ " , token.clientId , token.authority );
441443 MSID_LOG_VERBOSE_PII (context, @" Removing refresh token with clientID %@ , authority %@ , userId %@ , token %@ " , token.clientId , token.authority , token.accountIdentifier .homeAccountId , _PII_NULLIFY (token.refreshToken ));
442444
443- NSURL *authority = token.storageAuthority ? token.storageAuthority : token.authority ;
445+ NSURL *authority = token.storageAuthority . url ? token.storageAuthority . url : token.authority . url ;
444446
445447 MSIDDefaultCredentialCacheQuery *query = [MSIDDefaultCredentialCacheQuery new ];
446448 query.homeAccountId = token.accountIdentifier .homeAccountId ;
@@ -650,8 +652,8 @@ - (BOOL)saveAccessToken:(MSIDAccessToken *)accessToken
650652 // Delete access tokens with intersecting scopes
651653 MSIDDefaultCredentialCacheQuery *query = [MSIDDefaultCredentialCacheQuery new ];
652654 query.homeAccountId = accessToken.accountIdentifier .homeAccountId ;
653- query.environment = accessToken.authority .msidHostWithPortIfNecessary ;
654- query.realm = accessToken.authority .msidTenant ;
655+ query.environment = accessToken.authority .environment ;
656+ query.realm = accessToken.authority .url . msidTenant ;
655657 query.clientId = accessToken.clientId ;
656658 query.target = [accessToken.scopes msidToString ];
657659 query.targetMatchingOptions = MSIDIntersect;
@@ -727,42 +729,37 @@ - (BOOL)removeToken:(MSIDBaseToken *)token
727729
728730#pragma mark - Private
729731
730- - (MSIDBaseToken *)getTokenWithAuthority : (NSURL *)authority
732+ - (MSIDBaseToken *)getTokenWithAuthority : (MSIDAuthority *)authority
731733 cacheQuery : (MSIDDefaultCredentialCacheQuery *)cacheQuery
732734 context : (id <MSIDRequestContext>)context
733735 error : (NSError **)error
734736{
735737 MSIDTelemetryCacheEvent *event = [MSIDTelemetry startCacheEventWithName: MSID_TELEMETRY_EVENT_TOKEN_CACHE_LOOKUP context: context];
736738
737- NSArray < NSString *> * aliases = [_factory defaultCacheAliasesForEnvironment: authority.msidHostWithPortIfNecessary] ;
739+ MSID_LOG_VERBOSE (context, @" (Default accessor) Looking for token with aliases %@ , tenant %@ , clientId %@ , scopes %@ " , cacheQuery. environmentAliases , cacheQuery. realm , cacheQuery. clientId , cacheQuery. target ) ;
738740
739- for (NSString *alias in aliases)
740- {
741- MSID_LOG_VERBOSE (context, @" (Default accessor) Looking for token with alias %@ , tenant %@ , clientId %@ , scopes %@ " , alias, cacheQuery.realm , cacheQuery.clientId , cacheQuery.target );
741+ NSError *cacheError = nil ;
742742
743- NSError *cacheError = nil ;
743+ NSArray <MSIDCredentialCacheItem *> *cacheItems = [_accountCredentialCache getCredentialsWithQuery: cacheQuery legacyUserId: nil context: context error: error] ;
744744
745- NSArray <MSIDCredentialCacheItem *> *cacheItems = [_accountCredentialCache getCredentialsWithQuery: cacheQuery legacyUserId: nil context: context error: error];
745+ if (cacheError)
746+ {
747+ if (error) *error = cacheError;
748+ [MSIDTelemetry stopCacheEvent: event withItem: nil success: NO context: context];
749+ return nil ;
750+ }
746751
747- if (cacheError)
748- {
749- if (error) *error = cacheError;
750- [MSIDTelemetry stopCacheEvent: event withItem: nil success: NO context: context];
751- return nil ;
752- }
752+ if ([cacheItems count ])
753+ {
754+ MSIDBaseToken *resultToken = [cacheItems[0 ] tokenWithType: cacheQuery.credentialType];
753755
754- if ([cacheItems count ] )
756+ if (resultToken )
755757 {
756- MSIDBaseToken *resultToken = [cacheItems[0 ] tokenWithType: cacheQuery.credentialType];
757-
758- if (resultToken)
759- {
760- MSID_LOG_VERBOSE (context, @" (Default accessor) Found %lu tokens" , (unsigned long )[cacheItems count ]);
761- resultToken.storageAuthority = resultToken.authority ;
762- resultToken.authority = authority;
763- [MSIDTelemetry stopCacheEvent: event withItem: resultToken success: YES context: context];
764- return resultToken;
765- }
758+ MSID_LOG_VERBOSE (context, @" (Default accessor) Found %lu tokens" , (unsigned long )[cacheItems count ]);
759+ resultToken.storageAuthority = resultToken.authority ;
760+ resultToken.authority = authority;
761+ [MSIDTelemetry stopCacheEvent: event withItem: resultToken success: YES context: context];
762+ return resultToken;
766763 }
767764 }
768765
@@ -778,7 +775,7 @@ - (MSIDBaseToken *)getTokenWithAuthority:(NSURL *)authority
778775}
779776
780777- (MSIDBaseToken *)getRefreshTokenByLegacyUserId : (NSString *)legacyUserId
781- authority : (NSURL *)authority
778+ authority : (MSIDAuthority *)authority
782779 clientId : (NSString *)clientId
783780 familyId : (NSString *)familyId
784781 context : (id <MSIDRequestContext>)context
@@ -789,7 +786,7 @@ - (MSIDBaseToken *)getRefreshTokenByLegacyUserId:(NSString *)legacyUserId
789786
790787 MSIDTelemetryCacheEvent *event = [MSIDTelemetry startCacheEventWithName: MSID_TELEMETRY_EVENT_TOKEN_CACHE_LOOKUP context: context];
791788
792- NSArray <NSString *> *aliases = [_factory defaultCacheAliasesForEnvironment: authority.msidHostWithPortIfNecessary ];
789+ NSArray <NSString *> *aliases = [_factory defaultCacheAliasesForEnvironment: authority.environment ];
793790
794791 NSString *clientIdForQueries = clientId;
795792
@@ -875,7 +872,7 @@ - (BOOL)saveAccount:(MSIDAccount *)account
875872
876873 MSIDTelemetryCacheEvent *event = [MSIDTelemetry startCacheEventWithName: MSID_TELEMETRY_EVENT_TOKEN_CACHE_WRITE context: context];
877874 MSIDAccountCacheItem *cacheItem = account.accountCacheItem ;
878- cacheItem.environment = [_factory cacheEnvironmentFromEnvironment: account.authority.msidHostWithPortIfNecessary context: context];
875+ cacheItem.environment = [_factory cacheEnvironmentFromEnvironment: account.authority.environment context: context];
879876
880877 BOOL result = [_accountCredentialCache saveAccount: cacheItem context: context error: error];
881878 [MSIDTelemetry stopCacheEvent: event withItem: nil success: result context: context];
0 commit comments