Skip to content

Commit e74002b

Browse files
authored
Merge pull request #349 from AzureAD/return-grouped-accounts
get_accounts() will group equivalent accounts and return the merged result
2 parents b5397c3 + e65731e commit e74002b

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

msal/application.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,25 @@ def get_accounts(self, username=None):
642642
return accounts
643643

644644
def _find_msal_accounts(self, environment):
645-
return [a for a in self.token_cache.find(
646-
TokenCache.CredentialType.ACCOUNT, query={"environment": environment})
645+
grouped_accounts = {
646+
a.get("home_account_id"): # Grouped by home tenant's id
647+
{ # These are minimal amount of non-tenant-specific account info
648+
"home_account_id": a.get("home_account_id"),
649+
"environment": a.get("environment"),
650+
"username": a.get("username"),
651+
652+
# The following fields for backward compatibility, for now
653+
"authority_type": a.get("authority_type"),
654+
"local_account_id": a.get("local_account_id"), # Tenant-specific
655+
"realm": a.get("realm"), # Tenant-specific
656+
}
657+
for a in self.token_cache.find(
658+
TokenCache.CredentialType.ACCOUNT,
659+
query={"environment": environment})
647660
if a["authority_type"] in (
648-
TokenCache.AuthorityType.ADFS, TokenCache.AuthorityType.MSSTS)]
661+
TokenCache.AuthorityType.ADFS, TokenCache.AuthorityType.MSSTS)
662+
}
663+
return list(grouped_accounts.values())
649664

650665
def _get_authority_aliases(self, instance):
651666
if not self.authority_groups:

tests/test_application.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,42 @@ def mock_post(url, headers=None, *args, **kwargs):
555555
result = self.app.acquire_token_on_behalf_of("assertion", ["s"], post=mock_post)
556556
self.assertEqual(at, result.get("access_token"))
557557

558+
559+
class TestClientApplicationWillGroupAccounts(unittest.TestCase):
560+
def test_get_accounts(self):
561+
client_id = "my_app"
562+
scopes = ["scope_1", "scope_2"]
563+
environment = "login.microsoftonline.com"
564+
uid = "home_oid"
565+
utid = "home_tenant_guid"
566+
username = "Jane Doe"
567+
cache = msal.SerializableTokenCache()
568+
for tenant in ["contoso", "fabrikam"]:
569+
cache.add({
570+
"client_id": client_id,
571+
"scope": scopes,
572+
"token_endpoint":
573+
"https://{}/{}/oauth2/v2.0/token".format(environment, tenant),
574+
"response": TokenCacheTestCase.build_response(
575+
uid=uid, utid=utid, access_token="at", refresh_token="rt",
576+
id_token=TokenCacheTestCase.build_id_token(
577+
aud=client_id,
578+
sub="oid_in_" + tenant,
579+
preferred_username=username,
580+
),
581+
),
582+
})
583+
app = ClientApplication(
584+
client_id,
585+
authority="https://{}/common".format(environment),
586+
token_cache=cache)
587+
accounts = app.get_accounts()
588+
self.assertEqual(1, len(accounts), "Should return one grouped account")
589+
account = accounts[0]
590+
self.assertEqual("{}.{}".format(uid, utid), account["home_account_id"])
591+
self.assertEqual(environment, account["environment"])
592+
self.assertEqual(username, account["username"])
593+
self.assertIn("authority_type", account, "Backward compatibility")
594+
self.assertIn("local_account_id", account, "Backward compatibility")
595+
self.assertIn("realm", account, "Backward compatibility")
596+

0 commit comments

Comments
 (0)