Skip to content

Commit 5509c7d

Browse files
committed
GH-4: Add brief descriptions for each section
1 parent 9ddee23 commit 5509c7d

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

README.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# fastapi-oauth2 <img src="https://github.com/pysnippet.png" align="right" height="64" />
22

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.
45

56
## Features to be implemented
67

@@ -17,15 +18,36 @@ python -m pip install fastapi-oauth2
1718

1819
## Configuration
1920

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.
2143

2244
```python
2345
from fastapi_oauth2.client import OAuth2Client
2446
from fastapi_oauth2.config import OAuth2Config
2547
from social_core.backends.github import GithubOAuth2
2648

2749
oauth2_config = OAuth2Config(
28-
allow_http=True,
50+
allow_http=False,
2951
jwt_secret=os.getenv("JWT_SECRET"),
3052
jwt_expires=os.getenv("JWT_EXPIRES"),
3153
jwt_algorithm=os.getenv("JWT_ALGORITHM"),
@@ -34,25 +56,30 @@ oauth2_config = OAuth2Config(
3456
backend=GithubOAuth2,
3557
client_id=os.getenv("OAUTH2_CLIENT_ID"),
3658
client_secret=os.getenv("OAUTH2_CLIENT_SECRET"),
59+
redirect_uri="https://pysnippet.org/",
3760
scope=["user:email"],
3861
),
3962
]
4063
)
4164
```
4265

43-
## Usage
66+
## Integration
4467

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.
4670

4771
```python
4872
from fastapi import FastAPI
4973
from fastapi_oauth2.middleware import OAuth2Middleware
74+
from fastapi_oauth2.router import router as oauth2_router
5075

5176
app = FastAPI()
77+
app.include_router(oauth2_router)
5278
app.add_middleware(OAuth2Middleware, config=oauth2_config)
5379
```
5480

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.
5683

5784
```jinja2
5885
{% if request.user.is_authenticated %}

0 commit comments

Comments
 (0)