Authentication¶
AuthenCard
¶
Methods:
Name | Description |
---|---|
__init__ |
Initialize the Authen class with a secret key for JWT signing. |
from_config |
Initialize UserGenerator instance from a JSON configuration file. |
verify_access_token |
Verify a JWT access token by sending a request to the FastAPI verify-token endpoint. |
Attributes:
Name | Type | Description |
---|---|---|
token |
|
|
api_url |
|
__init__
¶
from_config
classmethod
¶
from_config(config_path: str = 'authen/secret.json')
Initialize UserGenerator instance from a JSON configuration file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path
|
str
|
Path to the JSON configuration file (default: authen/secret.json). |
'authen/secret.json'
|
Returns: An instance of UserGenerator with values loaded from the config file. Raises: FileNotFoundError: If the config file doesn't exist. KeyError: If required fields are missing in the config file. json.JSONDecodeError: If the config file is invalid JSON.
verify_access_token
¶
verify_access_token(token: str = None, api_url: str = 'http://localhost:8000/verify-token') -> Optional[Dict]
Verify a JWT access token by sending a request to the FastAPI verify-token endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The JWT token to verify |
None
|
api_url
|
str
|
The URL of the FastAPI verify-token endpoint (default: http://localhost:8000/verify-token) |
'http://localhost:8000/verify-token'
|
Returns:
Type | Description |
---|---|
Optional[Dict]
|
Optional[Dict]: Token payload with username, expires, and issued_at if valid, None if invalid |
Raises:
Type | Description |
---|---|
RequestException
|
If the API request fails |
server
¶
Classes:
Name | Description |
---|---|
Token |
|
TokenData |
|
User |
|
UserInDB |
|
UserCreate |
|
Functions:
Name | Description |
---|---|
verify_password |
Verify a plain password against a hashed password. |
get_password_hash |
Hash a password using bcrypt. |
get_user |
Retrieve user from database by username. |
authenticate_user |
Authenticate user by verifying username and password. |
create_access_token |
Create a JWT access token. |
get_current_user |
Get the current user from JWT token. |
get_current_active_user |
Ensure the current user is active. |
login_for_access_token |
Authenticate user and return access token. |
create_user |
Register a new user. |
read_users_me |
Get current user's information. |
protected_route |
Example of a protected endpoint. |
verify_token |
Verify JWT access token and return its payload. |
Attributes:
Name | Type | Description |
---|---|---|
secret |
|
|
SECRET_KEY |
|
|
ALGORITHM |
|
|
ACCESS_TOKEN_EXPIRE_MINUTES |
|
|
fake_users_db |
|
|
app |
|
|
pwd_context |
|
|
oauth2_scheme |
|
fake_users_db
module-attribute
¶
fake_users_db = {secret['username']: {'username': secret['username'], 'full_name': 'Your Full Name', 'email': 'your_email@example.com', 'hashed_password': secret['hashed_password'], 'disabled': False}}
Token
¶
TokenData
¶
User
¶
UserInDB
¶
UserCreate
¶
verify_password
¶
Verify a plain password against a hashed password.
authenticate_user
¶
Authenticate user by verifying username and password.
create_access_token
¶
Create a JWT access token.
get_current_user
async
¶
get_current_user(token: Annotated[str, Depends(oauth2_scheme)])
Get the current user from JWT token.
get_current_active_user
async
¶
get_current_active_user(current_user: Annotated[User, Depends(get_current_user)])
Ensure the current user is active.
login_for_access_token
async
¶
login_for_access_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()])
Authenticate user and return access token.
read_users_me
async
¶
read_users_me(current_user: Annotated[User, Depends(get_current_active_user)])
Get current user's information.
protected_route
async
¶
protected_route(current_user: Annotated[User, Depends(get_current_active_user)])
Example of a protected endpoint.