Ejemplo n.º 1
0
    def test_credscache_new_token_added_by_adal(self, mock_adal_auth_context, _, mock_open_for_write, mock_read_file):  # pylint: disable=line-too-long
        token_entry2 = {
            "accessToken": "new token",
            "tokenType": "Bearer",
            "userId": self.user1
        }

        def acquire_token_side_effect(*args):  # pylint: disable=unused-argument
            creds_cache.adal_token_cache.has_state_changed = True
            return token_entry2

        def get_auth_context(authority, **kwargs):  # pylint: disable=unused-argument
            mock_adal_auth_context.cache = kwargs['cache']
            return mock_adal_auth_context

        mock_adal_auth_context.acquire_token.side_effect = acquire_token_side_effect
        mock_open_for_write.return_value = FileHandleStub()
        mock_read_file.return_value = [self.token_entry1]
        creds_cache = CredsCache(auth_ctx_factory=get_auth_context)

        #action
        mgmt_resource = 'https://management.core.windows.net/'
        token_type, token = creds_cache.retrieve_token_for_user(
            self.user1, self.tenant_id, mgmt_resource)
        mock_adal_auth_context.acquire_token.assert_called_once_with(
            'https://management.core.windows.net/', self.user1, mock.ANY)

        #assert
        mock_open_for_write.assert_called_with(mock.ANY, 'w+')
        self.assertEqual(token, 'new token')
        self.assertEqual(token_type, token_entry2['tokenType'])
Ejemplo n.º 2
0
    def test_credscache_new_token_added_by_adal(self, mock_adal_auth_context, _, mock_open_for_write, mock_read_file): # pylint: disable=line-too-long
        token_entry2 = {
            "accessToken": "new token",
            "tokenType": "Bearer",
            "userId": self.user1
        }
        def acquire_token_side_effect(*args): # pylint: disable=unused-argument
            creds_cache.adal_token_cache.has_state_changed = True
            return token_entry2
        def get_auth_context(authority, **kwargs): # pylint: disable=unused-argument
            mock_adal_auth_context.cache = kwargs['cache']
            return mock_adal_auth_context

        mock_adal_auth_context.acquire_token.side_effect = acquire_token_side_effect
        mock_open_for_write.return_value = FileHandleStub()
        mock_read_file.return_value = [self.token_entry1]
        creds_cache = CredsCache(auth_ctx_factory=get_auth_context)

        #action
        mgmt_resource = 'https://management.core.windows.net/'
        token_type, token = creds_cache.retrieve_token_for_user(self.user1, self.tenant_id,
                                                                mgmt_resource)
        mock_adal_auth_context.acquire_token.assert_called_once_with(
            'https://management.core.windows.net/',
            self.user1,
            mock.ANY)

        #assert
        mock_open_for_write.assert_called_with(mock.ANY, 'w+')
        self.assertEqual(token, 'new token')
        self.assertEqual(token_type, token_entry2['tokenType'])
Ejemplo n.º 3
0
def _get_service_token():
    profile = Profile()
    credsCache = CredsCache()
    account = profile.get_subscription()
    user_name = account['user']['name']
    tenant = account['tenantId']
    scheme, token = credsCache.retrieve_token_for_user(user_name, tenant, SERVICE_RESOURCE_ID)
    service_token = "{} {}".format(scheme, token)

    return service_token
    
Ejemplo n.º 4
0
def _get_service_token():
    profile = Profile()
    credsCache = CredsCache()
    account = profile.get_subscription()

    user_name = account['user']['name']
    tenant = account['tenantId']

    if account['user']['type'] == _SERVICE_PRINCIPAL:
        scheme, token = credsCache.retrieve_token_for_service_principal(user_name, SERVICE_RESOURCE_ID)
    else:
        scheme, token = credsCache.retrieve_token_for_user(user_name, tenant, SERVICE_RESOURCE_ID)

    service_token = "{} {}".format(scheme, token)
    return service_token