Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/auth/keycloak_mock.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from copy import copy | from copy import copy | ||||
from unittest.mock import Mock | from unittest.mock import Mock | ||||
from django.conf import settings | |||||
from django.utils import timezone | from django.utils import timezone | ||||
from swh.web.auth.keycloak import KeycloakOpenIDConnect | from swh.web.auth.keycloak import KeycloakOpenIDConnect | ||||
from swh.web.auth.utils import OIDC_SWH_WEB_CLIENT_ID | |||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
from .sample_data import oidc_profile, realm_public_key, userinfo | from .sample_data import oidc_profile, realm_public_key, userinfo | ||||
class KeycloackOpenIDConnectMock(KeycloakOpenIDConnect): | class KeycloackOpenIDConnectMock(KeycloakOpenIDConnect): | ||||
def __init__(self, auth_success=True): | def __init__(self, auth_success=True): | ||||
swhweb_config = get_config() | swhweb_config = get_config() | ||||
super().__init__(swhweb_config['keycloak']['server_url'], | super().__init__(swhweb_config['keycloak']['server_url'], | ||||
swhweb_config['keycloak']['realm_name'], | swhweb_config['keycloak']['realm_name'], | ||||
settings.OIDC_SWH_WEB_CLIENT_ID) | OIDC_SWH_WEB_CLIENT_ID) | ||||
self._keycloak.public_key = lambda: realm_public_key | self._keycloak.public_key = lambda: realm_public_key | ||||
self._keycloak.well_know = lambda: { | self._keycloak.well_know = lambda: { | ||||
'issuer': f'{self.server_url}realms/{self.realm_name}', | 'issuer': f'{self.server_url}realms/{self.realm_name}', | ||||
'authorization_endpoint': (f'{self.server_url}realms/' | 'authorization_endpoint': (f'{self.server_url}realms/' | ||||
f'{self.realm_name}/protocol/' | f'{self.realm_name}/protocol/' | ||||
'openid-connect/auth'), | 'openid-connect/auth'), | ||||
'token_endpoint': (f'{self.server_url}realms/{self.realm_name}/' | 'token_endpoint': (f'{self.server_url}realms/{self.realm_name}/' | ||||
'protocol/openid-connect/token'), | 'protocol/openid-connect/token'), | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |