Skip to content

Commit f183da5

Browse files
committed
Supports GUID/scope and https://contoso.com//scope
1 parent 3f61c26 commit f183da5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

msal/cloudshell.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ def _scope_to_resource(scope): # This is an experimental reasonable-effort appr
3232
if scope.startswith(a):
3333
return a
3434
u = urlparse(scope)
35+
if not u.scheme and not u.netloc: # Typically the "GUID/scope" case
36+
return u.path.split("/")[0]
3537
if u.scheme:
36-
return "{}://{}".format(u.scheme, u.netloc)
38+
trailer = ( # https://learn.microsoft.com/en-us/entra/identity-platform/scopes-oidc#trailing-slash-and-default
39+
"/" if u.path.startswith("//") else "")
40+
return "{}://{}{}".format(u.scheme, u.netloc, trailer)
3741
return scope # There is no much else we can do here
3842

3943

tests/test_cloudshell.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from msal.cloudshell import _scope_to_resource
3+
4+
class TestScopeToResource(unittest.TestCase):
5+
6+
def test_expected_behaviors(self):
7+
for scope, expected_resource in {
8+
"https://analysis.windows.net/powerbi/api/foo":
9+
"https://analysis.windows.net/powerbi/api", # A special case
10+
"https://pas.windows.net/CheckMyAccess/Linux/.default":
11+
"https://pas.windows.net/CheckMyAccess/Linux/.default", # Special case
12+
"https://double-slash.com//scope": "https://double-slash.com/",
13+
"https://single-slash.com/scope": "https://single-slash.com",
14+
"guid/some/scope": "guid",
15+
"797f4846-ba00-4fd7-ba43-dac1f8f63013/.default": # Realistic GUID
16+
"797f4846-ba00-4fd7-ba43-dac1f8f63013"
17+
}.items():
18+
self.assertEqual(_scope_to_resource(scope), expected_resource)
19+
20+
if __name__ == '__main__':
21+
unittest.main()

0 commit comments

Comments
 (0)