Comments (6)
You should define a custom error handler in your FastAPI app for the KeycloakError that catches the error and returns the error message:
@app.exception_handler(KeycloakError)
async def keycloak_exception_handler(request: Request, exc: KeycloakError):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.reason},
)
Instead of throwing the error and raising a 500, the app should now proxy the errors as expected.
@jonra1993 Thank you for your question, we're going to add this to the documentation
from fastapi-keycloak.
you can try to override exception handlers
from fastapi-keycloak.
Thanks, @yannicschroeer your solution worked as expected
from fastapi-keycloak.
Hello, @yannicschroeer I have created a PR to add this into documentation please check it
#54
from fastapi-keycloak.
Hello,
Not working for me (create user endpoint as well as keycloak_exception_handler) as I am using router instead of app also tried
from backend.core.config import settings
from backend.secret import router
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi_keycloak import KeycloakError
def keycloak_exception_handler(request: Request, exc: KeycloakError):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.reason},
)
def create_application() -> FastAPI:
application = FastAPI(title=settings.PROJECT_NAME)
application.include_router(router)
return application
app = create_application()
app.add_exception_handler(KeycloakError, keycloak_exception_handler)
You should define a custom error handler in your FastAPI app for the KeycloakError that catches the error and returns the error message:
@app.exception_handler(KeycloakError) async def keycloak_exception_handler(request: Request, exc: KeycloakError): return JSONResponse( status_code=exc.status_code, content={"message": exc.reason}, )Instead of throwing the error and raising a 500, the app should now proxy the errors as expected.
@jonra1993 Thank you for your question, we're going to add this to the documentation
from fastapi-keycloak.
Hello, @Youssef-Harby can you try this setup using containers it worked in my case I plan in the future migrate this project to AuthN and AuthZ using keycloak it is still a work in progress, but you can check the setup and package versions.
from fastapi-keycloak.
Related Issues (20)
- realmRoles Field required HOT 4
- Why admin_client_secret? HOT 2
- Support other OAuth2 auth schemes than `OAuth2PasswordBearer`
- fastapi_keycloak.exceptions.KeycloakError: HTTP 403: {'error': 'unauthorized_client', 'error_description': 'Invalid client secret'} HOT 1
- Create clients from library HOT 1
- Question: 2FA support
- required_roles param in get_current_user method. Add OR condition
- unable to connect to keycloak server HOT 2
- The `realm-export.json` file in the documentation website needs to be updated
- fastapi-keycloak docs outdated?
- Unable to pip install from python docker images HOT 2
- Can we pass dynamic realm, client_id and client_secret params to FastAPIKeycloak class
- Cannot install from sources: distutils.errors.DistutilsOptionError: No configuration found for dynamic 'description'.
- Trying to run quickstart example, got error The 'realm_access' section of the provided access token is missing on /admin method HOT 7
- how to assign client roles to a user HOT 3
- Adde introspect token from server HOT 1
- Issue with Keycloak v20+ 'access required was not contained' HOT 4
- Refresh token HOT 2
- /protected return Not authenticated HOT 1
- password -> tokenUrl none is not an allowed value HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi-keycloak.