一个实现olin认证的简单flask扩展
Flask-OlinAuth的Python项目详细描述
flask olinauth实现olinapps auth。它自动提供 /olinauthlogin和/olinauthlogout路由到应用程序, 处理使用olinapps对用户进行实际身份验证的过程。
在高水平上,Flask Olinauth为您的 这两个应用程序都在example.py中演示。
@auth_required
确保用户登录的简单函数装饰器 在显示视图之前。用户被重定向到登录门户,使用 正确的论点,如果不是。
current_user
一个很好的本地代理,用于获取当前用户的所有视图, 格式化为字典。
logout_user
将注销当前经过身份验证的用户,允许您 创建自己的注销视图。
Flask Olinauth深受Flask登录的启发, 以及采用的功能,以及获得的大量知识。
安装
使用以下命令之一安装扩展:
$ easy_install flask-olinauth
或者,如果您安装了pip(您应该安装):
$ pip install flask-olinauth
示例用法
from flask import Flask, url_for from flask.ext.olinauth import OlinAuth, auth_required, current_user app = Flask(__name__) SECRET_KEY = "yeah, not actually a secret" DEBUG = True app.config.from_object(__name__) oa = OlinAuth(app, 'localhost:5000') #initial OlinAuth, with callback host of localhost:5000, for local server oa.init_app(app, 'localhost:5000') @app.route("/") def index(): if current_user: responseString = "Awesome index, guess what? %s is logged in. Sweet, right?" % current_user['id'] else: responseString = "<html>It is kind of lonely here... No users are logged in. <a href=%s>Checkout my secret</a> </html>" % url_for('secret') return responseString @app.route("/secret") @auth_required def secret(): return "I wouldn't normally show you this, but since %s is logged in, here is the secret: 42" % current_user['id'] if __name__ == "__main__": app.run(debug=True)