Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Update documentation to add error handler (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonra1993 authored Jun 16, 2022
1 parent dc71913 commit 33b69a8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
56 changes: 45 additions & 11 deletions documentation/docs/full_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
from typing import List, Optional

import uvicorn
from fastapi import FastAPI, Depends, Query, Body
from fastapi import FastAPI, Depends, Query, Body, Request
from fastapi.responses import JSONResponse
from pydantic import SecretStr

from fastapi_keycloak import FastAPIKeycloak, OIDCUser, UsernamePassword, HTTPMethod, KeycloakUser, KeycloakGroup
from fastapi_keycloak import (
FastAPIKeycloak,
OIDCUser,
UsernamePassword,
HTTPMethod,
KeycloakUser,
KeycloakGroup,
KeycloakError
)

app = FastAPI()
idp = FastAPIKeycloak(
Expand All @@ -21,10 +30,24 @@ idp = FastAPIKeycloak(
idp.add_swagger_config(app)


# Custom error handler for showing Keycloak errors on FastAPI
@app.exception_handler(KeycloakError)
async def keycloak_exception_handler(request: Request, exc: KeycloakError):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.reason},
)


# Admin

@app.post("/proxy", tags=["admin-cli"])
def proxy_admin_request(relative_path: str, method: HTTPMethod, additional_headers: dict = Body(None), payload: dict = Body(None)):
def proxy_admin_request(
relative_path: str,
method: HTTPMethod,
additional_headers: dict = Body(None),
payload: dict = Body(None),
):
return idp.proxy(
additional_headers=additional_headers,
relative_path=relative_path,
Expand Down Expand Up @@ -56,8 +79,17 @@ def get_user_by_query(query: str = None):


@app.post("/users", tags=["user-management"])
def create_user(first_name: str, last_name: str, email: str, password: SecretStr, id: str = None):
return idp.create_user(first_name=first_name, last_name=last_name, username=email, email=email, password=password.get_secret_value(), id=id)
def create_user(
first_name: str, last_name: str, email: str, password: SecretStr, id: str = None
):
return idp.create_user(
first_name=first_name,
last_name=last_name,
username=email,
email=email,
password=password.get_secret_value(),
id=id
)


@app.get("/user/{user_id}", tags=["user-management"])
Expand Down Expand Up @@ -182,12 +214,14 @@ def get_current_users_roles(user: OIDCUser = Depends(idp.get_current_user())):

@app.get("/admin", tags=["example-user-request"])
def company_admin(user: OIDCUser = Depends(idp.get_current_user(required_roles=["admin"]))):
return f'Hi admin {user}'
return f"Hi admin {user}"


@app.get("/login", tags=["example-user-request"])
def login(user: UsernamePassword = Depends()):
return idp.user_login(username=user.username, password=user.password.get_secret_value())
@app.post("/login", tags=["example-user-request"])
def login(user: UsernamePassword = Body(...)):
return idp.user_login(
username=user.username, password=user.password.get_secret_value()
)


# Auth Flow
Expand All @@ -207,6 +241,6 @@ def logout():
return idp.logout_uri


if __name__ == '__main__':
uvicorn.run('app:app', host="127.0.0.1", port=8081)
if __name__ == "__main__":
uvicorn.run("app:app", host="127.0.0.1", port=8081)
```
17 changes: 14 additions & 3 deletions tests/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import List, Optional

import uvicorn
from fastapi import Body, Depends, FastAPI, Query
from fastapi import FastAPI, Depends, Query, Body, Request
from fastapi.responses import JSONResponse
from pydantic import SecretStr

from fastapi_keycloak import (
Expand All @@ -10,6 +11,7 @@
KeycloakUser,
OIDCUser,
UsernamePassword,
KeycloakError
)

app = FastAPI()
Expand All @@ -25,6 +27,15 @@
idp.add_swagger_config(app)


# Custom error handler for showing Keycloak errors on FastAPI
@app.exception_handler(KeycloakError)
async def keycloak_exception_handler(request: Request, exc: KeycloakError):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.reason},
)


# Admin


Expand Down Expand Up @@ -212,8 +223,8 @@ def company_admin(
return f"Hi admin {user}"


@app.get("/login", tags=["example-user-request"])
def login(user: UsernamePassword = Depends()):
@app.post("/login", tags=["example-user-request"])
def login(user: UsernamePassword = Body(...)):
return idp.user_login(
username=user.username, password=user.password.get_secret_value()
)
Expand Down

0 comments on commit 33b69a8

Please sign in to comment.