44[ ![ Python] ( https://img.shields.io/pypi/pyversions/fastapi-oauth2.svg?logoColor=white )] ( https://pypi.org/project/fastapi-oauth2/ )
55[ ![ FastAPI] ( https://img.shields.io/badge/fastapi-%E2%89%A50.68.1-009486 )] ( https://pypi.org/project/fastapi-oauth2/ )
66[ ![ Tests] ( https://github.com/pysnippet/fastapi-oauth2/actions/workflows/tests.yml/badge.svg )] ( https://github.com/pysnippet/fastapi-oauth2/actions/workflows/tests.yml )
7- [ ![ License ] ( https://img.shields.io/pypi/l/ fastapi-oauth2. svg )] ( https://github.com/pysnippet/fastapi-oauth2/blob/master/LICENSE )
7+ [ ![ Docs ] ( https://github.com/pysnippet/ fastapi-oauth2/actions/workflows/docs.yml/badge. svg )] ( https://github.com/pysnippet/fastapi-oauth2/actions/workflows/docs.yml )
88
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.
11-
12- ## Installation
13-
14- ``` shell
15- python -m pip install fastapi-oauth2
16- ```
17-
18- ## Configuration
19-
20- Configuration requires you to provide the JWT requisites and define the clients of the particular providers. The
21- middleware configuration is declared with the ` OAuth2Config ` and ` OAuth2Client ` classes.
22-
23- ### OAuth2Config
24-
25- - ` allow_http ` - Allow insecure HTTP requests. Defaults to ` False ` .
26- - ` jwt_secret ` - The secret key used to sign the JWT. Defaults to ` None ` .
27- - ` jwt_expires ` - The expiration time of the JWT in seconds. Defaults to ` 900 ` .
28- - ` jwt_algorithm ` - The algorithm used to sign the JWT. Defaults to ` HS256 ` .
29- - ` clients ` - The list of the OAuth2 clients. Defaults to ` [] ` .
30-
31- ### OAuth2Client
32-
33- - ` backend ` - The [ social-core] ( https://github.com/python-social-auth/social-core ) authentication backend classname.
34- - ` client_id ` - The OAuth2 client ID for the particular provider.
35- - ` client_secret ` - The OAuth2 client secret for the particular provider.
36- - ` redirect_uri ` - The OAuth2 redirect URI to redirect to after success. Defaults to the base URL.
37- - ` scope ` - The OAuth2 scope for the particular provider. Defaults to ` [] ` .
38- - ` claims ` - Claims mapping for the certain provider.
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.
43-
44- ``` python
45- from fastapi_oauth2.claims import Claims
46- from fastapi_oauth2.client import OAuth2Client
47- from fastapi_oauth2.config import OAuth2Config
48- from social_core.backends.github import GithubOAuth2
49-
50- oauth2_config = OAuth2Config(
51- allow_http = False ,
52- jwt_secret = os.getenv(" JWT_SECRET" ),
53- jwt_expires = os.getenv(" JWT_EXPIRES" ),
54- jwt_algorithm = os.getenv(" JWT_ALGORITHM" ),
55- clients = [
56- OAuth2Client(
57- backend = GithubOAuth2,
58- client_id = os.getenv(" OAUTH2_CLIENT_ID" ),
59- client_secret = os.getenv(" OAUTH2_CLIENT_SECRET" ),
60- redirect_uri = " https://pysnippet.org/" ,
61- scope = [" user:email" ],
62- claims = Claims(
63- picture = " avatar_url" ,
64- identity = lambda user : " %s :%s " % (user.get(" provider" ), user.get(" id" )),
65- ),
66- ),
67- ]
68- )
69- ```
9+ FastAPI OAuth2 is a middleware-based social authentication mechanism supporting several OAuth2 providers. It leverages
10+ the [ social-core] ( https://github.com/python-social-auth/social-core ) authentication backends and integrates seamlessly
11+ with FastAPI applications.
7012
7113## Integration
7214
73- To integrate the package into your FastAPI application, you need to add the ` OAuth2Middleware ` with particular configs
74- in the above-represented format and include the router to the main router of the application.
15+ For integrating the package into an existing FastAPI application, the router with OAuth2 routes and
16+ the ` OAuth2Middleware ` with particular [ configs] ( https://docs.pysnippet.org/fastapi-oauth2/integration/configuration )
17+ should be added to the application.
7518
7619``` python
7720from fastapi import FastAPI
@@ -80,24 +23,14 @@ from fastapi_oauth2.router import router as oauth2_router
8023
8124app = FastAPI()
8225app.include_router(oauth2_router)
83- app.add_middleware(OAuth2Middleware, config = oauth2_config)
84- ```
85-
86- After adding the middleware, the ` user ` attribute will be available in the request context. It will contain the user
87- data provided by the OAuth2 provider.
88-
89- ``` jinja2
90- {% if request.user.is_authenticated %}
91- <a href="/oauth2/logout">Sign out</a>
92- {% else %}
93- <a href="/oauth2/github/auth">Sign in</a>
94- {% endif %}
26+ app.add_middleware(OAuth2Middleware, config = OAuth2Config(... ))
9527```
9628
9729## Contribute
9830
99- Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And
100- don't forget to add tests for your changes.
31+ Any contribution is welcome. Always feel free to open an issue or a discussion if you have any questions not covered by
32+ the documentation. If you have any ideas or suggestions, please, open a pull request. Your name will shine in our
33+ contributors' list. Be proud of what you build!
10134
10235## License
10336
0 commit comments