- python 3.10+
pip install pip install git+ssh://git@github.com/expertspec/expert_core.git@{tag}
from fastapi import FastAPI
from expert_core.plugins import init_keycloak
app = FastAPI()
@app.on_event("startup")
async def startup():
await init_keycloak(app)
from fastapi import Depends
from expert_core.plugins.keycloak import depends_authentication, OIDCUser
@app.get("/user")
async def get_user(user: OIDCUser = Depends(depends_authentication())):
user_id = user.sub
from fastapi import Depends, Path
from pydantic.types import UUID4
from expert_core.plugins.keycloak import depends_authentication, depends_idp, Keycloak, OIDCUser
@app.post("/chat/{item_id}/set")
async def set_user_permissions(
chat_id: UUID4 = Path(..., alias="item_id"),
user: OIDCUser = Depends(depends_authentication()),
idp: Keycloak = Depends(depends_idp)
):
_chat_id = str(chat_id)
await idp.update_permissions(user, chat_id, permissions="rw------")
from fastapi import Depends, Path
from pydantic.types import UUID4
from expert_core.plugins.keycloak import depends_permissions, OIDCUser
@app.get("/chat/{chat_id}/get")
async def check_user_permissions(
chat_id: UUID4,
user: OIDCUser = Depends(depends_permissions("rw.*", param="chat_id", in_="path")),
):
pass
from fastapi import Depends, Path
from pydantic.types import UUID4
from expert_core.plugins.keycloak import depends_authentication, depends_idp, Keycloak, OIDCUser
@app.delete("/chat/{item_id}/unset")
async def unset_user_permissions(
chat_id: UUID4 = Path(..., alias="item_id"),
user: OIDCUser = Depends(depends_authentication()),
idp: Keycloak = Depends(depends_idp)
):
_chat_id = str(chat_id)
await idp.update_permissions(user, chat_id)
# or
await idp.update_permissions(user, chat_id, permissions=None)
# or
await idp.update_permissions(user, chat_id, permissions="")
from fastapi import Depends, Path
from pydantic.types import UUID4
from expert_core.plugins.keycloak import depends_authentication, depends_idp, Keycloak, OIDCUser
@app.post("/chat/{chat_id}/message/{message_id}/set")
async def set_inherit_user_permissions(
chat_id: UUID4,
message_id: UUID4,
user: OIDCUser = Depends(depends_authentication()),
idp: Keycloak = Depends(depends_idp)
):
_chat_id = str(chat_id)
_message_id = str(message_id)
await idp.update_permissions(user, chat_id, _message_id, permissions="rwx-----")
from fastapi import Depends, Path
from pydantic.types import UUID4
from expert_core.plugins.keycloak import depends_permissions, OIDCUser
@app.get("/chat/{chat_id}/message/{message_id}/get")
async def check_inherit_user_permissions(
chat_id: UUID4,
message_id: UUID4,
user: OIDCUser = Depends(
depends_permissions(
"r.*",
param=["chat_id", "message_id"],
in_="path",
),
),
''' or
user: OIDCUser = Depends(
depends_permissions(
"r.*",
param=["chat_id", "message_id"],
in_=["path", "path"],
),
),
'''
):
pass
other examples:
depends_permissions("rw.*", param="chat_id", in_="body")
depends_permissions("rw.*", param=["chat_id", "message_id"], in_="body")
depends_permissions("rw.*", param=["chat_id", "message_id"], in_=["body", "path"])
depends_permissions("rw.*", param=["chat_id", "message_id", "user_id"], in_=["path", "query", "body"])