Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit c1af792

Browse files
Merge pull request #1 from code-specialist/oauth2autobackend
OAuth2Middleware with automatic renewal added
2 parents 3d9f222 + 1c0473e commit c1af792

73 files changed

Lines changed: 713 additions & 12618 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ __pycache__/
66
# C extensions
77
*.so
88

9+
# Docs
10+
docs/site/**
11+
912
# IDEs
1013
.DS_Store
1114
.idea

docs/docs/api-reference/oauth2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OAuth2Middleware
2+
::: fastapi_auth_middleware.OAuth2Middleware
3+
4+
## OAuth2Backend
5+
::: fastapi_auth_middleware.oauth2_middleware.OAuth2Backend

docs/docs/examples/oauth2.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# OAuth2
2+
3+
```python
4+
import uvicorn
5+
from fastapi import FastAPI
6+
from starlette.requests import Request
7+
8+
from fastapi_auth_middleware import OAuth2Middleware
9+
10+
11+
def get_public_key():
12+
with open("key.pem") as keyfile:
13+
return keyfile.readlines()
14+
15+
16+
app = FastAPI()
17+
# Add the middleware with a public key for your JWT signer
18+
app.add_middleware(OAuth2Middleware, public_key=get_public_key())
19+
20+
21+
@app.get('/') # Sample endpoint (secured)
22+
def home(request: Request):
23+
return request.user # Returns the user object that is injected into the request. The FastAPIUser in this case
24+
25+
26+
if __name__ == '__main__':
27+
uvicorn.run('oauth2:app', host="0.0.0.0", port=8080) # Starts the uvicorn ASGI
28+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# OAuth2 Automatic Renewal
2+
3+
```python
4+
import uvicorn
5+
from fastapi import FastAPI
6+
from starlette.requests import Request
7+
8+
from fastapi_auth_middleware import OAuth2Middleware
9+
10+
11+
def get_new_token(old_token: str):
12+
# TODO: implement this logic
13+
return "eyJgh..."
14+
15+
16+
def get_public_key():
17+
with open("key.pem") as keyfile:
18+
return keyfile.readlines()
19+
20+
21+
app = FastAPI()
22+
# Add the middleware with the function that will return a new token and a public key for your JWT signer
23+
app.add_middleware(OAuth2Middleware, get_new_token=get_new_token, public_key=get_public_key())
24+
25+
26+
@app.get('/') # Sample endpoint (secured)
27+
def home(request: Request):
28+
return request.user # Returns the user object that is injected into the request. The FastAPIUser in this case
29+
30+
31+
if __name__ == '__main__':
32+
uvicorn.run('oauth2_automatic_renewal:app', host="0.0.0.0", port=8080) # Starts the uvicorn ASGI
33+
```

docs/mkdocs.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ theme:
3333
nav:
3434
- Introduction: ./index.md
3535
- Examples:
36-
- Basic: ./examples/index.md
37-
- Basic with Scopes: ./examples/simple_with_scopes.md
38-
- Protected and Unprotected endpoints: ./examples/multiple.md
36+
- Basic: ./examples/index.md
37+
- Basic with Scopes: ./examples/simple_with_scopes.md
38+
- Protected and Unprotected endpoints: ./examples/multiple.md
39+
- OAuth2: ./examples/oauth2.md
40+
- OAuth2, auto token renewal: ./examples/oauth2_automatic_renewal.md
3941
- API Reference:
40-
- Middleware: ./api-reference/auth-middleware.md
41-
- Backend: ./api-reference/auth-backend.md
42-
- User: ./api-reference/user.md
42+
- Middleware: ./api-reference/auth-middleware.md
43+
- Backend: ./api-reference/auth-backend.md
44+
- User: ./api-reference/user.md
45+
- OAuth2: ./api-reference/oauth2.md
4346

4447
markdown_extensions:
4548
- pymdownx.highlight
@@ -55,8 +58,8 @@ markdown_extensions:
5558
- pymdownx.snippets
5659

5760
plugins:
58-
- search
59-
- mkdocstrings
61+
- search
62+
- mkdocstrings
6063

6164
extra_css:
6265
- ./css/styles.css

0 commit comments

Comments
 (0)