uf.auth#
Authentication and authorization for uf.
Provides authentication backends, session management, and role-based access control for uf applications.
Functions
Get the global API key manager. |
|
Get the global authentication backend. |
|
Get the global session manager. |
|
|
Decorator to require authentication for a function. |
|
Set the global API key manager. |
|
Set the global authentication backend. |
|
Set the global session manager. |
Classes
|
API key for programmatic access. |
|
Manage API keys for programmatic access. |
Base authentication backend. |
|
Simple in-memory dictionary-based authentication. |
|
Password hashing utilities. |
|
|
Manage user sessions. |
|
User account information. |
- class uf.auth.ApiKey(key, name, permissions=None, expires_at=None)[source]#
Bases:
objectAPI key for programmatic access.
- key#
The API key string
- name#
Descriptive name
- permissions#
List of allowed permissions
- created_at#
When key was created
- expires_at#
Optional expiration
- is_active#
Whether key is active
- class uf.auth.ApiKeyManager(key_prefix='sk_')[source]#
Bases:
objectManage API keys for programmatic access.
Example
>>> api_keys = ApiKeyManager() >>> key = api_keys.create_key('mobile_app', permissions=['read']) >>> print(f"Your API key: {key.key}") >>> # Later, validate >>> if api_keys.validate_key(key.key, 'read'): ... # Allow access
- class uf.auth.AuthBackend[source]#
Bases:
objectBase authentication backend.
Subclass this to create custom authentication backends.
- create_user(username, password, roles=None, permissions=None, **metadata)[source]#
Create a new user.
- class uf.auth.DictAuthBackend[source]#
Bases:
AuthBackendSimple in-memory dictionary-based authentication.
Suitable for development and simple applications.
Example
>>> backend = DictAuthBackend() >>> backend.create_user('admin', 'secret', roles=['admin']) >>> user = backend.authenticate('admin', 'secret')
- create_user(username, password, roles=None, permissions=None, **metadata)[source]#
Create a new user.
- Return type:
- classmethod from_dict(users_data)[source]#
Create backend from dictionary.
- Parameters:
users_data (
dict) – Dictionary mapping usernames to user info- Return type:
Example
>>> backend = DictAuthBackend.from_dict({ ... 'admin': {'password': 'secret', 'roles': ['admin']}, ... 'user': {'password': 'pass', 'roles': ['user']}, ... })
- class uf.auth.PasswordHasher[source]#
Bases:
objectPassword hashing utilities.
Uses PBKDF2-HMAC-SHA256 for secure password hashing.
- class uf.auth.SessionManager(secret_key, session_timeout=3600)[source]#
Bases:
objectManage user sessions.
Example
>>> sessions = SessionManager(secret_key='my-secret') >>> session_id = sessions.create_session('admin') >>> user = sessions.get_session(session_id)
- cleanup_expired()[source]#
Remove expired sessions.
- Return type:
- Returns:
Number of sessions removed
- class uf.auth.User(username, password_hash, roles=<factory>, permissions=<factory>, metadata=<factory>, created_at=<factory>, is_active=True)[source]#
Bases:
objectUser account information.
- username#
Unique username
- password_hash#
Hashed password
- roles#
List of role names
- permissions#
List of permission strings
- metadata#
Additional user metadata
- created_at#
When user was created
- is_active#
Whether account is active
- uf.auth.require_auth(backend, roles=None, permissions=None)[source]#
Decorator to require authentication for a function.
- Parameters:
- Returns:
Decorator function
Example
>>> backend = DictAuthBackend.from_dict({ ... 'admin': {'password': 'secret', 'roles': ['admin']} ... }) >>> @require_auth(backend, roles=['admin']) ... def delete_all(): ... pass