Skip to content

# Utils

## `` jwtlib.utils

### Summary

Auth Utilities: Token validation and user resolution.

This module provides high-level helpers for validating `JWT` payloads and resolving users, intended for use in dependency injection or middleware.

### Classes

### Functions

#### `` get_current_user `async`

    1

    get_current_user(token: str, repo: Optional[UserRepository] = None) -> PublicUser

Validate token and return authenticated public user.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`token` | `str` | The `JWT` string to validate. | required
`repo` | `UserRepository` | The user repository to use for resolution. | `None`

Returns:

Name | Type | Description
--- | --- | ---
`PublicUser` | `PublicUser` | The resolved and validated user object.

Raises:

Type | Description
--- | ---
`InvalidToken` | If the token is missing, malformed, or invalid.
`UserNotFound` | If the token is valid, but the user does not exist in the repository.

#### `` get_user_repository

    1

    get_user_repository() -> UserRepository

Return a singleton or new instance of the `UserRepository`.

Returns:

Name | Type | Description
--- | --- | ---
`UserRepository` | `UserRepository` | The user repository instance.

#### `` get_validated_token_payload

    1

    get_validated_token_payload(token: str) -> TokenPayload

Validate a `JWT` and return a typed payload.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`token` | `str` | The `JWT` string to validate. | required

Returns:

Name | Type | Description
--- | --- | ---
`TokenPayload` | `TokenPayload` | The validated and typed token payload.

Raises:

Type | Description
--- | ---
`JWTError` | If the token is invalid or malformed.