Extending Providers with Mixins
Relevant source files
The following files were used as context for generating this wiki page:
Extending Providers with Mixins is a core architectural pattern in FLEXT Auth that enables the modular composition of authentication provider capabilities. Instead of monolithic inheritance, providers use specific mixins to acquire specialized behaviors such as RFC-standard compliance, token handling, data encoding/decoding, and input validation.
This approach follows the Single Responsibility Principle by isolating distinct provider concerns into reusable traits. For instance, an RFC-compliant provider like FlextAuthRfcProvider inherits from FlextAuthProviderMixin to integrate these shared functionalities into the standard provider interface.
Sources: src/flext_auth/providers/rfc.py:1-25, src/flext_auth/providers/mixin.py, src/flext_auth/providers/_mixins/init.py
Core Mixin Architecture
The architecture relies on a primary base mixin, FlextAuthProviderMixin, which aggregates specific sub-mixins. This structure allows any authentication provider to implement a broad interface while keeping the actual logic partitioned into specialized modules.
Hierarchy and Relationships
The following diagram illustrates the relationship between the base provider mixin and its specialized components:
The diagram shows how FlextAuthProviderMixin serves as the integration point for codec, token, and validation behaviors, which are then utilized by concrete implementations like FlextAuthRfcProvider.
Sources: src/flext_auth/providers/rfc.py:25-30, src/flext_auth/providers/_mixins/init.py:12-25, src/flext_auth/providers/init.py:12-30
Specialized Mixin Components
1. Codec Mixin (FlextAuthProviderCodecMixin)
The Codec mixin handles the transformation of data between raw transmission formats and internal Python structures. It ensures that providers can consistently process payloads regardless of the underlying protocol.
Sources: src/flext_auth/providers/_mixins/codec.py, src/flext_auth/providers/_mixins/init.py:12-14
2. Token Mixin (FlextAuthProviderTokenMixin)
This mixin provides standardized methods for token lifecycle management. It is utilized by providers that issue or validate session-based or stateless tokens (e.g., JWT, OAuth2).
Sources: src/flext_auth/providers/_mixins/tokens.py, src/flext_auth/providers/_mixins/init.py:15-17
3. Validation Mixin (FlextAuthProviderValidationMixin)
The Validation mixin enforces structural and security constraints on authentication requests. It provides the logic for checking credential formats and ensuring that incoming data meets the provider's specific schema requirements.
Sources: src/flext_auth/providers/_mixins/validation.py, src/flext_auth/providers/_mixins/init.py:18-20
RFC Provider Integration
The FlextAuthRfcProvider serves as the reference implementation for using mixins. It combines the FlextAuthProviderMixin with the FlextAuthBaseProvider protocol to create a foundation for standards such as RFC 7617 (Basic Auth) or RFC 7519 (JWT).
RFC Implementation Flow
This flow demonstrates how the RFC provider utilizes configuration projection and mixin-provided validation during the authentication lifecycle.
Sources: src/flext_auth/providers/rfc.py:35-85
Key RFC Provider Methods
| Method | Description | Source |
|---|---|---|
project_to_scalar_config | Filters non-primitive values (keeps only str, int, bool) from settings. | rfc.py:44-59 |
get_rfc_version | Returns the specific RFC string implemented by the subclass. | rfc.py:61-70 |
validate_rfc_compliance | Implements logic to ensure operations follow RFC standards. | rfc.py:86-101 |
authenticate | Abstract method for credential verification. | rfc.py:103-117 |
Implementation Patterns
When creating a new provider, developers should inherit from both the mixin and the base provider class to ensure full compatibility with the FLEXT Auth ecosystem.
from flext_auth.providers.mixin import FlextAuthProviderMixin
from flext_auth import p, r
class CustomProvider(FlextAuthProviderMixin, p.Auth.FlextAuthBaseProvider):
def authenticate(self, credentials):
# Implementation using mixin-provided validation
return r.ok(value="token")
Sources: src/flext_auth/providers/rfc.py:25-28, src/flext_auth/providers/mixin.py
Summary
Extending providers with mixins allows the FLEXT Auth library to support a diverse range of authentication protocols (LDAP, SAML, JWT, etc.) without duplicating core logic. By decoupling concerns like RFC compliance and payload encoding into specific mixins, the system remains maintainable and extensible, providing a consistent interface for the FlextAuth service to interact with various identity sources.
Sources: src/flext_auth/providers/init.py:35-65, src/flext_auth/providers/rfc.py:1-15