跳转到主要内容

一个简单的Flask扩展,实现Olin的认证

项目描述

Flask-OlinAuth实现了OlinApps认证。它自动为应用程序提供/olinauthlogin/olinauthlogout路由,处理使用olinapps实际认证用户的过程。

从高层次来看,Flask-OlinAuth为您的应用程序提供了两个有用的组件,这两个组件都在example.py中得到了演示。

@auth_required

一个简单的函数装饰器,确保在显示视图之前用户已登录。如果未登录,则将用户重定向到登录门户,并带有正确的参数。

current_user

一个本地代理,允许您的所有视图获取当前用户,格式化为字典。

logout_user

将使当前认证的用户注销,允许您简单地创建自己的注销视图。

Flask-OlinAuth深受Flask-Login的启发,采用了结构和功能,并获得了很多知识。

安装

使用以下命令之一安装扩展

$ 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)

项目详情


下载文件

下载您平台所需的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

Flask-OlinAuth-1.0.1.zip (8.3 kB 查看哈希值)

上传时间 源代码

构建分发

Flask-OlinAuth-1.0.1.win32.exe (203.5 kB 查看哈希值)

上传时间 源代码

由以下组织支持