关于python:Flask登录和LDAP 您所在的位置:网站首页 python连接ldap 关于python:Flask登录和LDAP

关于python:Flask登录和LDAP

2023-03-23 23:34| 来源: 网络整理| 查看: 265

我正在使用Flask框架作为后端开发Web应用程序,我需要提供身份验证。

由于这是一个内部应用程序,可在我们的本地域中使用,因此我选择使用其已经存在的域凭据对用户进行身份验证。

我使用的方法是pywin32中的win32security.LogonUser,它在成功登录后返回一个句柄。

我试图了解flask-login的工作原理,但是@login_manager.user_loader回调使我感到困惑。

它说我应该提供一个可用于重新加载用户的ID,但是我没有数据库或持久性存储可提供此映射,因为我只在检查用户是否通过身份验证时很有趣。

我的用户类如下:

1234class User(flask_login.UserMixin):     def __init__(self,username):         self.username = username         self.id = ???

id的用法以及此ID如何映射回该实例?

相关讨论 如果使用的是LDAP后端,则可能应使用用户DN作为ID。

您可以使用LDAP模块在python中进行操作:

12345678910LDAP_SERVER ="yourldapserver" LDAP_PORT = 390033 # your port import ldap def login(email, password):     ld = ldap.open(LDAP_SERVER, port=LDAP_PORT)     try:         ld.simple_bind_s(email, password)     except ldap.INVALID_CREDENTIALS:         return False     return True

Flask登录不依赖于用户,也不需要任何特定的后端。您必须代表用户对象并返回一个ID。例如参见此帖子

烧瓶登录:无法理解其工作方式

使用ldap3进行烧瓶登录的简单示例。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081from flask_ldap3_login.forms import LDAPLoginForm from flask_ldap3_login import LDAP3LoginManager, AuthenticationResponse from flask_login import LoginManager, login_user, UserMixin, current_user app = Flask(__name__) app.config['SECRET_KEY'] = 'secret' app.config['DEBUG'] = 'True' # Setup LDAP Configuration Variables. Change these to your own settings. # Hostname of your LDAP Server app.config['LDAP_PORT'] = 636 # Hostname of your LDAP Server app.config['LDAP_HOST'] = 'ldap-name.com' app.config['LDAP_USE_SSL'] = True # Base DN of your directory app.config['LDAP_BASE_DN'] = 'dc=Hostname,dc=com' # Users DN to be prepended to the Base DN app.config['LDAP_USER_DN'] = 'ou=people' # Groups DN to be prepended to the Base DN app.config['LDAP_GROUP_DN'] = 'cn=ldap-groupname,ou=groups' # The RDN attribute for your user schema on LDAP app.config['LDAP_USER_RDN_ATTR'] = 'uid' # The Attribute you want users to authenticate to LDAP with. app.config['LDAP_USER_LOGIN_ATTR'] = 'uid' # The Username to bind to LDAP with app.config['LDAP_BIND_USER_DN'] = 'uid' # The Password to bind to LDAP with app.config['LDAP_BIND_USER_PASSWORD'] = 'pwd' login_manager = LoginManager(app)  # Setup a Flask-Login Manager ldap_manager = LDAP3LoginManager(app)  # Setup a LDAP3 Login Manager. # Create a dictionary to store the users in when they authenticate # This example stores users in memory. users = {} # Declare an Object Model for the user, and make it comply with the # flask-login UserMixin mixin. class User(UserMixin):  def __init__(self, dn, username, data):     self.dn = dn     self.username = username     self.data = data def __repr__(self):     return self.dn def get_id(self):     return self.dn # Declare a User Loader for Flask-Login. # Simply returns the User if it exists in our 'database', otherwise # returns None. @login_manager.user_loader def load_user(id):     if id in users:        return users[id]     return None # Declare The User Saver for Flask-Ldap3-Login # This method is called whenever a LDAPLoginForm() successfully validates. # Here you have to save the user, and return it so it can be used in the # login controller. @ldap_manager.save_user def save_user(dn, username, data, memberships):   user = User(dn, username, data)   users[dn] = user   return user,username @app.route('/', methods=['GET', 'POST']) def login():  # exists in LDAP.  form = LDAPLoginForm()  if form.validate_on_submit():         # Successfully logged in, We can now access the saved user object         # via form.user.         a = login         return redirect(url_for('mainpage'))     return render_template('login.html',form=form) else:     return render_template('error.html') 相关讨论 我仍然对如何将ID传递给login_manager感到困惑?我们什么时候调用load_user或save_user将ldaps信息传递给它?

self.id应该是唯一的字符串。 它可以是以下之一:

cn(在LDAP中唯一) sAMAccountName(在域中是唯一的,就像Unix登录一样) 邮件(多值,其中之一应该/可以是唯一的) ...

明智地选择一个。我更喜欢sAMAcountName作为自己的工作。 它要求您在ldap_bind之后执行LDAPSearch。

第一个未经身份验证的绑定(以查找DN)应与适用的用户进行,以避免信息泄漏(以防您被黑客入侵)。

Ldap连接是资源=>使用上下文管理器

12with ldap.open(LDAP_SERVER, port=LDAP_PORT) as ld:     # do the search/bind/search here


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有