|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
1 | 5 | # Integration
|
2 | 6 |
|
3 | 7 | In the previous section, were described the configuration components of the OAuth2 authentication middleware and this
|
4 |
| -section will cover its integration into a FastAPI application. |
| 8 | +section covers its integration into a FastAPI app. |
| 9 | + |
| 10 | +## OAuth2Middleware |
| 11 | + |
| 12 | +The `OAuth2Middleware` is an authentication middleware which means that its usage makes the `user` and `auth` attributes |
| 13 | +available in the [request](https://www.starlette.io/requests/) context. It has a mandatory argument `config` of |
| 14 | +[`OAuth2Config`](/integration/configuration#oauth2config) instance that has been discussed at the previous section and |
| 15 | +an optional argument `callback` which is a callable that is called when the authentication succeeds. |
| 16 | + |
| 17 | +```python |
| 18 | +app: FastAPI |
| 19 | + |
| 20 | +def on_auth_success(auth: Auth, user: User): |
| 21 | + """This could be async function as well.""" |
| 22 | + |
| 23 | +app.add_middleware( |
| 24 | + OAuth2Middleware, |
| 25 | + config=OAuth2Config(...), |
| 26 | + callback=on_auth_success, |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +### Auth context |
| 31 | + |
| 32 | +This is extended version of Starlette's [`AuthCredentials`](https://www.starlette.io/authentication/#authcredentials) |
| 33 | +and the difference is that the `Auth` has additionally the list of the `clients` that can be used in the Jinja templates |
| 34 | +to display them dynamically, and the `provider` is an item of the `clients` that was used to authenticate the current |
| 35 | +user. Also, there are some methods for managing the JWT tokens: `jwt_encode`, `jwt_decode`, and `jwt_create`. |
| 36 | + |
| 37 | +### User context |
| 38 | + |
| 39 | +This is the extended version of Starlette's [`BaseUser`](https://www.starlette.io/authentication/#users) and apart from |
| 40 | +the default `is_authenticated` and `display_name` and the extended `identity`, `picture`, and `email` properties, it |
| 41 | +also contains all attributes of the user received from a certain provider. |
| 42 | + |
| 43 | +### Callback |
| 44 | + |
| 45 | +The `callback` is called with the [`Auth`](#auth-context) and [`User`](#user-context) arguments when the authentication |
| 46 | +succeeds. This can be used for migrating an external user into the system of the existing application. Apart from other |
| 47 | +OAuth2 solutions that force using their base user models, certain architectural designs, or a database from a limited |
| 48 | +set of choices, this kind of solution gives developers freedom. |
| 49 | + |
| 50 | +## Router |
| 51 | + |
| 52 | +Router defines the endpoints that are used for the authentication and logout. The authentication is done by |
| 53 | +the `/oauth2/{provider}/auth` endpoint and the logout is done by the `/oauth2/logout` endpoint. The `{provider}` is the |
| 54 | +name of the provider that is going to be used for the authentication and coincides with the `name` attribute of |
| 55 | +the `backend` provided to the certain `OAuth2Client`. |
| 56 | + |
| 57 | +```python |
| 58 | +from fastapi_oauth2.router import router as oauth2_router |
| 59 | + |
| 60 | +app.include_router(oauth2_router) |
| 61 | +``` |
| 62 | + |
| 63 | +## Security |
| 64 | + |
| 65 | +FastAPI's `OAuth2`, `OAuth2PasswordBearer` and `OAuth2AuthorizationCodeBearer` security models are supported, but in |
| 66 | +case your application uses cookies for storing the authentication tokens, you can use the same named security models |
| 67 | +from the `fastapi_oauth2.security` module. |
| 68 | + |
| 69 | +## Examples |
| 70 | + |
| 71 | +Working examples of all the above-described topics can be found in |
| 72 | +the [examples](https://github.com/pysnippet/fastapi-oauth2/tree/master/examples) and |
| 73 | +the [tests](https://github.com/pysnippet/fastapi-oauth2/tree/master/tests) directories of the repository. Also, feel |
| 74 | +free to open an [issue](https://github.com/pysnippet/fastapi-oauth2/issues/new/choose) or |
| 75 | +a [discussion](https://github.com/pysnippet/fastapi-oauth2/discussions/new/choose) if you have any questions not covered |
| 76 | +by the documentation. |
0 commit comments