Skip to content

Commit 624cd6b

Browse files
authored
Merge pull request #151 from AzureAD/sedemche/split_authorities
Split authority
2 parents 3a186bc + bbcb8c6 commit 624cd6b

File tree

86 files changed

+3263
-1645
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3263
-1645
lines changed

IdentityCore/IdentityCore.xcodeproj/project.pbxproj

Lines changed: 126 additions & 69 deletions
Large diffs are not rendered by default.

IdentityCore/src/MSIDAADNetworkConfiguration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@
3636

3737
@property (nonatomic, nullable) NSString *drsDiscoveryApiVersion;
3838

39+
- (BOOL)isAADPublicCloud:(nonnull NSString *)host;
40+
41+
- (nonnull NSSet<NSString *> *)trustedHosts;
42+
3943
@end

IdentityCore/src/MSIDAADNetworkConfiguration.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
#import "MSIDAADNetworkConfiguration.h"
2525
#import "MSIDAADEndpointProvider.h"
26+
#import "MSIDConstants.h"
2627

2728
static MSIDAADNetworkConfiguration *s_defaultConfiguration;
29+
static NSSet<NSString *> *s_trustedHostList;
2830

2931
@implementation MSIDAADNetworkConfiguration
3032

@@ -33,6 +35,15 @@ + (void)initialize
3335
if (self == [MSIDAADNetworkConfiguration self])
3436
{
3537
s_defaultConfiguration = [MSIDAADNetworkConfiguration new];
38+
39+
s_trustedHostList = [NSSet setWithObjects:MSIDTrustedAuthority,
40+
MSIDTrustedAuthorityUS,
41+
MSIDTrustedAuthorityChina,
42+
MSIDTrustedAuthorityChina2,
43+
MSIDTrustedAuthorityGermany,
44+
MSIDTrustedAuthorityWorldWide,
45+
MSIDTrustedAuthorityUSGovernment,
46+
MSIDTrustedAuthorityCloudGovApi, nil];
3647
}
3748
}
3849

@@ -59,4 +70,16 @@ + (void)setDefaultConfiguration:(MSIDAADNetworkConfiguration *)defaultConfigurat
5970
s_defaultConfiguration = defaultConfiguration;
6071
}
6172

73+
- (BOOL)isAADPublicCloud:(NSString *)host
74+
{
75+
if (!host) return NO;
76+
77+
return [s_trustedHostList containsObject:host];
78+
}
79+
80+
- (NSSet<NSString *> *)trustedHosts
81+
{
82+
return s_trustedHostList;
83+
}
84+
6285
@end

IdentityCore/src/MSIDCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
NS_ASSUME_NONNULL_BEGIN
2727

28-
@interface MSIDCache <KeyType, ObjectType> : NSObject
28+
@interface MSIDCache <KeyType, ObjectType> : NSObject <NSCopying>
2929

3030
- (nullable ObjectType)objectForKey:(KeyType)key;
3131

IdentityCore/src/MSIDCache.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ - (NSUInteger)count
8787
return count;
8888
}
8989

90+
#pragma mark - NSCopying
91+
92+
- (id)copyWithZone:(NSZone *)zone
93+
{
94+
MSIDCache *item = [[self.class allocWithZone:zone] init];
95+
item->_container = [_container copyWithZone:zone];
96+
97+
return item;
98+
}
99+
90100
@end

IdentityCore/src/MSIDConstants.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
extern NSString *const MSID_PLATFORM_KEY;//The SDK platform. iOS or OSX
25-
extern NSString *const MSID_VERSION_KEY;
26-
extern NSString *const MSID_CPU_KEY;//E.g. ARM64
27-
extern NSString *const MSID_OS_VER_KEY;//iOS/OSX version
28-
extern NSString *const MSID_DEVICE_MODEL_KEY;//E.g. iPhone 5S
24+
extern NSString * _Nonnull const MSID_PLATFORM_KEY;//The SDK platform. iOS or OSX
25+
extern NSString * _Nonnull const MSID_VERSION_KEY;
26+
extern NSString * _Nonnull const MSID_CPU_KEY;//E.g. ARM64
27+
extern NSString * _Nonnull const MSID_OS_VER_KEY;//iOS/OSX version
28+
extern NSString * _Nonnull const MSID_DEVICE_MODEL_KEY;//E.g. iPhone 5S
29+
30+
extern NSString * _Nonnull const MSIDTrustedAuthority;
31+
extern NSString * _Nonnull const MSIDTrustedAuthorityUS;
32+
extern NSString * _Nonnull const MSIDTrustedAuthorityChina;
33+
extern NSString * _Nonnull const MSIDTrustedAuthorityChina2;
34+
extern NSString * _Nonnull const MSIDTrustedAuthorityGermany;
35+
extern NSString * _Nonnull const MSIDTrustedAuthorityWorldWide;
36+
extern NSString * _Nonnull const MSIDTrustedAuthorityUSGovernment;
37+
extern NSString * _Nonnull const MSIDTrustedAuthorityCloudGovApi;

IdentityCore/src/MSIDConstants.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@
2828
NSString *const MSID_CPU_KEY = @"x-client-CPU";
2929
NSString *const MSID_OS_VER_KEY = @"x-client-OS";
3030
NSString *const MSID_DEVICE_MODEL_KEY = @"x-client-DM";
31+
32+
NSString *const MSIDTrustedAuthority = @"login.windows.net";
33+
NSString *const MSIDTrustedAuthorityUS = @"login.microsoftonline.us";
34+
NSString *const MSIDTrustedAuthorityChina = @"login.chinacloudapi.cn";
35+
NSString *const MSIDTrustedAuthorityChina2 = @"login.partner.microsoftonline.cn";
36+
NSString *const MSIDTrustedAuthorityGermany = @"login.microsoftonline.de";
37+
NSString *const MSIDTrustedAuthorityWorldWide = @"login.microsoftonline.com";
38+
NSString *const MSIDTrustedAuthorityUSGovernment = @"login-us.microsoftonline.com";
39+
NSString *const MSIDTrustedAuthorityCloudGovApi = @"login.usgovcloudapi.net";
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
typedef NS_ENUM(NSInteger, MSIDADFSType)
24+
typedef NS_ENUM(NSInteger, MSIDDRSType)
2525
{
26-
MSIDADFSTypeOnPrems,
27-
MSIDADFSTypeCloud
26+
MSIDDRSTypeOnPrem,
27+
MSIDDRSTypeInCloud
2828
};

IdentityCore/src/cache/accessor/MSIDDefaultTokenCacheAccessor.m

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
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

Comments
 (0)