Skip to content

Commit fcb44b4

Browse files
committed
supports LDAP
1 parent db2c845 commit fcb44b4

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

scripts/config.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ export API_TICKET_TOKEN='tca@public@2021'
140140
## ScmProxy
141141
export SCMPROXY_HOST="127.0.0.1"
142142
export SCMPROXY_PORT=8009
143+
144+
# LDAP相关配置
145+
export LDAP_ENABLE=${LDAP_ENABLE:-false} # 默认关闭,开启请设置为true
146+
export LDAP_SERVER="" # ldap服务器地址
147+
export LDAP_PORT=389 # ldap默认端口号
148+
export LDAP_BASE_DN="" # ldap基础DN ou=People,dc=example,dc=com
149+
export LDAP_USER_SEARCH_FILTER="(&(objectClass=inetOrgPerson)(uid=%s))" # 用户搜索过滤器

server/dockerconfs/.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ API_TICKET_TOKEN=tca@public@2021
154154
## ScmProxy
155155
SCMPROXY_HOST=0.0.0.0
156156
SCMPROXY_PORT=8009
157+
158+
# LDAP相关配置
159+
export LDAP_ENABLE=${LDAP_ENABLE:-false} # 默认关闭,开启请设置为true
160+
export LDAP_SERVER="" # ldap服务器地址
161+
export LDAP_PORT=389 # ldap默认端口号
162+
export LDAP_BASE_DN="" # ldap基础DN ou=People,dc=example,dc=com
163+
export LDAP_USER_SEARCH_FILTER="(&(objectClass=inetOrgPerson)(uid=%s))" # 用户搜索过滤器

server/dockerconfs/Dockerfile-common-mirror

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
55
echo 'deb http://mirrors.tencent.com/debian/ bullseye-updates main non-free contrib' >> /etc/apt/sources.list && \
66
echo 'deb http://mirrors.tencent.com/debian-security bullseye-security main non-free contrib' >> /etc/apt/sources.list
77

8-
ARG EXTRA_TOOLS="gnupg curl wget jq vim-tiny net-tools procps python3-dev default-libmysqlclient-dev locales inotify-tools gcc subversion git telnet iputils-ping vim openssh-client"
8+
ARG EXTRA_TOOLS="gnupg curl wget jq vim-tiny net-tools procps python3-dev default-libmysqlclient-dev locales inotify-tools gcc subversion git telnet iputils-ping vim openssh-client libsasl2-dev python-dev libldap2-dev libssl-dev"
99

1010
RUN set -ex && cd / \
1111
&& apt-get update \

server/projects/login/apps/settings/open_base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import os
2121
from os.path import join
2222

23+
import ldap
24+
from django_auth_ldap.config import LDAPSearch
25+
2326
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2427

2528
# SECURITY WARNING: keep the secret key used in production secret!
@@ -41,6 +44,34 @@
4144
'login',
4245
]
4346

47+
if os.environ.get("LDAP_ENABLE", False):
48+
# 代码不能覆盖全部 ldap 使用方式, 如果出现错误又不知道怎么配置, 看下面文档
49+
# https://django-auth-ldap.readthedocs.io/en/latest/example.html
50+
51+
AUTHENTICATION_BACKENDS = [
52+
'django_auth_ldap.backend.LDAPBackend',
53+
]
54+
LDAP_SERVER = os.environ.get("LDAP_SERVER")
55+
LDAP_PORT = os.environ.get("LDAP_PORT")
56+
LDAP_BASE_DN = os.environ.get("LDAP_BASE_DN")
57+
LDAP_USER_SEARCH_FILTER = os.environ.get("LDAP_USER_SEARCH_FILTER"),
58+
59+
60+
AUTH_LDAP_SERVER_URI = "ldap://%s:%s" % (LDAP_SERVER, LDAP_PORT)
61+
62+
AUTH_LDAP_USER_SEARCH = LDAPSearch(
63+
"%s" % LDAP_BASE_DN,
64+
ldap.SCOPE_SUBTREE,
65+
"%s" % LDAP_USER_SEARCH_FILTER
66+
)
67+
68+
# 这里的配置是将ldap中的字段映射到django的字段, 请按照实际情况修改
69+
AUTH_LDAP_USER_ATTR_MAP = {'nickname': 'givenName', 'uid': 'cn', 'mail': 'mail', 'mobile': 'phone'}
70+
71+
# 下面两个配置一般不需要更改,如果要改请了解清楚
72+
AUTH_LDAP_ALWAYS_UPDATE_USER = True
73+
AUTH_LDAP_CACHE_TIMEOUT = 600
74+
4475
MIDDLEWARE = [
4576
'django.middleware.security.SecurityMiddleware',
4677
'django.contrib.sessions.middleware.SessionMiddleware',

server/projects/login/login/apis/v3.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
from django.forms.models import model_to_dict
1919
from django.http import HttpResponse
2020
from django.shortcuts import redirect, get_object_or_404
21+
from django.contrib.auth import authenticate
2122
from rest_framework import filters, generics
2223
from rest_framework import status
23-
from rest_framework.exceptions import NotAuthenticated, ParseError
24+
from rest_framework.exceptions import NotAuthenticated, ParseError, ValidationError
2425
from rest_framework.permissions import IsAuthenticated
2526
from rest_framework.response import Response
2627
from rest_framework.throttling import ScopedRateThrottle
@@ -31,6 +32,7 @@
3132
from login import serializers
3233
from login.lib import cdcrypto as crypto
3334
from login.models import UserInfo, UserAuth
35+
from login.core import UserManager
3436

3537
logger = logging.getLogger(__name__)
3638

@@ -206,10 +208,20 @@ def post(self, request, *args, **kwargs):
206208
credential = data.get("password", "")
207209
params = {}
208210
logger.debug("Current Login User: %s" % identifier)
209-
auth = UserAuth.objects.filter(identifier=identifier,
210-
identity_type="oapassword",
211-
credential=crypto.encrypt(credential, settings.PASSWORD_KEY)).first()
211+
212+
auth = authenticate(username=identifier, password=credential)
213+
214+
# 判断账号是否存在,如果不存在就创建
215+
if not (auth and UserManager.get_or_create_account(identifier)):
216+
auth = False
217+
218+
if not auth:
219+
auth = UserAuth.objects.filter(identifier=identifier,
220+
identity_type="oapassword",
221+
credential=crypto.encrypt(credential, settings.PASSWORD_KEY)).first()
222+
212223
if auth:
224+
auth = UserAuth.objects.filter(user=identifier).first()
213225
serializer = self.get_serializer(data={"uid": auth.uid})
214226
serializer.is_valid(raise_exception=True)
215227
params["access_token"] = serializer.validated_data["access"]

server/projects/login/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ cryptography===42.0.4
2222

2323
# for exception log
2424
sentry-sdk==1.14.0
25+
26+
# ldap
27+
django-auth-ldap==4.1.0
28+
python-ldap==3.4.3

0 commit comments

Comments
 (0)