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:
@@ -21,6 +21,7 @@ class AuthErrorCode(StrEnum):
|
||||
PROVIDER_NOT_FOUND = "provider_not_found"
|
||||
NOT_AUTHENTICATED = "not_authenticated"
|
||||
SYSTEM_ALREADY_INITIALIZED = "system_already_initialized"
|
||||
WORKSPACE_REQUIRED = "workspace_required"
|
||||
|
||||
|
||||
class TokenError(StrEnum):
|
||||
@@ -29,6 +30,7 @@ class TokenError(StrEnum):
|
||||
EXPIRED = "expired"
|
||||
INVALID_SIGNATURE = "invalid_signature"
|
||||
MALFORMED = "malformed"
|
||||
WORKSPACE_MISSING = "workspace_missing"
|
||||
|
||||
|
||||
class AuthErrorResponse(BaseModel):
|
||||
@@ -42,4 +44,6 @@ def token_error_to_code(err: TokenError) -> AuthErrorCode:
|
||||
"""Map TokenError to AuthErrorCode — single source of truth."""
|
||||
if err == TokenError.EXPIRED:
|
||||
return AuthErrorCode.TOKEN_EXPIRED
|
||||
if err == TokenError.WORKSPACE_MISSING:
|
||||
return AuthErrorCode.WORKSPACE_REQUIRED
|
||||
return AuthErrorCode.TOKEN_INVALID
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user