
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Keycloak OAuth client for Python projects with optional integrations for FastAPI & Starlette-Admin.
pip install keycloak-oauth[fastapi]
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 pagepip install keycloak-oauth[starlette-admin]
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(...)
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.
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.
This project is licensed under the MIT license. Have a look at the LICENSE for more details.
FAQs
Keycloak OAuth client for Python projects
We found that keycloak-oauth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.