Archives de catégorie : Python

Integrating OpenIdConnect with FastAPI’s OpenAPI UI

FastAPI is a powerful tool for building APIs with Python, but it doesn’t fully support OpenIdConnect out of the box. However, with a bit of tweaking, you can set up FastAPI’s OpenAPI UI (Swagger UI) to use OpenIdConnect authentication across all routes. This is particularly useful if you’re using an identity provider like Keycloak without having to set it inside your routes.

This is particularly useful in combination with the fastapi keycloak plugin, which doesn’t parameterize the openapi part.

What is OpenIdConnect?


OpenIdConnect is an identity layer on top of the OAuth 2.0 protocol, allowing clients to verify the identity of the end-user based on the authentication performed by an authorization server.

Setting Up OpenIdConnect in FastAPI for OpenApi


To set up OpenIdConnect in FastAPI, you need to modify the OpenAPI schema. Here’s an example:

app = FastAPI()
...
if app.openapi_schema:
    app.openapi_schema["components"]["securitySchemes"]["openId"] = {
        "type": "openIdConnect",
        "openIdConnectUrl": "https://yourkeycloakurl.com/realms/yourrealm/.well-known/openid-configuration",
    }
    app.openapi_schema["security"] = [{"openId": ["read", "write"]}]


In this code:

We’re adding a new security scheme to the OpenAPI schema. The type is openIdConnect, and the openIdConnectUrl is the well-known configuration URL of your OpenIdConnect provider (like Keycloak).
We’re setting the security property of the schema to use the OpenIdConnect scheme. The [« read », « write »] array represents the scopes that the OpenIdConnect provider should request. In this case, all our endpoints.


Wrapping Up


This approach allows you to set up OpenIdConnect authentication across all routes in FastAPI’s OpenAPI UI, without needing to manually configure each route. It’s a handy trick if you’re using an identity provider like Keycloak and want to leverage OpenIdConnect for your FastAPI application.

References

Python avec Pycharm

Pour mes débuts en Python, J’utilise l’IDE pycharm disponible sur :

http://www.jetbrains.com/pycharm/download

Cet IDE est basé sur la plateforme de IntelliJ IDEA de Jetbrains. Il est très complet et propose les même fonctionnalités que Resharper pour C#.

Par rapport Eclipse ou Wing IDE, rien que l’ajout des imports me fait l’aimer.

On peut bien sûr l’améliorer en rajouter des live templates comme le fait Peter Hoffmann sur son excellent blog:

http://peter-hoffmann.com/2010/python-live-templates-for-pycharm.html

Mes raccourcis préférés:

Ctrl+N : chercher une classe

Ctrl+shift+N: chercher un fichier

Ctrl+shift+A: chercher une action

Ctrl+Alt+R: lancer manage.py

La liste complète des raccourcis: http://www.jetbrains.com/pycharm/docs/PyCharm_ReferenceCard.pdf

La liste complète des fonctionnalités:

http://www.jetbrains.com/pycharm/features/index.html

Si vous avez des problèmes de performance essayez toujours les trucs et astuces pour intelliJ:

http://j2eeblogger.blogspot.com/2007/10/intellij-idea-performance.html