Skip to content

Commit 3ae702e

Browse files
authored
Merge pull request #1461 from AzureAD/veena/fixCrash
Fix crash due to null value being added to set
2 parents 2b3b0a7 + 689b7c3 commit 3ae702e

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

IdentityCore/src/cache/accessor/MSIDDefaultTokenCacheAccessor.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,10 @@ - (BOOL)saveAccount:(MSIDAccount *)account
12771277
}
12781278
else
12791279
{
1280-
[noReturnAccountsSet addObject:accountCacheItem.username];
1280+
if (accountCacheItem.username)
1281+
{
1282+
[noReturnAccountsSet addObject:accountCacheItem.username];
1283+
}
12811284
}
12821285
}
12831286

IdentityCore/tests/integration/MSIDDefaultAccessorSSOIntegrationTests.m

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,55 @@ - (void)testAccountsWithAuthority_whenReturnSignedInAccountTrue_shouldFilterOutS
17881788
XCTAssertFalse([accountUPNs containsObject:@"[email protected]"]);
17891789
}
17901790

1791+
- (void)testAccountsWithAuthority_whenReturnSignedInAccountTrue_AndUserNameNil_shouldNotCrash
1792+
{
1793+
// setup default cache
1794+
[self saveResponseWithUPN:@"[email protected]"
1795+
clientId:@"test_client_id"
1796+
authority:@"https://login.windows.net/common"
1797+
responseScopes:@"user.read user.write"
1798+
inputScopes:@"user.read user.write"
1799+
uid:@"uid"
1800+
utid:@"utid"
1801+
accessToken:@"access token"
1802+
refreshToken:@"refresh token"
1803+
familyId:@"3"
1804+
accessor:_nonSSOAccessor];
1805+
1806+
[self saveResponseWithUPN:nil
1807+
clientId:@"test_client_id2"
1808+
authority:@"https://login.windows.net/common"
1809+
responseScopes:@"user.read user.write"
1810+
inputScopes:@"user.read user.write"
1811+
uid:@"uid2"
1812+
utid:@"utid2"
1813+
accessToken:@"access token"
1814+
refreshToken:@"refresh token 2"
1815+
familyId:nil
1816+
accessor:_nonSSOAccessor];
1817+
1818+
// sign out the second account
1819+
NSError *error;
1820+
XCTAssertTrue([_accountMetadataCache updateSignInStateForHomeAccountId:@"uid2.utid2" clientId:@"test_client_id2" state:MSIDAccountMetadataStateSignedOut context:nil error:&error]);
1821+
XCTAssertNil(error);
1822+
1823+
// setup legacy cache
1824+
[self saveResponseWithUPN:@"[email protected]"
1825+
clientId:@"test_client_id3"
1826+
authority:@"https://login.windows.net/common"
1827+
responseScopes:@"user.read user.write"
1828+
inputScopes:@"user.read user.write"
1829+
uid:@"uid3"
1830+
utid:@"utid3"
1831+
accessToken:@"access token"
1832+
refreshToken:@"refresh token 2"
1833+
familyId:@"3"
1834+
accessor:_otherAccessor];
1835+
1836+
NSArray *accounts = [_defaultAccessor accountsWithAuthority:nil clientId:@"test_client_id2" familyId:@"3" accountIdentifier:nil accountMetadataCache:_accountMetadataCache signedInAccountsOnly:YES context:nil error:&error];
1837+
XCTAssertEqual([accounts count], 2);
1838+
}
1839+
17911840
- (void)testAccountsWithAuthority_whenReturnSignedInAccountTrue_shouldFilterOutSignedOutAccountInBothDefaultAndLegacyCache
17921841
{
17931842
// setup default cache
@@ -3117,7 +3166,15 @@ - (void)saveResponseWithUPN:(NSString *)upn
31173166
appIdentifier:(NSString *)appIdentifier
31183167
accessor:(id<MSIDCacheAccessor>)accessor
31193168
{
3120-
NSString *idToken = [MSIDTestIdTokenUtil idTokenWithPreferredUsername:upn subject:@"subject" givenName:@"Hello" familyName:@"World" name:@"Hello World" version:@"2.0" tid:tenantId];
3169+
NSString *idToken = nil;
3170+
if (!upn)
3171+
{
3172+
idToken = [MSIDTestIdTokenUtil idTokenWithGivenName:@"Hello" familyName:@"World" name:@"Hello World" version:@"2.0" tid:tenantId];
3173+
}
3174+
else
3175+
{
3176+
idToken = [MSIDTestIdTokenUtil idTokenWithPreferredUsername:upn subject:@"subject" givenName:@"Hello" familyName:@"World" name:@"Hello World" version:@"2.0" tid:tenantId];
3177+
}
31213178

31223179
MSIDTokenResponse *response = [MSIDTestTokenResponse v2TokenResponseWithAT:accessToken
31233180
RT:refreshToken

IdentityCore/tests/util/MSIDTestIdTokenUtil.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@
8383
version:(NSString *)version
8484
tid:(NSString *)tid;
8585

86+
+ (NSString *)idTokenWithGivenName:(NSString *)givenName
87+
familyName:(NSString *)familyName
88+
name:(NSString *)name
89+
version:(NSString *)version
90+
tid:(NSString *)tid;
91+
8692
@end

IdentityCore/tests/util/MSIDTestIdTokenUtil.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ + (NSString *)idTokenWithPreferredUsername:(NSString *)username
170170
tid:@"contoso.com"];
171171
}
172172

173+
+ (NSString *)idTokenWithGivenName:(NSString *)givenName
174+
familyName:(NSString *)familyName
175+
name:(NSString *)name
176+
version:(NSString *)version
177+
tid:(NSString *)tid
178+
{
179+
NSString *idTokenp1 = [@{ @"typ": @"JWT", @"alg": @"RS256", @"kid": @"_kid_value"} msidBase64UrlJson];
180+
NSString *idTokenp2 = [@{ @"iss" : @"issuer",
181+
@"given_name" : givenName,
182+
@"family_name" : familyName,
183+
@"name" : name,
184+
@"ver": version,
185+
@"tid": tid ? tid : @""
186+
} msidBase64UrlJson];
187+
return [NSString stringWithFormat:@"%@.%@.%@", idTokenp1, idTokenp2, idTokenp1];
188+
}
189+
173190
+ (NSString *)idTokenWithPreferredUsername:(NSString *)username
174191
subject:(NSString *)subject
175192
givenName:(NSString *)givenName

0 commit comments

Comments
 (0)