fastapi-oidc-backend


Namefastapi-oidc-backend JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/BlackVoid/fastapi-oidc-backend
SummaryBuild resource servers with FastAPI
upload_time2024-09-13 09:28:15
maintainerNone
docs_urlNone
authorFelix Gustavsson
requires_python<4.0.0,>=3.11.2
licenseMIT
keywords fastapi authentication oidc openidconnect
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastAPI Resource Backend

Build an OIDC resource server using FastAPI.

Your aplication receives the claims decoded from the access token.

Fork of fastapi-resource-server

# Usage

Run keycloak on port 8888:

```sh
docker container run --name auth-server -d -p 8080:8080 \
    -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin \
    quay.io/keycloak/keycloak:latest
```

Install dependencies

```sh
pip install fastapi fastapi_oidc_backend uvicorn
```

Create the main.py module

```python
from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, Security
from pydantic import BaseModel

from fastapi_oidc_backend.security import OidcResourceServer
from fastapi_oidc_backend.models import JwtKwargs

oidc_config = JwtKwargs(audience="myclient", issuer="http://localhost:8888/realms/myrealm")

@asynccontextmanager
async def app_startup(_app: FastAPI):
    await auth_scheme.load_configuration()
    yield

app = FastAPI(lifespan=app_startup,
              swagger_ui_init_oauth={
                  "clientId": oidc_config.audience,
                  "usePkceWithAuthorizationCodeGrant": True
              })
auth_scheme = OidcResourceServer(
    oidc_config,
    scheme_name="Keycloak",
)


class User(BaseModel):
    username: str
    given_name: str
    family_name: str
    email: str


def get_current_user(claims: dict = Security(auth_scheme)):
    claims.update(username=claims["preferred_username"])
    user = User.parse_obj(claims)
    return user


@app.get("/users/me")
def read_current_user(current_user: User = Depends(get_current_user)):
    return current_user
```

Run the application

```sh
uvicorn main:app
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/BlackVoid/fastapi-oidc-backend",
    "name": "fastapi-oidc-backend",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0.0,>=3.11.2",
    "maintainer_email": null,
    "keywords": "fastapi, authentication, oidc, openidconnect",
    "author": "Felix Gustavsson",
    "author_email": "felix@0b1.se",
    "download_url": "https://files.pythonhosted.org/packages/b4/8d/76508ba73410431007db83e39bbbaffb5919b1cdaf41c13c9b4f66aa9cf8/fastapi_oidc_backend-0.6.0.tar.gz",
    "platform": null,
    "description": "# FastAPI Resource Backend\n\nBuild an OIDC resource server using FastAPI.\n\nYour aplication receives the claims decoded from the access token.\n\nFork of fastapi-resource-server\n\n# Usage\n\nRun keycloak on port 8888:\n\n```sh\ndocker container run --name auth-server -d -p 8080:8080 \\\n    -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin \\\n    quay.io/keycloak/keycloak:latest\n```\n\nInstall dependencies\n\n```sh\npip install fastapi fastapi_oidc_backend uvicorn\n```\n\nCreate the main.py module\n\n```python\nfrom contextlib import asynccontextmanager\nfrom fastapi import Depends, FastAPI, Security\nfrom pydantic import BaseModel\n\nfrom fastapi_oidc_backend.security import OidcResourceServer\nfrom fastapi_oidc_backend.models import JwtKwargs\n\noidc_config = JwtKwargs(audience=\"myclient\", issuer=\"http://localhost:8888/realms/myrealm\")\n\n@asynccontextmanager\nasync def app_startup(_app: FastAPI):\n    await auth_scheme.load_configuration()\n    yield\n\napp = FastAPI(lifespan=app_startup,\n              swagger_ui_init_oauth={\n                  \"clientId\": oidc_config.audience,\n                  \"usePkceWithAuthorizationCodeGrant\": True\n              })\nauth_scheme = OidcResourceServer(\n    oidc_config,\n    scheme_name=\"Keycloak\",\n)\n\n\nclass User(BaseModel):\n    username: str\n    given_name: str\n    family_name: str\n    email: str\n\n\ndef get_current_user(claims: dict = Security(auth_scheme)):\n    claims.update(username=claims[\"preferred_username\"])\n    user = User.parse_obj(claims)\n    return user\n\n\n@app.get(\"/users/me\")\ndef read_current_user(current_user: User = Depends(get_current_user)):\n    return current_user\n```\n\nRun the application\n\n```sh\nuvicorn main:app\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Build resource servers with FastAPI",
    "version": "0.6.0",
    "project_urls": {
        "Homepage": "https://github.com/BlackVoid/fastapi-oidc-backend",
        "Repository": "https://github.com/BlackVoid/fastapi-oidc-backend"
    },
    "split_keywords": [
        "fastapi",
        " authentication",
        " oidc",
        " openidconnect"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3a31873a4b7697bea4a5c4b4177c79ba62c4e7146eef3cbcbfdfe065ff3891f6",
                "md5": "4b95a72b16b0a9894370ddf43dc9d2f7",
                "sha256": "91add7f72d1c14df0565f974080ebde4fef4836ca4fbf4df3719a504946daeff"
            },
            "downloads": -1,
            "filename": "fastapi_oidc_backend-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4b95a72b16b0a9894370ddf43dc9d2f7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.11.2",
            "size": 5552,
            "upload_time": "2024-09-13T09:28:14",
            "upload_time_iso_8601": "2024-09-13T09:28:14.522660Z",
            "url": "https://files.pythonhosted.org/packages/3a/31/873a4b7697bea4a5c4b4177c79ba62c4e7146eef3cbcbfdfe065ff3891f6/fastapi_oidc_backend-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b48d76508ba73410431007db83e39bbbaffb5919b1cdaf41c13c9b4f66aa9cf8",
                "md5": "38f9f8fe07e79d51a41286466918c8de",
                "sha256": "d8889ebcf9223047444afba3d3add8ea9c22ce12ec3c9cd30e4a85fe4040e5a0"
            },
            "downloads": -1,
            "filename": "fastapi_oidc_backend-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "38f9f8fe07e79d51a41286466918c8de",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.11.2",
            "size": 4497,
            "upload_time": "2024-09-13T09:28:15",
            "upload_time_iso_8601": "2024-09-13T09:28:15.422239Z",
            "url": "https://files.pythonhosted.org/packages/b4/8d/76508ba73410431007db83e39bbbaffb5919b1cdaf41c13c9b4f66aa9cf8/fastapi_oidc_backend-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-13 09:28:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "BlackVoid",
    "github_project": "fastapi-oidc-backend",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastapi-oidc-backend"
}
        
Elapsed time: 9.33074s