Skip to content

Commit b52e156

Browse files
author
lijianguo
committed
chore(passport): change ldap configuration
1 parent 4af5cd6 commit b52e156

3 files changed

Lines changed: 41 additions & 35 deletions

File tree

rootfs/api/serializers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import logging
55

6+
from django.conf import settings
67
from django.contrib.admin.models import LogEntry
78
from django.contrib.auth.forms import UserCreationForm
89
from django.contrib.auth.models import User
@@ -32,6 +33,9 @@ class Meta:
3233
"is_staff", "is_active", "is_superuser")
3334

3435
def update(self, instance, validated_data):
36+
if settings.LDAP_ENDPOINT:
37+
raise DryccException(
38+
"You cannot change user info when ldap is enabled.")
3539
if validated_data.get('username'):
3640
qs = User.objects.filter(username=validated_data.get('username')).\
3741
exclude(username=instance.username)

rootfs/api/settings/production.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,40 @@
257257
conn_max_age=600)
258258
}
259259

260+
# Static files (CSS, JavaScript, Images)
261+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
262+
STATIC_URL = '/assets/'
263+
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'web', 'dist', 'assets'))
264+
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
265+
266+
# see: https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html?highlight=oidc.key#creating-rsa-private-key # noqa
267+
with open('/var/run/secrets/drycc/passport/oidc-rsa-private-key') as f:
268+
OIDC_RSA_PRIVATE_KEY = f.read()
269+
OAUTH2_PROVIDER = {
270+
"OIDC_ENABLED": True,
271+
"OIDC_RSA_PRIVATE_KEY": OIDC_RSA_PRIVATE_KEY,
272+
"OAUTH2_VALIDATOR_CLASS": "api.serializers.CustomOAuth2Validator",
273+
"PKCE_REQUIRED": False,
274+
"ALLOWED_REDIRECT_URI_SCHEMES": ["http", "https"],
275+
"ACCESS_TOKEN_EXPIRE_SECONDS": int(os.environ.get('ACCESS_TOKEN_EXPIRE_SECONDS', 30 * 86400)), # noqa
276+
"ID_TOKEN_EXPIRE_SECONDS": int(os.environ.get('ID_TOKEN_EXPIRE_SECONDS', 30 * 86400)), # noqa
277+
"AUTHORIZATION_CODE_EXPIRE_SECONDS": int(os.environ.get('AUTHORIZATION_CODE_EXPIRE_SECONDS', 600)), # noqa
278+
"CLIENT_SECRET_GENERATOR_LENGTH": int(os.environ.get('CLIENT_SECRET_GENERATOR_LENGTH', 64)), # noqa
279+
"REFRESH_TOKEN_EXPIRE_SECONDS": int(os.environ.get('REFRESH_TOKEN_EXPIRE_SECONDS', 60 * 86400)), # noqa
280+
"ROTATE_REFRESH_TOKEN": True,
281+
"SCOPES": {
282+
"profile": "Profile",
283+
"openid": "OpenID Connect scope",
284+
},
285+
"DEFAULT_SCOPES": ['openid', ],
286+
"DEFAULT_CODE_CHALLENGE_METHOD": 'S256',
287+
}
288+
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] = (
289+
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
290+
'rest_framework.authentication.SessionAuthentication',
291+
'rest_framework.authentication.BasicAuthentication',
292+
)
293+
260294
# LDAP settings taken from environment variables.
261295
LDAP_ENDPOINT = os.environ.get('LDAP_ENDPOINT', '')
262296
LDAP_BIND_DN = os.environ.get('LDAP_BIND_DN', '')
@@ -275,7 +309,6 @@
275309
# In order to debug LDAP configuration it is possible to enable
276310
# verbose logging from auth-ldap plugin:
277311
# https://pythonhosted.org/django-auth-ldap/logging.html
278-
279312
if LDAP_ENDPOINT:
280313
AUTHENTICATION_BACKENDS = ("django_auth_ldap.backend.LDAPBackend",) + AUTHENTICATION_BACKENDS # noqa
281314
AUTH_LDAP_SERVER_URI = LDAP_ENDPOINT
@@ -312,40 +345,6 @@
312345
AUTH_LDAP_FIND_GROUP_PERMS = True
313346
AUTH_LDAP_CACHE_GROUPS = False
314347

315-
# Static files (CSS, JavaScript, Images)
316-
# https://docs.djangoproject.com/en/2.2/howto/static-files/
317-
STATIC_URL = '/assets/'
318-
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'web', 'dist', 'assets'))
319-
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
320-
321-
# see: https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html?highlight=oidc.key#creating-rsa-private-key # noqa
322-
with open('/var/run/secrets/drycc/passport/oidc-rsa-private-key') as f:
323-
OIDC_RSA_PRIVATE_KEY = f.read()
324-
OAUTH2_PROVIDER = {
325-
"OIDC_ENABLED": True,
326-
"OIDC_RSA_PRIVATE_KEY": OIDC_RSA_PRIVATE_KEY,
327-
"OAUTH2_VALIDATOR_CLASS": "api.serializers.CustomOAuth2Validator",
328-
"PKCE_REQUIRED": False,
329-
"ALLOWED_REDIRECT_URI_SCHEMES": ["http", "https"],
330-
"ACCESS_TOKEN_EXPIRE_SECONDS": int(os.environ.get('ACCESS_TOKEN_EXPIRE_SECONDS', 30 * 86400)), # noqa
331-
"ID_TOKEN_EXPIRE_SECONDS": int(os.environ.get('ID_TOKEN_EXPIRE_SECONDS', 30 * 86400)), # noqa
332-
"AUTHORIZATION_CODE_EXPIRE_SECONDS": int(os.environ.get('AUTHORIZATION_CODE_EXPIRE_SECONDS', 600)), # noqa
333-
"CLIENT_SECRET_GENERATOR_LENGTH": int(os.environ.get('CLIENT_SECRET_GENERATOR_LENGTH', 64)), # noqa
334-
"REFRESH_TOKEN_EXPIRE_SECONDS": int(os.environ.get('REFRESH_TOKEN_EXPIRE_SECONDS', 60 * 86400)), # noqa
335-
"ROTATE_REFRESH_TOKEN": True,
336-
"SCOPES": {
337-
"profile": "Profile",
338-
"openid": "OpenID Connect scope",
339-
},
340-
"DEFAULT_SCOPES": ['openid', ],
341-
"DEFAULT_CODE_CHALLENGE_METHOD": 'S256',
342-
}
343-
REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] = (
344-
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
345-
'rest_framework.authentication.SessionAuthentication',
346-
'rest_framework.authentication.BasicAuthentication',
347-
)
348-
349348
EMAIL_HOST = os.environ.get('EMAIL_HOST', '')
350349
EMAIL_PORT = os.environ.get('EMAIL_PORT', '')
351350
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', '')

rootfs/api/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ def destroy(self, request, *args, **kwargs):
230230
class UserAccountPasswordView(ListViewSet):
231231

232232
def update(self, request, *args, **kwargs):
233+
if settings.LDAP_ENDPOINT:
234+
raise DryccException(
235+
"You cannot change user info when ldap is enabled.")
233236
if not request.data.get('new_password'):
234237
raise DryccException("new_password is a required field")
235238
if not request.data.get('password'):

0 commit comments

Comments
 (0)