Skip to content

Commit 9ddee23

Browse files
committed
GH-4: Add code snippets
1 parent 4899131 commit 9ddee23

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

README.md

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

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
411

512
## Installation
613

714
```shell
815
python -m pip install fastapi-oauth2
916
```
1017

11-
## Features to be implemented
18+
## Configuration
1219

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+
```
1764

1865
## Contribute
1966

0 commit comments

Comments
 (0)