Skip to content

expertspec/expert_core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expert_core

Requirements

  • python 3.10+
pip install pip install git+ssh://git@github.com/expertspec/expert_core.git@{tag}

Permissions

1. Init

from fastapi import FastAPI

from expert_core.plugins import init_keycloak


app = FastAPI()

@app.on_event("startup")
async def startup():
    await init_keycloak(app)

2. Get current user

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

3. Set user 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/{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------")

4. Check user permissions

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

5. Unset user permissions

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="")

6. Set inherit user 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-----")

7. Check inherit user permissions

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"])

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages