# aiohttp-oauth2
A provider agnostic oauth2 client library for aiohttp, implemented as a self-composed nested application.
No opinions about auth mechanisms are enforced on the application, an `on_login` and `on_error` coroutine can be provided to implement your own login mechanisms (token, session, etc).
## Usage
### Simple
```python
from aiohttp import web
from aiohttp_oauth2 import oauth2_app
async def app_factory():
app = web.Application()
app.add_subapp(
"/github/", # any arbitrary prefix
oauth2_app(
client_id=123,
client_secret=456,
authorize_url="https://github.com/login/oauth/authorize",
token_url="https://github.com/login/oauth/access_token",
# add scopes if you want to customize them
scopes=["foo", "bar", "baz"],
# optionally add an on_login coroutine to handle the post-login logic
# it should expect the request and the oauth2 access code response
on_login=set_session_and_redirect,
on_error=show_error_page,
),
)
return app
```
The necessary oauth2 routes are added as `/auth` and `/callback`, only the former a developer needs to worry about.
Now logging in a user is as simple as redirecting them to: `/github/auth`.
### Complex
Since the `oauth2_app` function is simply a factory that generates sub-apps, you can use this to add any number of oauth2 providers to log in against:
```python
async def app_factory():
app = web.Application()
app.add_subapp(
"/github/",
oauth2_app(
...,
authorize_url="https://github.com/login/oauth/authorize",
token_url="https://github.com/login/oauth/access_token",
)
)
app.add_subapp(
"/github/",
oauth2_app(
...,
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
token_url="https://www.googleapis.com/oauth2/v4/token",
)
)
app.add_subapp(
"/twitter/",
oauth2_app(
...,
authorize_url="https://api.twitter.com/oauth/authorize",
token_url="https://api.twitter.com/oauth2/token",
)
)
...
return app
```
### Examples
Check the "examples" directory for working examples:
```
$ cd examples
$ pip install -r requirements.txt
# this just makes the library available for import, don't typically do it :D
$ PYTHONPATH=".." python github.py
```
PyPI 官网下载 | aiohttp-oauth2-0.0.2.tar.gz
版权申诉
67 浏览量
2022-01-08
22:30:38
上传
评论
收藏 3KB GZ 举报
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
最新资源
- 三次样条插值在C语言如何实现步骤介绍.docx
- SD6084电流模式同步降压转换器固定频率1.5MHz二极管封装SOT23-5
- NokoPrint-wifi蓝牙USB连接打印机[安卓免费App]
- 20211115aMmF9NbS.zip
- 解线性方程组-直接解法:(Gauss)高斯消去法、列主元、全主元 - 北太天元
- MapReduce单词统计 hadoop集群
- 深度学习源码神经网络新闻分类多分类问题ipynb源码带数据集
- 深度学习源码神经网络用预训练的卷积神经网络ipynb源码带数据集
- 深度学习源码神经网络使用词嵌入ipynb源码带数据集
- 深度学习源码神经网络使用LSTM生成文本ipynb源码带数据集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈