|
1 |
| -# fastapi-oauth2 |
| 1 | +# fastapi-oauth2 <img src="https://github.com/pysnippet.png" align="right" height="64" /> |
2 | 2 |
|
3 |
| -Easy to setup OAuth2 social authentication mechanism with support for several auth providers. |
| 3 | +[](https://pypi.org/project/fastapi-oauth2/) |
| 4 | +[](https://pypi.org/project/fastapi-oauth2/) |
| 5 | +[](https://pypi.org/project/fastapi-oauth2/) |
| 6 | +[](https://github.com/pysnippet/fastapi-oauth2/actions/workflows/tests.yml) |
| 7 | +[](https://github.com/pysnippet/fastapi-oauth2/blob/master/LICENSE) |
4 | 8 |
|
5 |
| -## Demo |
| 9 | +FastAPI OAuth2 is a middleware-based social authentication mechanism supporting several auth providers. It depends on |
| 10 | +the [social-core](https://github.com/python-social-auth/social-core) authentication backends. |
6 | 11 |
|
7 |
| -This sample application is made to demonstrate the use of the [**fastapi-oauth2**](./fastapi_oauth2) package. |
| 12 | +## Features to be implemented |
8 | 13 |
|
9 |
| -## Running the application |
| 14 | +- Use multiple OAuth2 providers at the same time |
| 15 | + * There need to be provided a way to configure the OAuth2 for multiple providers |
| 16 | +- Provide `fastapi.security.*` implementations that use cookies |
| 17 | +- Token -> user data, user data -> token easy conversion |
| 18 | +- Customizable OAuth2 routes |
| 19 | +- Registration support |
| 20 | + |
| 21 | +## Installation |
10 | 22 |
|
11 |
| -```bash |
12 |
| -uvicorn main:app --reload |
| 23 | +```shell |
| 24 | +python -m pip install fastapi-oauth2 |
13 | 25 | ```
|
14 | 26 |
|
15 |
| -## TODO |
| 27 | +## Configuration |
16 | 28 |
|
17 |
| -- Make the [**fastapi-oauth2**](./fastapi_oauth2) depend |
18 |
| - on (overuse) the [**social-core**](https://github.com/python-social-auth/social-core) |
| 29 | +Configuration requires you to provide the JWT requisites and define the clients of the particular providers. The |
| 30 | +middleware configuration is declared with the `OAuth2Config` and `OAuth2Client` classes. |
19 | 31 |
|
20 |
| -## Features |
| 32 | +### OAuth2Config |
21 | 33 |
|
22 |
| -- Integrate with any existing FastAPI project (no dependencies of the project should stop the work of |
23 |
| - the `fastapi-oauth2`) |
24 |
| - * Implementation must allow to provide a context for configurations (also, see how it is done in another projects) |
25 |
| -- Use multiple OAuth2 providers at the same time |
26 |
| - * There need to be provided a way to configure the OAuth2 for multiple providers |
27 |
| -- Token -> user data, user data -> token easy conversion |
28 |
| -- Customize OAuth2 routes |
| 34 | +- `allow_http` - Allow insecure HTTP requests. Defaults to `False`. |
| 35 | +- `jwt_secret` - The secret key used to sign the JWT. Defaults to `None`. |
| 36 | +- `jwt_expires` - The expiration time of the JWT in seconds. Defaults to `900`. |
| 37 | +- `jwt_algorithm` - The algorithm used to sign the JWT. Defaults to `HS256`. |
| 38 | +- `clients` - The list of the OAuth2 clients. Defaults to `[]`. |
| 39 | + |
| 40 | +### OAuth2Client |
| 41 | + |
| 42 | +- `backend` - The [social-core](https://github.com/python-social-auth/social-core) authentication backend classname. |
| 43 | +- `client_id` - The OAuth2 client ID for the particular provider. |
| 44 | +- `client_secret` - The OAuth2 client secret for the particular provider. |
| 45 | +- `redirect_uri` - The OAuth2 redirect URI to redirect to after success. Defaults to the base URL. |
| 46 | +- `scope` - The OAuth2 scope for the particular provider. Defaults to `[]`. |
| 47 | + |
| 48 | +It is also important to mention that for the configured clients of the auth providers, the authorization URLs are |
| 49 | +accessible by the `/oauth2/{provider}/auth` path where the `provider` variable represents the exact value of the auth |
| 50 | +provider backend `name` attribute. |
| 51 | + |
| 52 | +```python |
| 53 | +from fastapi_oauth2.client import OAuth2Client |
| 54 | +from fastapi_oauth2.config import OAuth2Config |
| 55 | +from social_core.backends.github import GithubOAuth2 |
| 56 | + |
| 57 | +oauth2_config = OAuth2Config( |
| 58 | + allow_http=False, |
| 59 | + jwt_secret=os.getenv("JWT_SECRET"), |
| 60 | + jwt_expires=os.getenv("JWT_EXPIRES"), |
| 61 | + jwt_algorithm=os.getenv("JWT_ALGORITHM"), |
| 62 | + clients=[ |
| 63 | + OAuth2Client( |
| 64 | + backend=GithubOAuth2, |
| 65 | + client_id=os.getenv("OAUTH2_CLIENT_ID"), |
| 66 | + client_secret=os.getenv("OAUTH2_CLIENT_SECRET"), |
| 67 | + redirect_uri="https://pysnippet.org/", |
| 68 | + scope=["user:email"], |
| 69 | + ), |
| 70 | + ] |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +## Integration |
| 75 | + |
| 76 | +To integrate the package into your FastAPI application, you need to add the `OAuth2Middleware` with particular configs |
| 77 | +in the above-represented format and include the router to the main router of the application. |
| 78 | + |
| 79 | +```python |
| 80 | +from fastapi import FastAPI |
| 81 | +from fastapi_oauth2.middleware import OAuth2Middleware |
| 82 | +from fastapi_oauth2.router import router as oauth2_router |
| 83 | + |
| 84 | +app = FastAPI() |
| 85 | +app.include_router(oauth2_router) |
| 86 | +app.add_middleware(OAuth2Middleware, config=oauth2_config) |
| 87 | +``` |
| 88 | + |
| 89 | +After adding the middleware, the `user` attribute will be available in the request context. It will contain the user |
| 90 | +data provided by the OAuth2 provider. |
| 91 | + |
| 92 | +```jinja2 |
| 93 | +{% if request.user.is_authenticated %} |
| 94 | + <a href="/oauth2/logout">Sign out</a> |
| 95 | +{% else %} |
| 96 | + <a href="/oauth2/github/auth">Sign in</a> |
| 97 | +{% endif %} |
| 98 | +``` |
| 99 | + |
| 100 | +## Contribute |
| 101 | + |
| 102 | +Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And |
| 103 | +don't forget to add tests for your changes. |
| 104 | + |
| 105 | +## License |
| 106 | + |
| 107 | +Copyright (C) 2023 Artyom Vancyan. [MIT](https://github.com/pysnippet/fastapi-oauth2/blob/master/LICENSE) |
0 commit comments