Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/signed-jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted committed Mar 4, 2024
2 parents 1c34b9a + ecf55b7 commit 8a1fc47
Show file tree
Hide file tree
Showing 9 changed files with 2,685 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ jobs:
- name: Typing (pyright)
run: poetry run pre-commit run pyright --all-files

# - name: Test
# run: poetry run pytest tests
- name: Test
run: poetry run pytest tests

publish-snapshot-version:
name: Publish snapshot to TestPyPI
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog
## [0.2.0](https://github.com/bakdata/python-keycloak-oauth/releases/tag/0.2.0) - Release Date: [2024-03-04]

#### Merged pull requests:

- Create Poetry package [#1](https://github.com/bakdata/python-keycloak-oauth/pull/1) ([@disrupted](https://github.com/disrupted))

- Create CI [#2](https://github.com/bakdata/python-keycloak-oauth/pull/2) ([@disrupted](https://github.com/disrupted))

- Document required usage of SessionMiddleware [#8](https://github.com/bakdata/python-keycloak-oauth/pull/8) ([@disrupted](https://github.com/disrupted))

- Create integration tests with Keycloak testcontainer [#7](https://github.com/bakdata/python-keycloak-oauth/pull/7) ([@disrupted](https://github.com/disrupted))





26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ pip install keycloak-oauth[fastapi]
```

```python
from fastapi import FastAPI
from backend.settings import settings, BASE_URL # secrets
from keycloak.oauth import KeycloakOAuth2
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,
Expand All @@ -26,7 +28,15 @@ keycloak = KeycloakOAuth2(
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:
Expand All @@ -42,9 +52,10 @@ 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 # secrets
from keycloak.oauth import KeycloakOAuth2
from backend.settings import settings, BASE_URL, SECRET_KEY # secrets
from keycloak_oauth import KeycloakOAuth2
from keycloak.starlette_admin import KeycloakAuthProvider

keycloak = KeycloakOAuth2(
Expand All @@ -60,7 +71,10 @@ admin = Admin(
title=...,
base_url=BASE_URL,
auth_provider=KeycloakAuthProvider(keycloak),
middlewares=[Middleware(SessionMiddleware, secret_key=SECRET_KEY)],
)

admin.add_view(...)
```

## Development
Expand All @@ -75,4 +89,4 @@ We are happy if you want to contribute to this project. If you find any bugs or

## License

This project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE.md) for more details.
This project is licensed under the MIT license. Have a look at the [LICENSE](LICENSE) for more details.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from starlette.routing import Route
from starlette_admin.auth import AdminUser, AuthProvider, login_not_required
from starlette_admin.base import BaseAdmin
from keycloak.oauth import KeycloakOAuth2, User
from keycloak_oauth import KeycloakOAuth2, User


class KeycloakAuthProvider(AuthProvider):
Expand Down
581 changes: 579 additions & 2 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tool.poetry]
name = "keycloak-oauth"
version = "0.1.0"
version = "0.2.0"
description = "Keycloak OAuth client for Python projects"
authors = ["bakdata <[email protected]>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/bakdata/python-keycloak-oauth"
packages = [{ include = "keycloak" }]
packages = [{ include = "keycloak_oauth" }]

[tool.poetry.dependencies]
python = "^3.10"
Expand All @@ -28,6 +28,13 @@ pre-commit = "^3.6.2"
ruff = "^0.2.2"
pyright = "^1.1.351"

[tool.poetry.group.test.dependencies]
pytest = "^8.0.1"
pytest-mock = "^3.12.0"
python-keycloak = "^3.9.0"
testcontainers-keycloak = { git = "https://github.com/TheForgottened/testcontainers-python", subdirectory = "keycloak" } # updated Keycloak container: https://github.com/testcontainers/testcontainers-python/pull/369
twill = "^3.2.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Loading

0 comments on commit 8a1fc47

Please sign in to comment.