|
1 | 1 | # fastapi-oauth2 <img src="https://github.com/pysnippet.png" align="right" height="64" />
|
2 | 2 |
|
3 |
| -Easy to setup OAuth2 authentication with support for several auth providers. |
| 3 | +[//]: # (TODO: LONG DESCRIPTION) |
| 4 | + |
| 5 | +## Features to be implemented |
| 6 | + |
| 7 | +- Use multiple OAuth2 providers at the same time |
| 8 | + * There need to be provided a way to configure the OAuth2 for multiple providers |
| 9 | +- Token -> user data, user data -> token easy conversion |
| 10 | +- Customize OAuth2 routes |
4 | 11 |
|
5 | 12 | ## Installation
|
6 | 13 |
|
7 | 14 | ```shell
|
8 | 15 | python -m pip install fastapi-oauth2
|
9 | 16 | ```
|
10 | 17 |
|
11 |
| -## Features to be implemented |
| 18 | +## Configuration |
12 | 19 |
|
13 |
| -- Use multiple OAuth2 providers at the same time |
14 |
| - * There need to be provided a way to configure the OAuth2 for multiple providers |
15 |
| -- Token -> user data, user data -> token easy conversion |
16 |
| -- Customize OAuth2 routes |
| 20 | +[//]: # (TODO: LONG DESCRIPTION) |
| 21 | + |
| 22 | +```python |
| 23 | +from fastapi_oauth2.client import OAuth2Client |
| 24 | +from fastapi_oauth2.config import OAuth2Config |
| 25 | +from social_core.backends.github import GithubOAuth2 |
| 26 | + |
| 27 | +oauth2_config = OAuth2Config( |
| 28 | + allow_http=True, |
| 29 | + jwt_secret=os.getenv("JWT_SECRET"), |
| 30 | + jwt_expires=os.getenv("JWT_EXPIRES"), |
| 31 | + jwt_algorithm=os.getenv("JWT_ALGORITHM"), |
| 32 | + clients=[ |
| 33 | + OAuth2Client( |
| 34 | + backend=GithubOAuth2, |
| 35 | + client_id=os.getenv("OAUTH2_CLIENT_ID"), |
| 36 | + client_secret=os.getenv("OAUTH2_CLIENT_SECRET"), |
| 37 | + scope=["user:email"], |
| 38 | + ), |
| 39 | + ] |
| 40 | +) |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +[//]: # (TODO: LONG DESCRIPTION) |
| 46 | + |
| 47 | +```python |
| 48 | +from fastapi import FastAPI |
| 49 | +from fastapi_oauth2.middleware import OAuth2Middleware |
| 50 | + |
| 51 | +app = FastAPI() |
| 52 | +app.add_middleware(OAuth2Middleware, config=oauth2_config) |
| 53 | +``` |
| 54 | + |
| 55 | +[//]: # (TODO: LONG DESCRIPTION) |
| 56 | + |
| 57 | +```jinja2 |
| 58 | +{% if request.user.is_authenticated %} |
| 59 | + <a href="/oauth2/logout">Sign out</a> |
| 60 | +{% else %} |
| 61 | + <a href="/oauth2/github/auth">Sign in</a> |
| 62 | +{% endif %} |
| 63 | +``` |
17 | 64 |
|
18 | 65 | ## Contribute
|
19 | 66 |
|
|
0 commit comments