Changeset View
Changeset View
Standalone View
Standalone View
swh/auth/keycloak.py
# Copyright (C) 2020-2021 The Software Heritage developers | # Copyright (C) 2020-2021 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 | ||||
import json | |||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
from urllib.parse import urlencode | from urllib.parse import urlencode | ||||
from keycloak import KeycloakOpenID | from keycloak import KeycloakOpenID | ||||
# The next import is required to allow callers to catch on their own term the following | # The next import is required to allow callers to catch on their own term the following | ||||
# exception | # exception | ||||
from keycloak.exceptions import KeycloakError # noqa | from keycloak.exceptions import KeycloakError # noqa | ||||
▲ Show 20 Lines • Show All 191 Lines • ▼ Show 20 Lines | def from_configfile(cls, **kwargs: Any) -> "KeycloakOpenIDConnect": | ||||
kwargs: kwargs passed to instantiation call | kwargs: kwargs passed to instantiation call | ||||
Returns: | Returns: | ||||
the KeycloakOpenIDConnect instance | the KeycloakOpenIDConnect instance | ||||
""" | """ | ||||
config = dict(load_from_envvar()).get("keycloak", {}) | config = dict(load_from_envvar()).get("keycloak", {}) | ||||
config.update({k: v for k, v in kwargs.items() if v is not None}) | config.update({k: v for k, v in kwargs.items() if v is not None}) | ||||
return cls.from_config(keycloak=config) | return cls.from_config(keycloak=config) | ||||
def keycloak_error_message(keycloak_error: KeycloakError) -> str: | |||||
"""Transform a keycloak exception into an error message. | |||||
""" | |||||
msg_dict = json.loads(keycloak_error.error_message.decode()) | |||||
error_msg = msg_dict["error"] | |||||
error_desc = msg_dict.get("error_description") | |||||
if error_desc: | |||||
error_msg = f"{error_msg}: {error_desc}" | |||||
return error_msg |