From 9f991a846673d3bf98753f208260d089f222425b Mon Sep 17 00:00:00 2001 From: jiasli <4003950+jiasli@users.noreply.github.com> Date: Wed, 1 Nov 2023 09:58:07 +0800 Subject: [PATCH] expires_on --- .../cli/command_modules/profile/_help.py | 4 ++++ .../cli/command_modules/profile/custom.py | 2 +- .../tests/latest/test_profile_custom.py | 21 +++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/profile/_help.py b/src/azure-cli/azure/cli/command_modules/profile/_help.py index 08cfa5588ee..df903840b9e 100644 --- a/src/azure-cli/azure/cli/command_modules/profile/_help.py +++ b/src/azure-cli/azure/cli/command_modules/profile/_help.py @@ -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: > diff --git a/src/azure-cli/azure/cli/command_modules/profile/custom.py b/src/azure-cli/azure/cli/command_modules/profile/custom.py index 727232982da..a962baf98d5 100644 --- a/src/azure-cli/azure/cli/command_modules/profile/custom.py +++ b/src/azure-cli/azure/cli/command_modules/profile/custom.py @@ -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 } diff --git a/src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py b/src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py index 1183cbb0849..8388ff70463 100644 --- a/src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py +++ b/src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py @@ -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) @@ -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' } @@ -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) @@ -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)