JWT token parsing
Your task
Parse and structurally validate a JWT-like token WITHOUT verifying its signature. The token is three base64url segments joined by dots: header.payload.signature. Decode the header and payload as JSON. Return a dict {'valid': False} if there are not exactly three segments, if either of the first two segments is not valid base64url-encoded JSON, or if the header lacks an 'alg' or 'typ' field. Otherwise return {'valid': True, 'alg': <header alg>, 'sub': <payload's 'sub' or empty string if absent>}. Padding has been stripped from each segment.
Examples
parse_jwt_structure(token: str) -> dictExample 1
Input
parse_jwt_structure("eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiAidXNlcjEyMyIsICJuYW1lIjogIkFsaWNlIn0.sigsigsig")Output
{"valid": True, "alg": "HS256", "sub": "user123"}