Skip to content

Version 0.4.0

Compare
Choose a tag to compare
@waza-ari waza-ari released this 18 Apr 14:57
· 47 commits to main since this release

Overview

Changes

  • New setup method to add middleware to FastAPI app - enabling additional features
  • Error response code added to OpenAPI spec by default
  • JSONResponse instead of PlainTextResponse following a consistent schema

New Features

  • Swagger UI Authentication

Important Changes

New Setup Method

This version introduces a new method to configure the middleware for your FastAPI application. The old method of adding the Middleware class directly is still available and supported, therefore consider this as a new method adding new options while maintaining backwards compatibility.

Old usage

from fastapi_keycloak_middleware import KeycloakConfiguration, KeycloakMiddleware

# Set up Keycloak
 keycloak_config = KeycloakConfiguration(
   ...
 )

 app = FastAPI()

 # Add middleware with basic config
 app.add_middleware(
     KeycloakMiddleware,
     keycloak_configuration=keycloak_config,
 )

New usage

from fastapi_keycloak_middleware import KeycloakConfiguration, setup_keycloak_middleware

# Set up Keycloak
 keycloak_config = KeycloakConfiguration(
   ...
 )

 app = FastAPI()

 # Add middleware with basic config
 setup_keycloak_middleware(
     app,
     keycloak_configuration=keycloak_config,
 )

This allows us to make more sophisticated changes to the app, such as adding proper responses and add Swagger UI authentication.

Error Response codes

Error response codes are now enabled by default. See below for details.

New Feature

Swagger UI Authentication

It is now possible to properly configure the Swagger UI to authenticate against Keycloak and send requests with the correct token to the backend. Therefore it is now possible to actually test the API using Swagger UI. It requires the new setup method mentioned above and needs to be explicitly enabled.

** Example **:

 keycloak_config = KeycloakConfiguration(
     url="https://sso.your-keycloak.com/auth/",
     realm="<Realm Name>",
     client_id="<Client ID>",
     client_secret="<Client Secret>",
     swagger_client_id="<Swagger Client ID>",
     swagger_auth_scopes=["openid", "profile"], # Optional
     swagger_auth_pkce=True, # Optional
     swagger_scheme_name="keycloak" # Optional
 )

setup_keycloak_middleware(
    app,
    keycloak_configuration=keycloak_config,
    add_swagger_auth=True
)

Please refer to the documentation for additional details.

Error Response codes

The library now has the ability to automatically add 401 and 403 error responses to the OpenAPI spec. This is mainly useful when working with client generators that automatically create client SDKs based on the OpenAPI spec. This feature is enabled by default, but doesn't overwrite any existing responses you may have added to your application.

The default behaviour can be disabled by setting the add_exception_response to False when calling setup_keycloak_middleware:

setup_keycloak_middleware(
    app,
    keycloak_configuration=keycloak_config,
    add_exception_response=False
)

Full Changelog

  • fix: properly use Pydantic field defaults by @waza-ari in #36
  • Switch from PlainTextResponse to JSONResponse by @waza-ari in #37
  • fix: middleware typing was incorrectly accepting a FastAPI app, while… by @waza-ari in #38
  • Add initialisation helper by @waza-ari in #39

Full Changelog: v0.3.1...v0.4.0