Skip to content

Commit b6b4408

Browse files
committed
Add 'show_claims' argument to 'get_access_token' command
1 parent 87dec2c commit b6b4408

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/azure-cli/azure/cli/command_modules/profile/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ def load_arguments(self, command):
7777
c.argument('resource', options_list=['--resource'], help='Azure resource endpoints in AAD v1.0.')
7878
c.argument('scopes', options_list=['--scope'], nargs='*', help='Space-separated AAD scopes in AAD v2.0. Default to Azure Resource Manager.')
7979
c.argument('tenant', options_list=['--tenant', '-t'], help='Tenant ID for which the token is acquired. Only available for user and service principal account, not for MSI or Cloud Shell account')
80-
80+
c.argument('show_claims', help='Show the decoded claims of the token.')
8181

8282
COMMAND_LOADER_CLS = ProfileCommandsLoader

src/azure-cli/azure/cli/command_modules/profile/_help.py

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
- name: Get an access token to use with MS Graph API
8989
text: >
9090
az account get-access-token --resource-type ms-graph
91+
- name: Show the decoded claims of the token
92+
text: >
93+
az account get-access-token --show-claims
9194
"""
9295

9396
helps['self-test'] = """

src/azure-cli/azure/cli/command_modules/profile/custom.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def show_subscription(cmd, subscription=None):
5353
return profile.get_subscription(subscription)
5454

5555

56-
def get_access_token(cmd, subscription=None, resource=None, scopes=None, resource_type=None, tenant=None):
56+
def get_access_token(cmd, subscription=None, resource=None, scopes=None, resource_type=None, tenant=None, show_claims=False):
5757
"""
5858
get AAD token to access to a specified resource.
5959
Use 'az cloud show' command for other Azure resources
@@ -73,9 +73,15 @@ def get_access_token(cmd, subscription=None, resource=None, scopes=None, resourc
7373
'expiresOn': creds[2]['expiresOn'],
7474
'tenant': tenant
7575
}
76+
7677
if subscription:
7778
result['subscription'] = subscription
7879

80+
if show_claims:
81+
import jwt
82+
decoded = jwt.decode(creds[1], algorithms=['RS256'], options={'verify_signature': False})
83+
result['claims'] = decoded
84+
7985
return result
8086

8187

src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py

+16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ def test_get_raw_token(self, get_raw_token_mock):
8787
self.assertEqual(result, expected_result)
8888
get_raw_token_mock.assert_called_with(mock.ANY, None, None, None, tenant_id)
8989

90+
# test get token with decoded claims
91+
test_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwczovL2dyYXBoLm1pY3Jvc29mdC5jb20vIiwibmFtZSI6IlRlc3QgVG9rZW4ifQ.redacted"
92+
get_raw_token_mock.return_value = (('bearer', test_token, token_entry), None, 'tenant123')
93+
result = get_access_token(cmd, show_claims=True)
94+
expected_result = {
95+
'tokenType': 'bearer',
96+
'accessToken': test_token,
97+
'expires_on': timestamp,
98+
'expiresOn': datetime_local,
99+
'tenant': 'tenant123',
100+
'claims': {'aud': 'https://graph.microsoft.com/', 'name': 'Test Token'}
101+
}
102+
103+
self.assertEqual(result, expected_result)
104+
get_raw_token_mock.assert_called_with(mock.ANY, None, None, None, None)
105+
90106
@mock.patch('azure.cli.command_modules.profile.custom.Profile', autospec=True)
91107
def test_get_login(self, profile_mock):
92108
invoked = []

0 commit comments

Comments
 (0)