pyconfigs/user.py file allows you to define custom user attributes and configure OAuth authentication providers for your Squirrels project. This enables user-specific data filtering, access control, and integration with external authentication systems.
The
user.py file is optional. If it doesn’t exist, then no custom user fields are defined and no authentication providers are configured.File structure
Theuser.py file typically contains:
- A
CustomUserFieldsclass that extendsauth.CustomUserFieldsto define custom user attributes - Optional authentication provider functions decorated with
@auth.provider
pyconfigs/user.py
CustomUserFields class
TheCustomUserFields class allows you to define custom attributes that are associated with each authenticated user. These fields can be used for:
- Filtering parameter options based on user attributes
- Access control in data models
- User-specific context variables
- Customizing dataset results based on user permissions
Class definition
YourCustomUserFields class must:
- Extend
auth.CustomUserFields(imported fromsquirrels.authorsquirrels) - Be named exactly
CustomUserFields - Have a default value for all fields
pyconfigs/user.py
Supported field types
Each custom field must be one of the following types:str- String valuesint- Integer valuesfloat- Float valuesbool- Boolean valuestyping.Literal- Literal types (e.g.,Literal["option1", "option2"])
| None after the type to make a field nullable.
Authentication providers
Authentication providers enable OAuth-based login for your Squirrels project. Users can authenticate using external providers like Google, Microsoft, GitHub, etc.The AuthProviderArgs object
Provider functions receive asqrl argument of type AuthProviderArgs that provides:
| Property | Type | Description |
|---|---|---|
project_path | str | Absolute path to the Squirrels project directory |
proj_vars | dict[str, Any] | Project variables from squirrels.yml |
env_vars | dict[str, str] | Environment variables from .env files and system |
Registering a provider
Use the@auth.provider decorator to register an authentication provider:
- Accept an
sqrlargument of type AuthProviderArgs - Return a ProviderConfigs object
ProviderConfigs
The ProviderConfigs class requires:client_id: OAuth client ID (typically from environment variables)client_secret: OAuth client secret (typically from environment variables)server_url: URL of the OAuth serverget_user: Function that converts OAuth claims to a RegisteredUser objectserver_metadata_path: Optional path to OAuth server metadata (defaults to/.well-known/openid-configuration)client_kwargs: Optional dictionary of additional OAuth client arguments
Using custom user fields examples
In context.py
Access custom user fields to create user-specific context variables:pyconfigs/context.py
In parameter options
Filter parameter options based on user attributes:pyconfigs/parameters.py
In Python data models
Use custom user fields for access control and filtering:models/federates/fed_sales.py
Best practices
- Store secrets in environment variables: Never hardcode OAuth client IDs and secrets. Use environment variables instead.
-
Type casting: When accessing custom fields in
context.pyor data models, usecast()to get proper type hints and IDE autocomplete.
Related pages
- CustomUserFields - Full reference for the base class
- provider - Decorator function for registering authentication providers
- ProviderConfigs - Configuration class for OAuth providers
- RegisteredUser - Class representing authenticated users
- AuthProviderArgs - Properties available in the
sqrlobject argument - context.py - How to use user fields in context variables
- parameters.py - How to filter parameter options by user attributes