JWTStr¶
JWTStr
¶
Bases: str
A string subclass with structural validation for JWT (JSON Web Token) format.
Only the structure is checked: three urlsafe-base64 segments whose header
and payload decode to JSON objects. The signature is neither verified nor
inspected, so the payload of a JWTStr built from untrusted input is
attacker-controlled data — use JWTModel.from_token() to verify.
Examples:
from pydantic import BaseModel
from pydantic_jwt import JWTStr
class Auth(BaseModel):
token: JWTStr
auth = Auth(token='eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dGVzdA')
print(auth.token.header)
#> {'alg': 'HS256'}
print(auth.token.algorithm)
#> 'HS256'
print(auth.token.payload)
#> {'sub': '1234567890'}
print(auth.token.signature)
#> b'test'
algorithm
property
¶
Return the alg value from the header, or None if it is absent.
validate
classmethod
¶
Return whether the value is structurally a valid JWT, without raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jwt
|
str
|
The value to inspect. A non-string returns |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|