烧瓶应用程序的OAuth2提供程序。

Flask-Sentinel的Python项目详细描述


OAuth2提供程序当前支持资源所有者密码凭据 如RFC 6749第1.3.3节所述授予。

由Flask OAuthlib、Redis和MongoDB提供动力。

展开

$ pip install flask-sentinel

用法

一旦安装了扩展及其依赖项,就可以像任何 其他烧瓶扩展:

fromflaskimportFlaskfromflask.ext.sentinelimportResourceOwnerPasswordCredentials,oauthapp=Flask(__name__)# optionally load settings from py moduleapp.config.from_object('settings')@app.route('/endpoint')@oauth.require_oauth()defrestricted_access():return"You made it through and accessed the protected resource!"if__name__=='__main__':ResourceOwnerPasswordCredentials(app)app.run(ssl_context='adhoc')

用户和客户管理

您可以通过默认管理界面创建用户和客户机 在https://localhost:5000/oauth/management提供。

https://raw.githubusercontent.com/pyeve/flask-sentinel/master/static/console.png

您可以用自己的页面覆盖上面的默认页面。放下你的习惯 management.html文件位于应用程序中的templates文件夹中 根。

此页可以而且应该具有受限访问权限。为了达到这个目的,设定 SENTINEL_MANAGEMENT_USERNAMESENTINEL_MANAGEMENT_PASSWORD在 应用程序设置。当页面是 通过浏览器访问。

测试

创建用户和客户端后,可以使用curl测试应用程序。

生成承载令牌

$ curl -k -X POST -d "client_id=9qFbZD4udTzFVYo0u5UzkZX9iuzbdcJDRAquTfRk&grant_type=password&username=jonas&password=pass" https://localhost:5000/oauth/token
{"access_token": "NYODXSR8KalTPnWUib47t5E8Pi8mo4", "token_type": "Bearer", "refresh_token": "s6L6OPL2bnKSRSbgQM3g0wbFkJB4ML", "scope": ""}

使用检索到的刷新令牌生成承载令牌

$ curl -X POST -d "client_id=9qFbZD4udTzFVYo0u5UzkZX9iuzbdcJDRAquTfRk&grant_type=refresh_token&refresh_token=s6L6OPL2bnKSRSbgQM3g0wbFkJB4ML" https://localhost:5000/oauth/token
{"access_token": "RmPAfqfsDoMCbQ2DUUehwcw1hMCMJj", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "s6L6OPL2bnKSRSbgQM3g0wbFkJB4ML", "scope": ""}

使用检索到的承载令牌访问受保护资源

$ curl -k -H "Authorization: Bearer NYODXSR8KalTPnWUib47t5E8Pi8mo4" https://localhost:5000/endpoint
You made it through and accessed the protected resource!

配置

配置的工作方式与其他任何Flask configuration类似。这是 内置默认值:

^{tt7}$Default prefix for OAuth endpoints. Defaults to ^{tt8}$. Prepends both token and management urls.
^{tt9}$Url for token creation endpoint. Set to ^{tt10}$ to disable this feature. Defaults to ^{tt11}$, so the complete url is ^{tt12}$.
^{tt13}$Url for management endpoint. Set to ^{tt10}$ to disable this feature. Defaults to ^{tt15}$, so the complete url is ^{tt16}$.
^{tt17}$Url for the redis server. Defaults to ^{tt18}$.
^{tt19}$Mongo database name. Defaults to ^{tt20}$.
^{tt4}$Username needed to access the management page.
^{tt5}$Password needed to access the management page.
^{tt23}$The error page when there is an error, default value is ^{tt24}$.
^{tt25}$Default Bearer token expires time, default is ^{tt26}$.
^{tt27}$You can also configure the error page uri with an endpoint name.

其他标准pymongo设置,如MONGO_HOSTMONGO_PORTMONGO_URI也受支持;只需在它们前面加上SENTINEL_作为前缀 如上图所示。

创建令牌时,它将同时添加到数据库和redis缓存中。 在redis中,key是访问令牌本身,而value是 请求令牌的用户。这允许快速令牌 绕过数据库查找的身份验证/验证。这个技术可以 例如,在将flask-sentinelEve供电的rest集成时使用 API实例。

使用带有EVE的烧瓶哨兵

请参阅Eve-OAuth2示例项目。

安全性

ssl/tls

使用OAuth 2.0时,所有通信都必须使用SSL/TLS加密。 本例使用自动生成的ssl证书,但是在生产环境中 环境您应该使用更正式、更广泛信任的证书 你的域名。此外,请求应该由 NGIX并代理到认证服务。

注意:如果使用的是不可信的 在ssl/tls下运行的开发服务器。

密码散列

bcrypt和随机生成的salt用于在 它被添加到数据库中。你不应该用纯文本存储密码!

许可证

Flask Sentinel是一个Nicola IarocciGestionali Amica开源 分布在BSD license下的项目。

确认

这项工作基于Josh BrandoffJonas BrunsgaardyoloAPI项目。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Hibernate、Spring和SLF4J绑定   java如何准确地导航导航地图。天花板入口()工作?   从excel文件导入数据时发生java异常   java如何将地图转换为url查询字符串?   java HSQLDB在数据库中插入值   java将元素从JTextField添加到ArrayList   java如何使用SetDataSource从名称中有空格的MP3文件中获取唱片集艺术   java排序ArrayList<ArrayList<String>>作为行字段   java在拼写检查器中更新正确的拼写   哪里可以找到JavaAPI类图?   spring boot如何在自动生成的swagger java类中屏蔽任何参数   java使用哪种设计模式(我需要类似中介模式的东西)?   java为什么JTextfield没有出现在这个实例中?   如何在Java中向当前日期添加一个月?   安卓如何使java类可序列化,从而扩展不可序列化的