提供安全cookie身份验证的夸脱扩展
quart-auth的Python项目详细描述
Quart Auth是Quart提供安全cookie的扩展 身份验证(会话管理)。它允许会话 登录、验证和注销。在
使用
要在Quart应用程序中使用Quart Auth,必须创建AuthManager并 用应用程序初始化它
app=Quart(__name__)AuthManager(app)
或者通过工厂模式
^{pr2}$此外,您还需要配置Quart Auth,默认为 最安全的。至少你需要设置密钥
app.secret_key="secret key"# Do not use this key
你可以通过
>>>importsecrets>>>secrets.token_urlsafe(16)
Tou可能还需要禁用安全cookies才能在开发中使用,请参阅 配置如下。在
初始化AuthManager后,可以使用login_required 函数来装饰只能由访问的路由 经过身份验证的用户
fromquart_authimportlogin_required@app.route("/")@login_requiredasyncdefrestricted_route():...
如果没有用户登录,则引发Unauthorized异常。为了抓住它, 安装错误处理程序
@app.errorhandler(Unauthorized)asyncdefredirect_to_login(*_:Exception)->ResponseReturnValue:returnredirect(url_for("login"))
还可以使用login_user和logout_user函数来 启动和结束特定AuthenticatedUser实例的会话
fromquart_authimportAuthUser,login_user,logout_user@app.route("/login")asyncdeflogin():# Check Credentials here, e.g. username & password....# We'll assume the user has an identifying ID equal to 2login_user(AuthUser(2))...@app.route("/logout")asyncdeflogout():logout_user()...
用户(无论是否经过身份验证)都可以通过全局 current_user,包括在模板中
fromquartimportrender_template_stringfromquart_authimportcurrent_user@app.route("/")asyncdefuser():returnawaitrender_template_string("{{ current_user.is_authenticated }}")
贡献
Quart Auth是在GitLab上开发的。非常欢迎你来 打开issues或 提议merge requests。在
测试
最好的方法是用毒物检测
$ pip install tox $ tox
这将检查代码样式并运行测试。在
帮助
这个自述是最好的开始,然后尝试打开 issue。在
- 项目
标签: