首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flask-login和LDAP

Flask-login和LDAP
EN

Stack Overflow用户
提问于 2012-11-09 17:41:24
回答 4查看 13.9K关注 0票数 8

我正在开发一个使用flask框架作为后端的need应用程序,我需要提供身份验证。

由于这是一个在我们的本地域上使用的内部应用程序,因此我选择使用他们已经存在的域凭据来验证用户。

我使用的方法是来自pywin32win32security.LogonUser,它返回一个成功登录的句柄。

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

它说我应该提供一个可以用来重新加载用户的id,但是我没有数据库或持久存储来提供这个映射,因为我只对检查用户是否通过身份验证感兴趣。

我的User类如下所示:

代码语言:javascript
复制
class User(flask_login.UserMixin):
    def __init__(self,username):
        self.username = username
        self.id = ??? 

要使用什么id,这个id如何映射回这个实例?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-11-10 00:30:03

Flask-login不依赖于用户,也不需要任何特定的后端。您必须表示user对象并返回一个id。请看这篇文章的例子

flask-login: can't understand how it works

票数 6
EN

Stack Overflow用户

发布于 2012-12-15 00:35:51

您可以在python中使用LDAP模块完成此操作:

代码语言:javascript
复制
LDAP_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
票数 11
EN

Stack Overflow用户

发布于 2019-01-02 06:02:18

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

代码语言:javascript
复制
from 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')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13305503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档