Skip to content

Commit 405b4b8

Browse files
authored
Merge pull request #530 from AzureAD/allow-client_id-as-scope
Allow using client_id as scope. Needed by B2C.
2 parents 48954fc + 94561cd commit 405b4b8

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

msal/application.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,9 @@ def _decorate_scope(
588588
raise ValueError(
589589
"API does not accept {} value as user-provided scopes".format(
590590
reserved_scope))
591-
if self.client_id in scope_set:
592-
if len(scope_set) > 1:
593-
# We make developers pass their client id, so that they can express
594-
# the intent that they want the token for themselves (their own
595-
# app).
596-
# If we do not restrict them to passing only client id then they
597-
# could write code where they expect an id token but end up getting
598-
# access_token.
599-
raise ValueError("Client Id can only be provided as a single scope")
600-
decorated = set(reserved_scope) # Make a writable copy
601-
else:
602-
decorated = scope_set | reserved_scope
591+
592+
# client_id can also be used as a scope in B2C
593+
decorated = scope_set | reserved_scope
603594
decorated -= self._exclude_scopes
604595
return list(decorated)
605596

tests/test_application.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,18 @@ def test_organizations_authority_should_emit_warnning(self):
625625
self._test_certain_authority_should_emit_warnning(
626626
authority="https://login.microsoftonline.com/organizations")
627627

628+
629+
class TestScopeDecoration(unittest.TestCase):
630+
def _test_client_id_should_be_a_valid_scope(self, client_id, other_scopes):
631+
# B2C needs this https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens#openid-connect-scopes
632+
reserved_scope = ['openid', 'profile', 'offline_access']
633+
scopes_to_use = [client_id] + other_scopes
634+
self.assertEqual(
635+
set(ClientApplication(client_id)._decorate_scope(scopes_to_use)),
636+
set(scopes_to_use + reserved_scope),
637+
"Scope decoration should return input scopes plus reserved scopes")
638+
639+
def test_client_id_should_be_a_valid_scope(self):
640+
self._test_client_id_should_be_a_valid_scope("client_id", [])
641+
self._test_client_id_should_be_a_valid_scope("client_id", ["foo"])
642+

tests/test_e2e.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,18 @@ def test_b2c_acquire_token_by_ropc(self):
884884
scope=config["scopes"],
885885
)
886886

887+
def test_b2c_allows_using_client_id_as_scope(self):
888+
# See also https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens#openid-connect-scopes
889+
config = self.get_lab_app_object(azureenvironment="azureb2ccloud")
890+
config["scopes"] = [config["appId"]]
891+
self._test_username_password(
892+
authority=self._build_b2c_authority("B2C_1_ROPC_Auth"),
893+
client_id=config["appId"],
894+
username="[email protected]",
895+
password=self.get_lab_user_secret("msidlabb2c"),
896+
scope=config["scopes"],
897+
)
898+
887899

888900
class WorldWideRegionalEndpointTestCase(LabBasedTestCase):
889901
region = "westus"

0 commit comments

Comments
 (0)