1
1
# fastapi-oauth2 <img src =" https://github.com/pysnippet.png " align =" right " height =" 64 " />
2
2
3
- [ // ] : # ( TODO: LONG DESCRIPTION )
3
+ FastAPI OAuth2 is a middleware-based social authentication mechanism supporting several auth providers. It depends on
4
+ the [ social-core] ( https://github.com/python-social-auth/social-core ) authentication backends.
4
5
5
6
## Features to be implemented
6
7
@@ -17,15 +18,36 @@ python -m pip install fastapi-oauth2
17
18
18
19
## Configuration
19
20
20
- [ // ] : # ( TODO: LONG DESCRIPTION )
21
+ Configuration requires you to provide the JWT requisites and define the clients of the particular providers. The
22
+ middleware configuration is declared with the ` OAuth2Config ` and ` OAuth2Client ` classes.
23
+
24
+ ### OAuth2Config
25
+
26
+ - ` allow_http ` - Allow insecure HTTP requests. Defaults to ` False ` .
27
+ - ` jwt_secret ` - The secret key used to sign the JWT. Defaults to ` None ` .
28
+ - ` jwt_expires ` - The expiration time of the JWT in seconds. Defaults to ` 900 ` .
29
+ - ` jwt_algorithm ` - The algorithm used to sign the JWT. Defaults to ` HS256 ` .
30
+ - ` clients ` - The list of the OAuth2 clients. Defaults to ` [] ` .
31
+
32
+ ### OAuth2Client
33
+
34
+ - ` backend ` - The [ social-core] ( https://github.com/python-social-auth/social-core ) authentication backend classname.
35
+ - ` client_id ` - The OAuth2 client ID for the particular provider.
36
+ - ` client_secret ` - The OAuth2 client secret for the particular provider.
37
+ - ` redirect_uri ` - The OAuth2 redirect URI to redirect to after success. Defaults to the base URL.
38
+ - ` scope ` - The OAuth2 scope for the particular provider. Defaults to ` [] ` .
39
+
40
+ It is also important to mention that for the configured clients of the auth providers, the authorization URLs are
41
+ accessible by the ` /oauth2/{provider}/auth ` path where the ` provider ` variable represents the exact value of the auth
42
+ provider backend ` name ` attribute.
21
43
22
44
``` python
23
45
from fastapi_oauth2.client import OAuth2Client
24
46
from fastapi_oauth2.config import OAuth2Config
25
47
from social_core.backends.github import GithubOAuth2
26
48
27
49
oauth2_config = OAuth2Config(
28
- allow_http = True ,
50
+ allow_http = False ,
29
51
jwt_secret = os.getenv(" JWT_SECRET" ),
30
52
jwt_expires = os.getenv(" JWT_EXPIRES" ),
31
53
jwt_algorithm = os.getenv(" JWT_ALGORITHM" ),
@@ -34,25 +56,30 @@ oauth2_config = OAuth2Config(
34
56
backend = GithubOAuth2,
35
57
client_id = os.getenv(" OAUTH2_CLIENT_ID" ),
36
58
client_secret = os.getenv(" OAUTH2_CLIENT_SECRET" ),
59
+ redirect_uri = " https://pysnippet.org/" ,
37
60
scope = [" user:email" ],
38
61
),
39
62
]
40
63
)
41
64
```
42
65
43
- ## Usage
66
+ ## Integration
44
67
45
- [ // ] : # ( TODO: LONG DESCRIPTION )
68
+ To integrate the package into your FastAPI application, you need to add the ` OAuth2Middleware ` with particular configs
69
+ in the above-represented format and include the router to the main router of the application.
46
70
47
71
``` python
48
72
from fastapi import FastAPI
49
73
from fastapi_oauth2.middleware import OAuth2Middleware
74
+ from fastapi_oauth2.router import router as oauth2_router
50
75
51
76
app = FastAPI()
77
+ app.include_router(oauth2_router)
52
78
app.add_middleware(OAuth2Middleware, config = oauth2_config)
53
79
```
54
80
55
- [ // ] : # ( TODO: LONG DESCRIPTION )
81
+ After adding the middleware, the ` user ` attribute will be available in the request context. It will contain the user
82
+ data provided by the OAuth2 provider.
56
83
57
84
``` jinja2
58
85
{% if request.user.is_authenticated %}
0 commit comments