feat(auth): decode_token rejects legacy 4-field JWTs as WORKSPACE_MISSING

After PR4 every JWT must carry wid (workspace_id). decode_token now
returns the new TokenError.WORKSPACE_MISSING when the signature is
valid but the payload lacks wid; expired tokens still report EXPIRED
first so /auth/refresh logic stays correct. AuthErrorCode gains a
matching WORKSPACE_REQUIRED for middleware to surface to clients.

Updates 13 existing test sites that issued tokens without wid to pass
workspace_id="ws-test" + role="owner", reflecting the new contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1445043649
2026-05-12 22:22:10 +08:00
parent 54cb94c30f
commit b4bef65079
6 changed files with 97 additions and 15 deletions
+11 -1
View File
@@ -68,10 +68,20 @@ def decode_token(token: str) -> TokenPayload | TokenError:
config = get_auth_config()
try:
payload = jwt.decode(token, config.jwt_secret, algorithms=["HS256"])
return TokenPayload(**payload)
except jwt.ExpiredSignatureError:
return TokenError.EXPIRED
except jwt.InvalidSignatureError:
return TokenError.INVALID_SIGNATURE
except jwt.PyJWTError:
return TokenError.MALFORMED
# Reject legacy pre-PR4 tokens that lack the wid claim. Reported as
# WORKSPACE_MISSING (not MALFORMED) so middleware can surface a
# specific 401 telling the frontend to re-issue via /select-workspace.
if "wid" not in payload or payload.get("wid") is None:
return TokenError.WORKSPACE_MISSING
try:
return TokenPayload(**payload)
except Exception:
return TokenError.MALFORMED