keycloak-oauth


Namekeycloak-oauth JSON
Version 0.3.0 PyPI version JSON
download
home_pagehttps://github.com/bakdata/python-keycloak-oauth
SummaryKeycloak OAuth client for Python projects
upload_time2024-03-18 11:24:13
maintainer
docs_urlNone
authorbakdata
requires_python>=3.10,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # python-keycloak-oauth

Keycloak OAuth client for Python projects with optional integrations for [FastAPI](https://github.com/tiangolo/fastapi) & [Starlette-Admin](https://github.com/jowilf/starlette-admin).

## Getting started

### FastAPI

```sh
pip install keycloak-oauth[fastapi]
```

```python
from typing import Annotated
from fastapi import FastAPI, Request, Depends
from starlette.middleware.sessions import SessionMiddleware
from backend.settings import settings, BASE_URL, SECRET_KEY  # secrets
from keycloak_oauth import KeycloakOAuth2

keycloak = KeycloakOAuth2(
    client_id=settings.keycloak.client_id,
    client_secret=settings.keycloak.client_secret,
    server_metadata_url=str(settings.keycloak.server_metadata_url),
    client_kwargs=settings.keycloak.client_kwargs,
    base_url=BASE_URL,
)
# create router and register API endpoints
keycloak.setup_fastapi_routes()

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
app.include_router(keycloak.router, prefix="/auth")

@app.get("/")
def index(
    request: Request, user: Annotated[User, Depends(KeycloakOAuth2.get_user)]
):
    """Protected endpoint, will return 401 Unauthorized if not signed in."""
    return f"Hello {user.name}"
```

We now expose the API endpoints for Keycloak:

- `/auth/login`: redirect to Keycloak login page
- `/auth/callback`: authorize user with Keycloak access token
- `/auth/logout`: deauthorize user and redirect to the logout page

### Starlette-Admin

```sh
pip install keycloak-oauth[starlette-admin]
```

```python
from starlette.middleware.sessions import SessionMiddleware
from starlette_admin.contrib.sqla import Admin
from backend.settings import settings, BASE_URL, SECRET_KEY  # secrets
from keycloak_oauth import KeycloakOAuth2
from keycloak.starlette_admin import KeycloakAuthProvider

keycloak = KeycloakOAuth2(
    client_id=settings.keycloak.client_id,
    client_secret=settings.keycloak.client_secret,
    server_metadata_url=str(settings.keycloak.server_metadata_url),
    client_kwargs=settings.keycloak.client_kwargs,
    base_url=BASE_URL,
)

admin = Admin(
    # engine,
    title=...,
    base_url=BASE_URL,
    auth_provider=KeycloakAuthProvider(keycloak),
    middlewares=[Middleware(SessionMiddleware, secret_key=SECRET_KEY)],
)

admin.add_view(...)
```

## Development

If you want to contribute to this project, you can simply clone the repository and run `poetry install --all-extras`.

Please also run `pre-commit install` for linting and enforcing a consistent code style.

## Contributing

We are happy if you want to contribute to this project. If you find any bugs or have suggestions for improvements, please open an issue. We are also happy to accept your PRs. Just open an issue beforehand and let us know what you want to do and why.

## License

This project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE) for more details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bakdata/python-keycloak-oauth",
    "name": "keycloak-oauth",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "bakdata",
    "author_email": "opensource@bakdata.com",
    "download_url": "https://files.pythonhosted.org/packages/49/b8/508ad5c06adc878f5357b2d94150bac2a5c6a6c54176a094e781201d04c1/keycloak_oauth-0.3.0.tar.gz",
    "platform": null,
    "description": "# python-keycloak-oauth\n\nKeycloak OAuth client for Python projects with optional integrations for [FastAPI](https://github.com/tiangolo/fastapi) & [Starlette-Admin](https://github.com/jowilf/starlette-admin).\n\n## Getting started\n\n### FastAPI\n\n```sh\npip install keycloak-oauth[fastapi]\n```\n\n```python\nfrom typing import Annotated\nfrom fastapi import FastAPI, Request, Depends\nfrom starlette.middleware.sessions import SessionMiddleware\nfrom backend.settings import settings, BASE_URL, SECRET_KEY  # secrets\nfrom keycloak_oauth import KeycloakOAuth2\n\nkeycloak = KeycloakOAuth2(\n    client_id=settings.keycloak.client_id,\n    client_secret=settings.keycloak.client_secret,\n    server_metadata_url=str(settings.keycloak.server_metadata_url),\n    client_kwargs=settings.keycloak.client_kwargs,\n    base_url=BASE_URL,\n)\n# create router and register API endpoints\nkeycloak.setup_fastapi_routes()\n\napp = FastAPI()\napp.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)\napp.include_router(keycloak.router, prefix=\"/auth\")\n\n@app.get(\"/\")\ndef index(\n    request: Request, user: Annotated[User, Depends(KeycloakOAuth2.get_user)]\n):\n    \"\"\"Protected endpoint, will return 401 Unauthorized if not signed in.\"\"\"\n    return f\"Hello {user.name}\"\n```\n\nWe now expose the API endpoints for Keycloak:\n\n- `/auth/login`: redirect to Keycloak login page\n- `/auth/callback`: authorize user with Keycloak access token\n- `/auth/logout`: deauthorize user and redirect to the logout page\n\n### Starlette-Admin\n\n```sh\npip install keycloak-oauth[starlette-admin]\n```\n\n```python\nfrom starlette.middleware.sessions import SessionMiddleware\nfrom starlette_admin.contrib.sqla import Admin\nfrom backend.settings import settings, BASE_URL, SECRET_KEY  # secrets\nfrom keycloak_oauth import KeycloakOAuth2\nfrom keycloak.starlette_admin import KeycloakAuthProvider\n\nkeycloak = KeycloakOAuth2(\n    client_id=settings.keycloak.client_id,\n    client_secret=settings.keycloak.client_secret,\n    server_metadata_url=str(settings.keycloak.server_metadata_url),\n    client_kwargs=settings.keycloak.client_kwargs,\n    base_url=BASE_URL,\n)\n\nadmin = Admin(\n    # engine,\n    title=...,\n    base_url=BASE_URL,\n    auth_provider=KeycloakAuthProvider(keycloak),\n    middlewares=[Middleware(SessionMiddleware, secret_key=SECRET_KEY)],\n)\n\nadmin.add_view(...)\n```\n\n## Development\n\nIf you want to contribute to this project, you can simply clone the repository and run `poetry install --all-extras`.\n\nPlease also run `pre-commit install` for linting and enforcing a consistent code style.\n\n## Contributing\n\nWe are happy if you want to contribute to this project. If you find any bugs or have suggestions for improvements, please open an issue. We are also happy to accept your PRs. Just open an issue beforehand and let us know what you want to do and why.\n\n## License\n\nThis project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE) for more details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Keycloak OAuth client for Python projects",
    "version": "0.3.0",
    "project_urls": {
        "Homepage": "https://github.com/bakdata/python-keycloak-oauth",
        "Repository": "https://github.com/bakdata/python-keycloak-oauth"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d32beda444c27607b3e00765c7f6f86f2fcad37599fa690e6cb7859f7d82ff08",
                "md5": "63b198f51feb797f914a8c38457cd2a1",
                "sha256": "ff6dab300fb1defca0b93f108765d7bcfdb2495617d411d92d9cb89e3978ed9d"
            },
            "downloads": -1,
            "filename": "keycloak_oauth-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "63b198f51feb797f914a8c38457cd2a1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 6010,
            "upload_time": "2024-03-18T11:24:10",
            "upload_time_iso_8601": "2024-03-18T11:24:10.058105Z",
            "url": "https://files.pythonhosted.org/packages/d3/2b/eda444c27607b3e00765c7f6f86f2fcad37599fa690e6cb7859f7d82ff08/keycloak_oauth-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "49b8508ad5c06adc878f5357b2d94150bac2a5c6a6c54176a094e781201d04c1",
                "md5": "50f0552a862a60be5d54a1974c641b94",
                "sha256": "ef361ffe3ce872da0ec44447cba18d39b8fde3a31b17135094eb64210f8213a5"
            },
            "downloads": -1,
            "filename": "keycloak_oauth-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "50f0552a862a60be5d54a1974c641b94",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 5207,
            "upload_time": "2024-03-18T11:24:13",
            "upload_time_iso_8601": "2024-03-18T11:24:13.647599Z",
            "url": "https://files.pythonhosted.org/packages/49/b8/508ad5c06adc878f5357b2d94150bac2a5c6a6c54176a094e781201d04c1/keycloak_oauth-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-18 11:24:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bakdata",
    "github_project": "python-keycloak-oauth",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "keycloak-oauth"
}
        
Elapsed time: 0.19802s