|
| 1 | +"""Manage authentication flow for FastAPI endpoints with JWK based JWT auth.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from asyncio import Lock |
| 5 | +from typing import Any, Callable |
| 6 | + |
| 7 | +from fastapi import Request, HTTPException, status |
| 8 | +from authlib.jose import JsonWebKey, KeySet, jwt, Key |
| 9 | +from authlib.jose.errors import ( |
| 10 | + BadSignatureError, |
| 11 | + DecodeError, |
| 12 | + ExpiredTokenError, |
| 13 | + JoseError, |
| 14 | +) |
| 15 | +from cachetools import TTLCache |
| 16 | +import aiohttp |
| 17 | + |
| 18 | +from constants import ( |
| 19 | + DEFAULT_VIRTUAL_PATH, |
| 20 | +) |
| 21 | +from auth.interface import AuthInterface |
| 22 | +from auth.utils import extract_user_token |
| 23 | +from models.config import JwkConfiguration |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +# Global JWK registry to avoid re-fetching JWKs for each request. Cached for 1 |
| 28 | +# hour, keys are unlikely to change frequently. |
| 29 | +_jwk_cache: TTLCache[str, KeySet] = TTLCache(maxsize=3, ttl=3600) |
| 30 | +# Ideally this would be an RWLock, but it would require adding a dependency on |
| 31 | +# aiorwlock |
| 32 | +_jwk_cache_lock = Lock() |
| 33 | + |
| 34 | + |
| 35 | +async def get_jwk_set(url: str) -> KeySet: |
| 36 | + """Fetch the JWK set from the cache, or fetch it from the URL if not cached.""" |
| 37 | + async with _jwk_cache_lock: |
| 38 | + if url not in _jwk_cache: |
| 39 | + async with aiohttp.ClientSession() as session: |
| 40 | + # TODO(omertuc): handle connection errors, timeouts, etc. |
| 41 | + async with session.get(url) as resp: |
| 42 | + resp.raise_for_status() |
| 43 | + _jwk_cache[url] = JsonWebKey.import_key_set(await resp.json()) |
| 44 | + return _jwk_cache[url] |
| 45 | + |
| 46 | + |
| 47 | +class KeyNotFoundError(Exception): |
| 48 | + """Exception raised when a key is not found in the JWK set based on kid/alg.""" |
| 49 | + |
| 50 | + |
| 51 | +def key_resolver_func( |
| 52 | + jwk_set: KeySet, |
| 53 | +) -> Callable[[dict[str, Any], dict[str, Any]], Key]: |
| 54 | + """ |
| 55 | + Create a key resolver function. |
| 56 | +
|
| 57 | + Return a function to find a key in the given jwk_set. The function matches the |
| 58 | + signature expected by the jwt.decode key kwarg. |
| 59 | + """ |
| 60 | + |
| 61 | + def _internal(header: dict[str, Any], _payload: dict[str, Any]) -> Key: |
| 62 | + """Match kid and alg from the JWT header to the JWK set. |
| 63 | +
|
| 64 | + Resolve the key from the JWK set based on the JWT header. Also |
| 65 | + match the algorithm to make sure the algorithm stated by the user |
| 66 | + is the same algorithm the key itself expects. |
| 67 | +
|
| 68 | + # We intentionally do not use find_by_kid because it's a bad function |
| 69 | + # that doesn't take the alg into account |
| 70 | + """ |
| 71 | + if "alg" not in header: |
| 72 | + raise KeyNotFoundError("Token header missing 'alg' field") |
| 73 | + |
| 74 | + if "kid" in header: |
| 75 | + keys = [key for key in jwk_set.keys if key.kid == header.get("kid")] |
| 76 | + |
| 77 | + if len(keys) == 0: |
| 78 | + raise KeyNotFoundError( |
| 79 | + "No key found matching kid and alg in the JWK set" |
| 80 | + ) |
| 81 | + |
| 82 | + if len(keys) > 1: |
| 83 | + # This should never happen! Bad JWK set! |
| 84 | + raise KeyNotFoundError( |
| 85 | + "Internal server error, multiple keys found matching this kid" |
| 86 | + ) |
| 87 | + |
| 88 | + key = keys[0] |
| 89 | + |
| 90 | + if key["alg"] != header["alg"]: |
| 91 | + raise KeyNotFoundError( |
| 92 | + "Key found by kid does not match the algorithm in the token header" |
| 93 | + ) |
| 94 | + |
| 95 | + return key |
| 96 | + |
| 97 | + # No kid in the token header, we will try to find a key by alg |
| 98 | + keys = [key for key in jwk_set.keys if key["alg"] == header["alg"]] |
| 99 | + |
| 100 | + if len(keys) == 0: |
| 101 | + raise KeyNotFoundError("No key found matching alg in the JWK set") |
| 102 | + |
| 103 | + # Token has no kid and even we have more than one key with this algorithm - we will |
| 104 | + # return the first key which matches the algorithm, hopefully it will |
| 105 | + # match the token, but if not, unlucky - we're not going to brute-force all |
| 106 | + # keys until we find the one that matches, that makes us more vulnerable to DoS |
| 107 | + return keys[0] |
| 108 | + |
| 109 | + return _internal |
| 110 | + |
| 111 | + |
| 112 | +class JwkTokenAuthDependency(AuthInterface): # pylint: disable=too-few-public-methods |
| 113 | + """JWK AuthDependency class for JWK-based JWT authentication.""" |
| 114 | + |
| 115 | + def __init__( |
| 116 | + self, config: JwkConfiguration, virtual_path: str = DEFAULT_VIRTUAL_PATH |
| 117 | + ) -> None: |
| 118 | + """Initialize the required allowed paths for authorization checks.""" |
| 119 | + self.virtual_path: str = virtual_path |
| 120 | + self.config: JwkConfiguration = config |
| 121 | + |
| 122 | + async def __call__(self, request: Request) -> tuple[str, str, str]: |
| 123 | + """Authenticate the JWT in the headers against the keys from the JWK url.""" |
| 124 | + user_token = extract_user_token(request.headers) |
| 125 | + |
| 126 | + jwk_set = await get_jwk_set(str(self.config.url)) |
| 127 | + |
| 128 | + try: |
| 129 | + claims = jwt.decode(user_token, key=key_resolver_func(jwk_set)) |
| 130 | + except KeyNotFoundError as exc: |
| 131 | + raise HTTPException( |
| 132 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 133 | + detail="Invalid token: signed by unknown key or algorithm mismatch", |
| 134 | + ) from exc |
| 135 | + except BadSignatureError as exc: |
| 136 | + raise HTTPException( |
| 137 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 138 | + detail="Invalid token: bad signature", |
| 139 | + ) from exc |
| 140 | + except DecodeError as exc: |
| 141 | + raise HTTPException( |
| 142 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 143 | + detail="Invalid token: decode error", |
| 144 | + ) from exc |
| 145 | + except JoseError as exc: |
| 146 | + raise HTTPException( |
| 147 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 148 | + detail="Invalid token: unknown error", |
| 149 | + ) from exc |
| 150 | + except Exception as exc: |
| 151 | + raise HTTPException( |
| 152 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 153 | + detail="Internal server error", |
| 154 | + ) from exc |
| 155 | + |
| 156 | + try: |
| 157 | + claims.validate() |
| 158 | + except ExpiredTokenError as exc: |
| 159 | + raise HTTPException( |
| 160 | + status_code=status.HTTP_401_UNAUTHORIZED, detail="Token has expired" |
| 161 | + ) from exc |
| 162 | + except JoseError as exc: |
| 163 | + raise HTTPException( |
| 164 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 165 | + detail="Error validating token", |
| 166 | + ) from exc |
| 167 | + except Exception as exc: |
| 168 | + raise HTTPException( |
| 169 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 170 | + detail="Internal server error during token validation", |
| 171 | + ) from exc |
| 172 | + |
| 173 | + try: |
| 174 | + user_id: str = claims[self.config.jwt_configuration.user_id_claim] |
| 175 | + except KeyError as exc: |
| 176 | + raise HTTPException( |
| 177 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 178 | + detail=f"Token missing claim: {self.config.jwt_configuration.user_id_claim}", |
| 179 | + ) from exc |
| 180 | + |
| 181 | + try: |
| 182 | + username: str = claims[self.config.jwt_configuration.username_claim] |
| 183 | + except KeyError as exc: |
| 184 | + raise HTTPException( |
| 185 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 186 | + detail=f"Token missing claim: {self.config.jwt_configuration.username_claim}", |
| 187 | + ) from exc |
| 188 | + |
| 189 | + logger.info("Successfully authenticated user %s (ID: %s)", username, user_id) |
| 190 | + |
| 191 | + return user_id, username, user_token |
0 commit comments