Skip to content

JWT validator

Provider-agnostic OIDC JWT validation. Supports RS256/384/512, PS256/384/512, ES256/384/512, EdDSA.

jwt_validator

JWKS-based JWT validation for any standard OIDC provider (Keycloak, Okta, Microsoft Entra ID, Duende, Auth0, …).

The issuer URL's /.well-known/openid-configuration is fetched once and cached; the jwks_uri within it is used to verify token signatures. No provider-specific code — pure OIDC / RFC 7517.

JwtFailReason

Bases: Enum

Why JWT validation failed — used to emit the correct WWW-Authenticate error.

validate_jwt async

validate_jwt(
    token: str, issuer_url: str
) -> tuple[dict | None, JwtFailReason | None]

Validate a signed JWT via the issuer's JWKS endpoint.

The issuer's /.well-known/openid-configuration is fetched (and cached) to locate the jwks_uri automatically — no need to hard-code a certs URL.

Returns (claims, None) on success. Returns (None, JwtFailReason.EXPIRED) when the token has expired. Returns (None, JwtFailReason.INVALID) for any other failure.

Source code in mcpauthkit/jwt_validator.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
async def validate_jwt(token: str, issuer_url: str) -> tuple[dict | None, JwtFailReason | None]:
    """
    Validate a signed JWT via the issuer's JWKS endpoint.

    The issuer's /.well-known/openid-configuration is fetched (and cached)
    to locate the jwks_uri automatically — no need to hard-code a certs URL.

    Returns (claims, None) on success.
    Returns (None, JwtFailReason.EXPIRED) when the token has expired.
    Returns (None, JwtFailReason.INVALID) for any other failure.
    """
    try:
        header = jwt.get_unverified_header(token)
        alg = header.get("alg", "RS256")
        if alg not in _ALLOWED_ALGORITHMS:
            logger.warning("JWT uses disallowed algorithm: %s", alg)
            return None, JwtFailReason.INVALID

        oidc = await _get_oidc_config(issuer_url)
        jwks = await _get_jwks(oidc["jwks_uri"])

        claims = jwt.decode(
            token,
            jwks,
            algorithms=list(_ALLOWED_ALGORITHMS),
            options={"verify_aud": False},
        )

        # Enforce issuer
        expected_issuer = oidc.get("issuer", "")
        if expected_issuer and claims.get("iss") != expected_issuer:
            logger.warning(
                "JWT issuer mismatch: expected '%s', got '%s'",
                expected_issuer,
                claims.get("iss"),
            )
            return None, JwtFailReason.INVALID

        return claims, None

    except ExpiredSignatureError:
        logger.debug("JWT validation failed: token expired")
        return None, JwtFailReason.EXPIRED
    except JWTError as exc:
        logger.debug("JWT validation failed: %s", exc)
        return None, JwtFailReason.INVALID
    except Exception as exc:
        logger.warning("Unexpected error during JWT validation: %s", exc)
        return None, JwtFailReason.INVALID