[Python] Verify Keycloak Token from React Frontend in my Flask Python Server using a...

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I want to verify a Token that is sent to my server using authlib and Keycloak. This is my current setup, although it is not working. Somehow the token in the validate_token() method is always None.

    class ClientCredsTokenValidator(JWTBearerTokenValidator):
    def __init__(self, issuer):
    certs = keycloak_openid.certs()
    public_key = JsonWebKey.import_key_set(certs)
    super(ClientCredsTokenValidator, self).__init__(public_key)
    self.claims_options = {
    "exp": {"essential": True},
    "iss": {"essential": True, "value": issuer},
    }

    def validate_token(self, token, scopes, request):
    super(ClientCredsTokenValidator, self).validate_token(token, scopes, request)


    require_auth = ResourceProtector()
    validator = ClientCredsTokenValidator(KEYCLOAK_ISSUER)
    require_auth.register_token_validator(validator)


    My app route I try to fetch looks like this:

    @app.route('/loans/all', methods=['GET'])
    @require_auth(None)
    def get_all_loans():
    ...


    The keycloak_openid.certs() is a package fetch the certificates from my keycloak instance, which is hosted in a docker container on my local machine.

    What am I missing?

    Continue reading...

Compartilhe esta Página