def claims(audience, issuer): from framework.auth.jwt import format_access_token from framework.core.common import generate_random_id return format_access_token(user=generate_random_id(), machine_token=True, audiences=[audience], issuer=issuer, expiration_seconds=60 * 60, scopes=[generate_random_id() for i in range(3)])
def invalid_access_headers(app_settings, issuer, signing_key): from framework.auth.oauth import generate_oauth_headers from framework.auth.jwt import format_access_token, sign_token from framework.core.common import generate_random_id payload = format_access_token( user=generate_random_id(), machine_token=True, audiences=app_settings['audiences'], issuer=generate_random_id(), expiration_seconds=60 * 60, scopes=[generate_random_id() for i in range(3)]) token = sign_token(payload=payload, signing_key=signing_key) return generate_oauth_headers(access_token=get_token(token, app_settings))
def get_app_settings(env_folder=None, refresh=False): import os import json from framework.core.common import generate_random_id import logging global __app_settings__ folder = os.getenv('ENV_FOLDER', '/apps/settings/') if env_folder is not None: folder = env_folder if __app_settings__ is None or refresh: app_settings = dict() for root, dirs, files in os.walk(folder, topdown=True): dirs.sort() for file in files: local_path = os.path.join(folder, '{0}/{1}'.format(root, file)) try: with open(local_path, 'rt') as f: app_settings.update(json.load(f)) except json.JSONDecodeError as e: logging.error(local_path) raise e prefix = '' if app_settings.get('environment', 'test') == 'test': prefix = generate_random_id().split('-')[0] app_settings['prefix'] = prefix __app_settings__ = { k: format_setting_value(v) for k, v in app_settings.items() } return __app_settings__
def test_invalid_audience(signed_token, public_keys, issuers): from framework.auth.jwt import decode_token from jose.exceptions import JWTError with pytest.raises(JWTError): decode_token(token=signed_token, audiences=[generate_random_id()], issuers=issuers, auth_keys=public_keys['keys'])
def user_access_headers(app_settings, signing_key): from framework.auth.oauth import generate_oauth_headers from framework.auth.jwt import format_access_token, sign_token from framework.core.common import generate_random_id payload = format_access_token(user=generate_random_id(), machine_token=False, audiences=app_settings['audiences'], issuer=app_settings['issuers'][0], expiration_seconds=60 * 60, scopes=[]) token = sign_token(payload=payload, signing_key=signing_key) print(payload) return generate_oauth_headers(access_token=get_token(token, app_settings))
import pytest import responses from framework.core.common import generate_random_id issuer_list = [generate_random_id() for _ in range(3)] audience_list = [generate_random_id() for _ in range(3)] @pytest.fixture(scope='module') def issuers(): return issuer_list @pytest.fixture(scope='module') def audiences(): return audience_list @pytest.fixture(scope='module', params=issuer_list) def issuer(request): return request.param @pytest.fixture(scope='module', params=audience_list) def audience(request): return request.param @pytest.fixture( scope='session', )
def test_random_id_is_uuid(): from framework.core.common import generate_random_id import uuid assert uuid.UUID(generate_random_id())