feat: add a Keycloak auth handler - #51
Open
mxw94 wants to merge 4 commits into
Open
Conversation
Servers that delegate authentication to Keycloak need a bearer token rather than basic auth. KeycloakAuthHandler obtains an access token from Keycloak via the password grant and caches it until shortly before it expires, so a client that sends many requests does not request a new token per request. Ported from the thingsboard-frost-adapter, based on python-keycloak.
A docker compose stack with a Keycloak-protected FROST server, a matching realm import, and a session fixture that starts it (mirroring the basic-auth fixture). The happy-path test authenticates through KeycloakAuthHandler and creates and reads a Thing.
Keep the Keycloak compose file and its realm import next to the existing FROST compose file, as docker-compose.keycloak.yml.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a Keycloak auth handler
Adds
KeycloakAuthHandler, anAuthHandlerfor FROST servers that delegateauthentication to Keycloak. It obtains an access token
from Keycloak via the password grant and sends it as a bearer token, caching the
token until shortly before it expires so a client that sends many requests does
not request a new token per request.
Usage mirrors the existing basic-auth handler:
Changes
frost_sta_client/service/keycloak_auth_handler.py— the handler, built onpython-keycloak(added torequirements.txtandsetup.py).fsc.KeycloakAuthHandler.tests/test_keycloak_integration.pyplus a session fixturein
tests/conftest.pythat starts a Keycloak-protected FROST server fromfrost_server/docker-compose.keycloak.yml(withfrost_server/realm-frost-test.json),mirroring the existing
frost_serverfixture. The happy-path test authenticatesthrough
KeycloakAuthHandlerand creates and reads a Thing.Testing
pytest— passes (integration tests skipped without the flag).FROST_STA_CLIENT_RUN_INTEGRATION=1 pytest tests/test_keycloak_integration.pybrings up the stack, authenticates, and performs an authenticated create/read.
Notes for reviewers
(
EXTERNAL_IP, filled in by the fixture) so the token issuer is identicalwhether the token is minted on the host or validated inside the FROST
container.