Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/azure-cli/azure/cli/command_modules/profile/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
long-summary: >
The token will be valid for at least 5 minutes with the maximum at 60 minutes.
If the subscription argument isn't specified, the current account is used.


In the output, `expires_on` represents a POSIX timestamp and `expiresOn` represents a local datetime.
It is recommended for downstream applications to use `expires_on` because it is in UTC.
examples:
- name: Get an access token for the current account
text: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_access_token(cmd, subscription=None, resource=None, scopes=None, resourc
result = {
'tokenType': creds[0],
'accessToken': creds[1],
# 'expires_on': creds[2].get('expires_on', None),
'expires_on': creds[2]['expires_on'],
'expiresOn': creds[2]['expiresOn'],
'tenant': tenant
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def test_get_raw_token(self, get_raw_token_mock):
cmd = mock.MagicMock()
cmd.cli_ctx = DummyCli()

get_raw_token_mock.return_value = (['bearer', 'token123', {'expiresOn': '2100-01-01'}], 'sub123', 'tenant123')
timestamp = 1695270561
datetime_local = '2023-09-21 04:29:21.000000'

token_entry = {
'accessToken': 'token123',
'expires_on': timestamp,
'expiresOn': datetime_local
}
get_raw_token_mock.return_value = (('bearer', 'token123', token_entry), 'sub123', 'tenant123')

result = get_access_token(cmd)

Expand All @@ -46,7 +54,8 @@ def test_get_raw_token(self, get_raw_token_mock):
expected_result = {
'tokenType': 'bearer',
'accessToken': 'token123',
'expiresOn': '2100-01-01',
'expires_on': timestamp,
'expiresOn': datetime_local,
'subscription': 'sub123',
'tenant': 'tenant123'
}
Expand All @@ -55,8 +64,7 @@ def test_get_raw_token(self, get_raw_token_mock):
# assert it takes customized resource, subscription
resource = 'https://graph.microsoft.com/'
subscription_id = '00000001-0000-0000-0000-000000000000'
get_raw_token_mock.return_value = (['bearer', 'token123', {'expiresOn': '2100-01-01'}], subscription_id,
'tenant123')
get_raw_token_mock.return_value = (('bearer', 'token123', token_entry), subscription_id, 'tenant123')
result = get_access_token(cmd, subscription=subscription_id, resource=resource)
get_raw_token_mock.assert_called_with(mock.ANY, resource, None, subscription_id, None)

Expand All @@ -67,12 +75,13 @@ def test_get_raw_token(self, get_raw_token_mock):

# test get token with tenant
tenant_id = '00000000-0000-0000-0000-000000000000'
get_raw_token_mock.return_value = (['bearer', 'token123', {'expiresOn': '2100-01-01'}], None, tenant_id)
get_raw_token_mock.return_value = (('bearer', 'token123', token_entry), None, tenant_id)
result = get_access_token(cmd, tenant=tenant_id)
expected_result = {
'tokenType': 'bearer',
'accessToken': 'token123',
'expiresOn': '2100-01-01',
'expires_on': timestamp,
'expiresOn': datetime_local,
'tenant': tenant_id
}
self.assertEqual(result, expected_result)
Expand Down