使用 ldap3 验证 django 应用程序
authentication django app with ldap3
我的 Django 应用程序中的用户身份验证有问题。我正在使用 Python 3.5 和 Django 1.10。
我编写了简单的绑定配置来检查用户是否是来自数据库的真实用户:
username = request.POST['username']
password = request.POST['password']
server = Server(LDAP_AUTH_URL)
c = Connection(server, user=LDAP_AUTH_CONNECTION_USERNAME, password=LDAP_AUTH_CONNECTION_PASSWORD)
c.open()
c.bind()
if c.bind():
user_search_filter = '(uid={})'.format(username)
c.search(search_base=LDAP_AUTH_SEARCH_BASE,
search_filter=user_search_filter,
search_scope=SUBTREE)
username = c.response[0]['dn']
if c.rebind(user=username, password=password):
return HttpResponseRedirect(next)
但现在我不知道如何处理它,在 django 中我们当然有这样的东西:
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
return HttpResponseRedirect(next)
else:
return render(request, 'login.html', {'error_message': 'Your account has been disabled'})
else:
return render(request, 'login.html', {'error_message': 'Invalid login'})
但在这种情况下,我们必须在我们的基础中拥有用户帐户,而不是来自 ldap 授权。
所以我想在我通过 ldap 登录时获得 django 的授权,以便使用 "@login_required"[=12= 访问任何其他视图]
也许我对 Python >3 的 ldap 和 ldap3 的想法有问题。
有人可以帮助我或给我有用的链接吗?
用户授权的拆分管理。
增加了检查ldap授权的功能。说它将 return True/False
通过提供的凭据。
def ldap_auth(login, password):
...
return c.bind()
因此在 auth 视图中,您现在需要针对 ldap 和非 ldap 用户的两个不同条件:
# if it is ldap user check ldap_auth
# if user doesn't exist locally you could create it manually in case of successful auth
else:
user = authenticate(username=username, password=password)
在前端,您需要以某种方式指定它是否是 ldap 用户。或者你可以更进一步,根据几个检查添加一些逻辑:尝试在普通用户中找到用户,如果没有结果 - 尝试通过 ldap 登录。
为 LDAP 身份验证添加一个 django 身份验证后端,例如:
/users/auth/backend.py
并在您的 settings.py 中配置此后端,以便 django 将其用于所有登录调用。
并添加 authenticate()
实现 LDAP 身份验证的方法。您还需要实现 get_user()
方法。
建议您在首次登录时或在您的应用程序中首次在活动目录中搜索用户时从 LDAP 同步用户帐户,并为您的应用程序维护用户模型。
可以在此处找到有关设置身份验证后端和同步用户的更多信息:
我的 Django 应用程序中的用户身份验证有问题。我正在使用 Python 3.5 和 Django 1.10。
我编写了简单的绑定配置来检查用户是否是来自数据库的真实用户:
username = request.POST['username']
password = request.POST['password']
server = Server(LDAP_AUTH_URL)
c = Connection(server, user=LDAP_AUTH_CONNECTION_USERNAME, password=LDAP_AUTH_CONNECTION_PASSWORD)
c.open()
c.bind()
if c.bind():
user_search_filter = '(uid={})'.format(username)
c.search(search_base=LDAP_AUTH_SEARCH_BASE,
search_filter=user_search_filter,
search_scope=SUBTREE)
username = c.response[0]['dn']
if c.rebind(user=username, password=password):
return HttpResponseRedirect(next)
但现在我不知道如何处理它,在 django 中我们当然有这样的东西:
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
return HttpResponseRedirect(next)
else:
return render(request, 'login.html', {'error_message': 'Your account has been disabled'})
else:
return render(request, 'login.html', {'error_message': 'Invalid login'})
但在这种情况下,我们必须在我们的基础中拥有用户帐户,而不是来自 ldap 授权。
所以我想在我通过 ldap 登录时获得 django 的授权,以便使用 "@login_required"[=12= 访问任何其他视图]
也许我对 Python >3 的 ldap 和 ldap3 的想法有问题。
有人可以帮助我或给我有用的链接吗?
用户授权的拆分管理。
增加了检查ldap授权的功能。说它将 return True/False
通过提供的凭据。
def ldap_auth(login, password):
...
return c.bind()
因此在 auth 视图中,您现在需要针对 ldap 和非 ldap 用户的两个不同条件:
# if it is ldap user check ldap_auth
# if user doesn't exist locally you could create it manually in case of successful auth
else:
user = authenticate(username=username, password=password)
在前端,您需要以某种方式指定它是否是 ldap 用户。或者你可以更进一步,根据几个检查添加一些逻辑:尝试在普通用户中找到用户,如果没有结果 - 尝试通过 ldap 登录。
为 LDAP 身份验证添加一个 django 身份验证后端,例如:
/users/auth/backend.py
并在您的 settings.py 中配置此后端,以便 django 将其用于所有登录调用。
并添加 authenticate()
实现 LDAP 身份验证的方法。您还需要实现 get_user()
方法。
建议您在首次登录时或在您的应用程序中首次在活动目录中搜索用户时从 LDAP 同步用户帐户,并为您的应用程序维护用户模型。
可以在此处找到有关设置身份验证后端和同步用户的更多信息: