AuthXConfig class, which extends Pydantic’s BaseSettings. This allows configuration through environment variables, .env files, or direct instantiation.
Basic configuration
Create a configuration instance and pass it to AuthX:Environment variables
All configuration options can be set via environment variables:config.py:21 for the AuthXConfig class definition.
Token configuration
Expiration settings
Control how long tokens remain valid:Optional[timedelta]Default:
timedelta(minutes=15) for access, timedelta(days=20) for refreshReference:
config.py:35 and config.py:48
Set to
None for tokens that never expire. This is not recommended for production.Algorithm and keys
Configure the cryptographic algorithm and keys for token signing:- JWT_ALGORITHM
- JWT_SECRET_KEY
- JWT_PUBLIC_KEY
- JWT_PRIVATE_KEY
Type:
Default:
Options:
Reference:
AlgorithmTypeDefault:
"HS256"Options:
HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512Reference:
config.py:36JWT claims
Configure standard JWT claims:JWT_IDENTITY_CLAIM(str, default:"sub"): Claim for user identity -config.py:45JWT_ENCODE_ISSUER(Optional[str], default:None): Issuer for encoding -config.py:42JWT_DECODE_ISSUER(Optional[str], default:None): Expected issuer for decoding -config.py:39JWT_ENCODE_AUDIENCE(Optional[StringOrSequence], default:None): Audience for encoding -config.py:41JWT_DECODE_AUDIENCE(Optional[StringOrSequence], default:None): Expected audience for decoding -config.py:38JWT_ENCODE_NBF(bool, default:True): Include not-before claim -config.py:43JWT_DECODE_LEEWAY(Optional[int], default:0): Clock skew tolerance in seconds -config.py:40JWT_DECODE_ALGORITHMS(Sequence[AlgorithmType], default:["HS256"]): Allowed algorithms for decoding -config.py:37
Error messages
Customize error message format:strDefault:
"msg"Reference:
config.py:44
Token location configuration
Configure where tokens are sent and received:TokenLocations (Sequence of “headers”, “cookies”, “json”, “query”)Default:
["headers"]Reference:
config.py:50
Header options
Configure header-based authentication:JWT_HEADER_NAME(str, default:"Authorization"): Header name -config.py:52JWT_HEADER_TYPE(str, default:"Bearer"): Token prefix -config.py:53
Cookie options
Configure cookie-based authentication:JWT_ACCESS_COOKIE_NAME(str, default:"access_token_cookie"): Access token cookie name -config.py:55JWT_ACCESS_COOKIE_PATH(str, default:"/"): Access token cookie path -config.py:56JWT_REFRESH_COOKIE_NAME(str, default:"refresh_token_cookie"): Refresh token cookie name -config.py:63JWT_REFRESH_COOKIE_PATH(str, default:"/"): Refresh token cookie path -config.py:64JWT_COOKIE_SECURE(bool, default:True): Require HTTPS -config.py:61JWT_COOKIE_HTTP_ONLY(bool, default:True): Prevent JavaScript access -config.py:62JWT_COOKIE_SAMESITE(Optional[SameSitePolicy], default:"lax"): SameSite policy -config.py:60JWT_COOKIE_DOMAIN(Optional[str], default:None): Cookie domain -config.py:58JWT_COOKIE_MAX_AGE(Optional[int], default:None): Max age in seconds -config.py:59JWT_SESSION_COOKIE(bool, default:True): Session cookie -config.py:65
CSRF protection
Configure CSRF protection for cookie-based authentication:JWT_COOKIE_CSRF_PROTECT(bool, default:True): Enable CSRF protection -config.py:57JWT_CSRF_IN_COOKIES(bool, default:True): Store CSRF token in cookies -config.py:72JWT_ACCESS_CSRF_COOKIE_NAME(str, default:"csrf_access_token"): Access CSRF cookie name -config.py:67JWT_ACCESS_CSRF_COOKIE_PATH(str, default:"/"): Access CSRF cookie path -config.py:68JWT_REFRESH_CSRF_COOKIE_NAME(str, default:"csrf_refresh_token"): Refresh CSRF cookie name -config.py:74JWT_REFRESH_CSRF_COOKIE_PATH(str, default:"/"): Refresh CSRF cookie path -config.py:75JWT_ACCESS_CSRF_HEADER_NAME(str, default:"X-CSRF-TOKEN"): Access CSRF header name -config.py:70JWT_REFRESH_CSRF_HEADER_NAME(str, default:"X-CSRF-TOKEN"): Refresh CSRF header name -config.py:77JWT_ACCESS_CSRF_FIELD_NAME(str, default:"csrf_token"): Access CSRF form field -config.py:69JWT_REFRESH_CSRF_FIELD_NAME(str, default:"csrf_token"): Refresh CSRF form field -config.py:76JWT_CSRF_CHECK_FORM(bool, default:False): Check form data for CSRF -config.py:71JWT_CSRF_METHODS(HTTPMethods, default:["POST", "PUT", "PATCH", "DELETE"]): Methods requiring CSRF -config.py:73
Query parameter options
Configure query parameter authentication:strDefault:
"token"Reference:
config.py:79
JSON body options
Configure JSON body authentication:JWT_JSON_KEY(str, default:"access_token"): Access token key in JSON -config.py:81JWT_REFRESH_JSON_KEY(str, default:"refresh_token"): Refresh token key in JSON -config.py:82
Implicit refresh configuration
Configure automatic token refresh for cookie-based authentication:JWT_IMPLICIT_REFRESH_DELTATIME(timedelta, default:timedelta(minutes=10)): When to refresh -config.py:89JWT_IMPLICIT_REFRESH_ROUTE_EXCLUDE(list[str], default:[]): Routes to exclude -config.py:85JWT_IMPLICIT_REFRESH_ROUTE_INCLUDE(list[str], default:[]): Routes to include -config.py:86JWT_IMPLICIT_REFRESH_METHOD_EXCLUDE(HTTPMethods, default:[]): Methods to exclude -config.py:87JWT_IMPLICIT_REFRESH_METHOD_INCLUDE(HTTPMethods, default:[]): Methods to include -config.py:88
Implicit refresh middleware automatically refreshes access tokens before they expire, creating a seamless user experience for cookie-based authentication.
Configuration helpers
TheAuthXConfig class provides helper methods and properties:
Check algorithm type
config.py:92 for is_algo_symmetric and config.py:97 for is_algo_asymmetric.
Check token location
config.py:118 for the has_location() method.
Get encryption keys
config.py:123 for private_key and config.py:128 for public_key.