Jwtlib
jwtlib
jwtlib: A framework-agnostic JWT authentication library.
Summary
jwtlib provides a set of pure logic components for handling JWT discovery,
user registration, login, and token introspection. It is designed to be
decoupled from any specific web framework (like FastAPI or Flask).
Installation
Install using pip:
1 | |
Quick Start
1 2 3 4 5 6 7 | |
Public API
This package re-exports the core authentication primitives. Consumers are encouraged to import from this namespace for high-level operations.
Authentication: - register_user - login_user - logout_user - get_logged_in_user
Introspection: - introspect_token - authenticate_request (logic-only)
Models: - RegisterRequest / LoginRequest / LoginResponse / LogoutResponse - PublicUser - IntrospectRequest / IntrospectResponse - TokenPayload
Exceptions: - AuthError - InvalidToken - UserNotFound
Classes
AuthError
Bases: Exception
Base authentication and authorization error.
Notes
Guarantees:
1 2 | |
IntrospectRequest
Bases: BaseModel
Payload for requesting token introspection.
Attributes:
| Name | Type | Description |
|---|---|---|
token |
str
|
JWT access token to introspect. |
IntrospectResponse
Bases: BaseModel
Result of a token introspection operation.
Attributes:
| Name | Type | Description |
|---|---|---|
active |
bool
|
Indicates whether the token is valid and active. |
user |
Optional[PublicUser]
|
Public user details if the token is valid; otherwise null. |
InvalidToken
Bases: AuthError
Raised when a JWT is missing, malformed, expired, or invalid.
Notes
Guarantees:
1 | |
LoginRequest
Bases: IdentityMixin, PasswordMixin
Payload for authenticating a user and issuing a JWT.
Attributes:
| Name | Type | Description |
|---|---|---|
username |
str
|
Username identifier. |
password |
str
|
Plain-text password to be verified. |
LoginResponse
Bases: BaseModel
Response returned after successful authentication.
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str
|
JWT access token for authenticated requests. |
user |
PublicUser
|
Public profile of the authenticated user. |
LogoutResponse
Bases: BaseModel
Response returned after a logout operation.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
str
|
Human-readable logout confirmation. |
PublicUser
Bases: IdentityMixin, ActiveStateMixin
Public-facing user representation returned by authentication APIs.
Attributes:
| Name | Type | Description |
|---|---|---|
username |
str
|
Unique username identifier. |
email |
EmailStr
|
User's email address. |
is_active |
bool
|
Whether the user account is active. |
RegisterRequest
Bases: IdentityMixin, PasswordMixin
Payload for registering a new user account.
Attributes:
| Name | Type | Description |
|---|---|---|
username |
str
|
Unique username identifier. |
email |
EmailStr
|
User's email address. |
password |
str
|
Plain-text password (to be hashed by the repository layer). |
TokenPayload
Bases: BaseModel
Decoded JWT payload.
Attributes:
| Name | Type | Description |
|---|---|---|
sub |
str
|
Subject claim identifying the user (typically a username or user ID). |
exp |
int
|
Expiration time as a Unix timestamp (seconds since epoch). |
Notes
Responsibilities:
1 | |
Guarantees:
1 | |
User
Bases: BaseDocument, IdentityMixin, ActiveStateMixin
Internal user persistence model.
Attributes:
| Name | Type | Description |
|---|---|---|
hashed_password |
str
|
Secure hash of the user's password. |
Notes
Responsibilities:
1 | |
Guarantees:
1 | |
UserNotFound
Bases: AuthError
Raised when a valid token does not map to an existing user.
Notes
Guarantees:
1 | |
Functions
authenticate_request
async
Authenticate an incoming request using token introspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
should_skip_authentication |
Callable[[str, str], bool]
|
Callable that decides whether authentication is required for a given HTTP method and path. |
required |
method |
str
|
HTTP method of the incoming request. |
required |
path |
str
|
Request path. |
required |
authorization_token |
str | None
|
JWT access token provided by the caller. |
required |
Returns:
| Type | Description |
|---|---|
Optional[PublicUser]
|
PublicUser | None: PublicUser if authentication succeeds; None if authentication is skipped. |
Raises:
| Type | Description |
|---|---|
InvalidToken
|
If authentication is required but the token is missing, invalid, or revoked. |
AuthServiceUnavailable
|
If the auth service cannot be reached. |
Notes
Responsibilities:
1 | |
Guarantees:
1 | |
get_logged_in_user
async
Resolve the currently authenticated user from a JWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token |
str
|
JWT access token. |
required |
repo |
UserRepository
|
Optional user repository instance. If not provided, a default repository is obtained via dependency utilities. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
PublicUser |
PublicUser
|
The authenticated user as a PublicUser. |
Raises:
| Type | Description |
|---|---|
InvalidToken
|
If the token is missing, malformed, or invalid. |
AuthError
|
If the token is valid, but the user cannot be resolved. |
introspect_token
async
Introspect a JWT for service-to-service authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token |
str
|
JWT access token to introspect. |
required |
repo |
UserRepository
|
Optional user repository instance. If not provided, a default repository is obtained via dependency utilities. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
IntrospectResponse |
IntrospectResponse
|
IntrospectResponse indicating valid token with user, invalid token, or valid token with no user. |
Notes
Responsibilities:
1 | |
Guarantees:
1 | |
login_user
async
Authenticate a user and issue an access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user |
LoginRequest
|
Login payload containing username and password. |
required |
repo |
UserRepository
|
Optional user repository instance. If not provided, a default repository is obtained via dependency utilities. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
LoginResponse |
LoginResponse
|
LoginResponse containing the issued access token and related metadata. |
Raises:
| Type | Description |
|---|---|
AuthError
|
If the credentials are invalid. |
logout_user
async
Perform a stateless logout.
Returns:
| Name | Type | Description |
|---|---|---|
LogoutResponse |
LogoutResponse
|
LogoutResponse containing a logout confirmation message. |
Notes
Guarantees:
1 | |
register_user
async
Register a new user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user |
RegisterRequest
|
Registration payload containing username, email, and password. |
required |
repo |
UserRepository
|
Optional user repository instance. If not provided, a default repository is obtained via dependency utilities. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
PublicUser |
PublicUser
|
The newly created user as a public user representation. |