-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add excluded_paths to token auth (#227)
* add excluded_paths to token auth * make suggested changes * move example to `literalinclude` and add images * add params to docstring --------- Co-authored-by: Daniel Townsend <dan@dantownsend.co.uk>
- Loading branch information
1 parent
bcb1c72
commit 40d4316
Showing
8 changed files
with
222 additions
and
11 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
docs/source/token_auth/examples/excluded_paths_example/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
An example usage of excluded_paths. | ||
""" | ||
|
||
from fastapi import Depends, FastAPI | ||
from fastapi.middleware import Middleware | ||
from fastapi.security.api_key import APIKeyHeader | ||
from starlette.middleware.authentication import AuthenticationMiddleware | ||
from tables import Movie # An example Table | ||
|
||
from piccolo_api.crud.endpoints import PiccoloCRUD | ||
from piccolo_api.fastapi.endpoints import FastAPIKwargs, FastAPIWrapper | ||
from piccolo_api.token_auth.middleware import ( | ||
SecretTokenAuthProvider, | ||
TokenAuthBackend, | ||
) | ||
|
||
app = FastAPI( | ||
dependencies=[Depends(APIKeyHeader(name="Authorization"))], | ||
middleware=[ | ||
Middleware( | ||
AuthenticationMiddleware, | ||
backend=TokenAuthBackend( | ||
SecretTokenAuthProvider(tokens=["abc123"]), | ||
excluded_paths=["/docs", "/openapi.json"], | ||
), | ||
) | ||
], | ||
) | ||
|
||
|
||
# This is a quick way of building FastAPI endpoiints using Piccolo, but isn't | ||
# required: | ||
FastAPIWrapper( | ||
"/movies/", | ||
fastapi_app=app, | ||
piccolo_crud=PiccoloCRUD(Movie, read_only=False), | ||
fastapi_kwargs=FastAPIKwargs( | ||
all_routes={"tags": ["Movie"]}, | ||
), | ||
) |
23 changes: 23 additions & 0 deletions
23
docs/source/token_auth/examples/excluded_paths_example/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import sys | ||
|
||
import uvicorn | ||
|
||
sys.path.insert( | ||
0, | ||
os.path.dirname( | ||
os.path.dirname( | ||
os.path.dirname( | ||
os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | ||
) | ||
) | ||
), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
from app import app | ||
from tables import Movie | ||
|
||
Movie.create_table(if_not_exists=True).run_sync() | ||
uvicorn.run(app, port=8081) |
9 changes: 9 additions & 0 deletions
9
docs/source/token_auth/examples/excluded_paths_example/tables.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from piccolo.columns.column_types import Varchar | ||
from piccolo.engine.sqlite import SQLiteEngine | ||
from piccolo.table import Table | ||
|
||
DB = SQLiteEngine() | ||
|
||
|
||
class Movie(Table, db=DB): | ||
title = Varchar() |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters