认证模块¶
toolregistry_server.auth.BearerTokenAuth ¶
Bearer token authentication handler.
This class provides Bearer token authentication for FastAPI endpoints. It supports multiple valid tokens and can be used as a FastAPI dependency.
Attributes:
| Name | Type | Description |
|---|---|---|
tokens |
set[str]
|
Set of valid tokens. |
Example
Initialize the authentication handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
Sequence[str] | None
|
List of valid tokens. If None or empty, authentication is disabled (all requests pass). |
None
|
Source code in src/toolregistry_server/auth/__init__.py
enabled
property
¶
Check if authentication is enabled.
Returns:
| Type | Description |
|---|---|
bool
|
True if at least one token is configured. |
tokens
property
¶
Get the set of valid tokens.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of valid tokens. |
add_token ¶
Add a valid token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The token to add. |
required |
remove_token ¶
Remove a token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The token to remove. |
required |
verify ¶
Verify a token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The token to verify. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the token is valid or auth is disabled. |
Source code in src/toolregistry_server/auth/__init__.py
toolregistry_server.auth.create_bearer_dependency ¶
Create a FastAPI dependency for Bearer token authentication.
This function creates a dependency that can be used with FastAPI's dependency injection system to protect endpoints with Bearer token authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth
|
BearerTokenAuth
|
The BearerTokenAuth instance to use for verification. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A FastAPI dependency function. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If FastAPI is not installed. |
Example
Source code in src/toolregistry_server/auth/__init__.py
toolregistry_server.auth.verify_token ¶
Verify a token against a list of valid tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The token to verify. |
required |
valid_tokens
|
Sequence[str]
|
List of valid tokens. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the token is in the valid tokens list. |