This repository contains a Traefik middleware for authentication via Keycloak, with a local debug server to test it outside of Traefik.
traefik-auth-middleware/
|── auth_test.go # Unit tests
├── auth.go # Middleware code
├── go.mod
└── debug/
└── main.go # Local debug server
auth.go → middleware used by Traefik.debug/main.go → minimal HTTP server to test the middleware and view Keycloak responses.Install tools and dev dependencies
mise installmise run install_yaegi
mise run test
mise run formatmise run lint
go run debug/.
8080.Using curl:
curl "http://localhost:8080?shop_name=test&api_key=secret"
{"message": "OK - middleware passed","token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI..."}
If the request is malformed or authentication fails, the middleware returns an appropriate HTTP status code and an error message.
The middleware outputs JSON logs to standard output:
{"level": "INFO","message": "Traefik-auth-middleware - Fetching auth token success for shop: test","plugin_name": "sw-auth-plugin"}
These logs allow you to track:
main.go is only used for local debugging.CreateConfig() for debugging.shop_name and api_key) are present.status code.Middleware debug server started on :8080
{"level":"INFO","message":"Traefik-auth-middleware - Fetching auth token success for shop: test","plugin_name":"sw-auth-plugin"}
HTTP Response:
{"message": "OK - middleware passed","token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI..."}