From ecf55b72cecf2c0aadb11002c2510f2621f75f8a Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Mon, 4 Mar 2024 12:32:05 +0000 Subject: [PATCH] Expand readme example and fix link to license (#10) * Expand example * Fix link to license --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9340f4f..5d0ba5a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ pip install keycloak-oauth[fastapi] ``` ```python -from fastapi import FastAPI +from typing import Annotated +from fastapi import FastAPI, Request, Depends from starlette.middleware.sessions import SessionMiddleware from backend.settings import settings, BASE_URL, SECRET_KEY # secrets from keycloak_oauth import KeycloakOAuth2 @@ -29,6 +30,13 @@ keycloak.setup_fastapi_routes() app = FastAPI() app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY) app.include_router(keycloak.router, prefix="/auth") + +@app.get("/") +def index( + request: Request, user: Annotated[User, Depends(KeycloakOAuth2.get_user)] +): + """Protected endpoint, will return 401 Unauthorized if not signed in.""" + return f"Hello {user.name}" ``` We now expose the API endpoints for Keycloak: @@ -65,6 +73,8 @@ admin = Admin( auth_provider=KeycloakAuthProvider(keycloak), middlewares=[Middleware(SessionMiddleware, secret_key=SECRET_KEY)], ) + +admin.add_view(...) ``` ## Development @@ -79,4 +89,4 @@ We are happy if you want to contribute to this project. If you find any bugs or ## License -This project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE.md) for more details. +This project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE) for more details.