BrightHive API授权和身份验证库
brighthive-authlib的Python项目详细描述
BrightHive身份验证库
authlib是专门为简化向驻留在brighthhive数据信任中的restful web服务添加身份验证和授权功能的任务而构建的库。有了这个库,开发人员只需要提供一个oauth 2.0提供程序,并将其注入一个简单的装饰器中,以保护特定的url。
快速链接
功能
- 内置对auth0的支持。
- 用于将身份验证功能注入应用程序的简单装饰器。
- 专门为Flask应用程序构建的,但是可以扩展到其他框架。
安装
Pypi
Pypi是安装此库的最快方法。只需通过pip安装程序安装brighthive-authlib
包。此库用于Python3.5+。
pip install brighthive-authlib
pipenv
通过Pipenv安装库很简单。
pipenv install brighthive-authlib
依赖性
- 烧瓶>;=1.0.2
- PyCryptoDome=3.8.0
- python jose[pycryptodome]>;=3.0.1
- 请求>;=2.21.0
用法
auth0示例
下面显示的代码片段是一个简单的flask应用程序,配置为使用Auth0作为默认的oauth 2.0提供程序。配置库以用于烧瓶应用程序需要三个步骤:
- 实例化OAuth 2.0提供程序对象。
- 用
token_required
装饰器装饰要保护的路由(传递OAuth 2.0提供程序对象和可选的作用域字典)。 - 配置自定义错误处理程序以处理
OAuth2ProviderError
异常(在本例中,返回401状态)。 - 坐下来惊叹整个过程是多么的无痛。
importjsonfromflaskimportFlask,requestfrombrighthive_authlibimportOAuth2ProviderError,OAuth2ProviderFactory,AuthLibConfiguration,token_required# Warning: Testing purposes only. These attributes need to be protected.PROVIDER='AUTH0'OAUTH2_URL='https://brighthive-test.auth0.com'JSON_URL='{}/.well-known/jwks.json'.format(OAUTH2_URL)AUDIENCE='http://localhost:8000'ALGORITHMS=['RS256']# Build the Auth Service Configuration Objectauth_config=AuthLibConfiguration(provider=PROVIDER,base_url=OAUTH2_URL,jwks_url=JSON_URL,algorithms=ALGORITHMS,audience=AUDIENCE)oauth2_provider=OAuth2ProviderFactory.get_provider(PROVIDER,auth_config)# Builed the Flask Appapp=Flask(__name__)# Add a Public Route@app.route('/public')defpublic_resource():returnjson.dumps({'message':'You can see me because I am public!'}),200# Add a Private Route@app.route('/private')@token_required(oauth2_provider,['get:users'])defprivate_resource():returnjson.dumps({'message':'If you can see me, you have a valid token.'}),200# Error Handler for Invalid or Expired Tokens@app.errorhandler(OAuth2ProviderError)defhandle_auth_error(e):returnjson.dumps({'message':'Access Denied'}),401if__name__=='__main__':app.run(debug=True)
贡献
要为此项目贡献代码,只需:
- 从here分叉存储库。
- 编写一些代码和相关的单元测试。
- 创建拉取请求。
开发准备
要设置开发环境,请克隆存储库并使用Pipenv创建虚拟环境。
git clone git@github.com:brighthive/tpot-abacus-api.git
cd tpot-abacus-api
pipenv --three
安装python依赖项。
pipenv install pipenv install --dev
测试
这个项目使用优秀的pytest和expects库进行单元测试。所有单元测试都位于tests
模块中。要运行单元测试,请在命令提示下调用pytest
。
$ pytest=======test session starts======= platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0 rootdir: /Users/gmundy/Work/brighthive-data-trusts/authlib, inifile: pytest.ini plugins: cov-2.6.1 collected 2 items tests/test_provider_factory.py .. [100%]===========2 passed in 0.60 seconds===========
许可证
麻省理工学院执照
版权所有)2019
BrightHive
特此免费向任何人授予许可 获取此软件和相关文档的副本 文件(以下简称“软件”),用于在没有 限制,包括但不限于使用权, 复制、修改、合并、发布、分发、再授权和/或出售 软件的副本,并允许 为此提供了软件,但须遵守以下规定 条件:
上述版权公告及本许可公告须 包含在软件的所有副本或大部分中。
软件按“原样”提供,不作任何保证, 明示或默示,包括但不限于保证 适销性、适合特定用途的 不侵犯。在任何情况下,作者或版权 持有人应对任何索赔、损害或其他责任负责, 无论是在合同诉讼、侵权诉讼还是其他诉讼中 来自、脱离或与软件或使用或 软件的其他交易。