2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-09-09 06:00:50 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import EncryptedField
|
2022-05-20 02:01:41 +00:00
|
|
|
|
2021-09-09 06:00:50 +00:00
|
|
|
__all__ = [
|
|
|
|
'OIDCSettingSerializer', 'KeycloakSettingSerializer',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CommonSettingSerializer(serializers.Serializer):
|
2023-02-23 08:38:44 +00:00
|
|
|
PREFIX_TITLE = _('OIDC')
|
2021-09-09 06:00:50 +00:00
|
|
|
# OpenID 公有配置参数 (version <= 1.5.8 或 version >= 1.5.8)
|
|
|
|
BASE_SITE_URL = serializers.CharField(
|
2022-06-29 06:56:17 +00:00
|
|
|
required=False, allow_null=True, allow_blank=True,
|
2024-05-22 09:51:45 +00:00
|
|
|
max_length=1024, label=_('Base site URL'),
|
|
|
|
help_text=_("The current site's URL is used to construct the callback address")
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_CLIENT_ID = serializers.CharField(
|
|
|
|
required=False, max_length=1024, label=_('Client Id')
|
|
|
|
)
|
2022-05-20 02:01:41 +00:00
|
|
|
AUTH_OPENID_CLIENT_SECRET = EncryptedField(
|
|
|
|
required=False, max_length=1024, label=_('Client Secret')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2022-04-29 04:54:15 +00:00
|
|
|
AUTH_OPENID_CLIENT_AUTH_METHOD = serializers.ChoiceField(
|
|
|
|
default='client_secret_basic',
|
|
|
|
choices=(
|
|
|
|
('client_secret_basic', 'Client Secret Basic'),
|
|
|
|
('client_secret_post', 'Client Secret Post')
|
|
|
|
),
|
2024-02-27 03:25:42 +00:00
|
|
|
label=_('Request method')
|
2022-04-29 04:54:15 +00:00
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
AUTH_OPENID_SHARE_SESSION = serializers.BooleanField(required=False, label=_('Share session'))
|
|
|
|
AUTH_OPENID_IGNORE_SSL_VERIFICATION = serializers.BooleanField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, label=_('Ignore SSL verification')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-06-08 02:06:14 +00:00
|
|
|
AUTH_OPENID_USER_ATTR_MAP = serializers.JSONField(
|
2024-02-26 08:53:40 +00:00
|
|
|
required=True, label=_('User attribute'),
|
2024-05-22 09:51:45 +00:00
|
|
|
help_text=_(
|
|
|
|
"User attribute mapping, where the `key` is the JumpServer user attribute name "
|
|
|
|
"and the `value` is the OIDC service user attribute name"
|
|
|
|
)
|
2022-05-31 07:39:49 +00:00
|
|
|
)
|
2022-12-13 07:30:08 +00:00
|
|
|
AUTH_OPENID_PKCE = serializers.BooleanField(required=False, label=_('Enable PKCE'))
|
|
|
|
AUTH_OPENID_CODE_CHALLENGE_METHOD = serializers.ChoiceField(
|
|
|
|
default='S256', label=_('Code challenge method'),
|
|
|
|
choices=(('S256', 'HS256'), ('plain', 'Plain'))
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KeycloakSettingSerializer(CommonSettingSerializer):
|
|
|
|
# OpenID 旧配置参数 (version <= 1.5.8 (discarded))
|
|
|
|
AUTH_OPENID_KEYCLOAK = serializers.BooleanField(
|
2024-05-22 09:51:45 +00:00
|
|
|
label=_("Use Keycloak"), required=False, default=False,
|
|
|
|
help_text=_(
|
|
|
|
"Use Keycloak as the OpenID Connect server, or use standard OpenID Connect Protocol"
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_SERVER_URL = serializers.CharField(
|
2024-02-26 08:53:40 +00:00
|
|
|
required=False, max_length=1024, label=_('Server')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_REALM_NAME = serializers.CharField(
|
|
|
|
required=False, max_length=1024, allow_null=True, label=_('Realm name')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class OIDCSettingSerializer(KeycloakSettingSerializer):
|
|
|
|
# OpenID 新配置参数 (version >= 1.5.9)
|
2024-05-22 09:51:45 +00:00
|
|
|
AUTH_OPENID = serializers.BooleanField(
|
|
|
|
required=False, label=_('OIDC'), help_text=_('OpenID Connect')
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
AUTH_OPENID_PROVIDER_ENDPOINT = serializers.CharField(
|
|
|
|
required=False, max_length=1024, label=_('Provider endpoint')
|
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_AUTHORIZATION_ENDPOINT = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('Authorization endpoint')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_TOKEN_ENDPOINT = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('Token endpoint')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_JWKS_ENDPOINT = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('JWKS endpoint')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_USERINFO_ENDPOINT = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('Userinfo endpoint')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_END_SESSION_ENDPOINT = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('End session endpoint')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_SIGNATURE_ALG = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, label=_('Signature algorithm')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_PROVIDER_SIGNATURE_KEY = serializers.CharField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, max_length=1024, allow_null=True, label=_('Signing key')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2024-05-22 09:51:45 +00:00
|
|
|
AUTH_OPENID_SCOPES = serializers.CharField(
|
|
|
|
required=False, max_length=1024, label=_('Scopes')
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
AUTH_OPENID_ID_TOKEN_MAX_AGE = serializers.IntegerField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, label=_('ID Token max age (s)')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
AUTH_OPENID_ID_TOKEN_INCLUDE_CLAIMS = serializers.BooleanField(
|
2024-02-27 03:25:42 +00:00
|
|
|
required=False, label=_('ID Token include claims')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2024-05-22 09:51:45 +00:00
|
|
|
AUTH_OPENID_USE_STATE = serializers.BooleanField(
|
|
|
|
required=False, label=_('Use state')
|
|
|
|
)
|
|
|
|
AUTH_OPENID_USE_NONCE = serializers.BooleanField(
|
|
|
|
required=False, label=_('Use nonce')
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
AUTH_OPENID_ALWAYS_UPDATE_USER = serializers.BooleanField(
|
|
|
|
required=False, label=_('Always update user')
|
|
|
|
)
|