def test_get_token(mock_requests): response = MagicMock() response.status_code = 200 response.json.return_value = json.loads( """{"access_token": "abc", "expires_in": 60}""") mock_requests.post.return_value = response access, expiration = basic_auth.get_token("https://corp.idp.net", "abc123", "my_scope") assert access == "abc" assert expiration == 60
def _refresh_credentials_basic(flyte_client): """ This function is used by the _handle_rpc_error() decorator, depending on the AUTH_MODE config object. This handler is meant for SDK use-cases of auth (like pyflyte, or when users call SDK functions that require access to Admin, like when waiting for another workflow to complete from within a task). This function uses basic auth, which means the credentials for basic auth must be present from wherever this code is running. :param flyte_client: RawSynchronousFlyteClient :return: """ auth_endpoints = _credentials_access.get_authorization_endpoints(flyte_client.url) token_endpoint = auth_endpoints.token_endpoint client_secret = _basic_auth.get_secret() _logging.debug("Basic authorization flow with client id {} scope {}".format(_CLIENT_ID.get(), _SCOPE.get())) authorization_header = _basic_auth.get_basic_authorization_header(_CLIENT_ID.get(), client_secret) token, expires_in = _basic_auth.get_token(token_endpoint, authorization_header, _SCOPE.get()) _logging.info("Retrieved new token, expires in {}".format(expires_in)) flyte_client.set_access_token(token)