Source code for cadrumo.application.auth.providers
"""Application-owned authentication provider contract and selection service."""from__future__importannotationsfromcollections.abcimportGeneratorfromcontextlibimportcontextmanagerfromcontextvarsimportContextVarfromtypingimportTYPE_CHECKING,Protocol,runtime_checkablefrom...core.auth_providerimportAuthProviderDescription,AuthProviderKindfrom...core.errors.hierarchyimportInternalInvariantErrorfrom..auth_credentialsimportActiveCertificateCredentialsfrom.certificate_secret_backendimportCertificateSecretBackendFactoryfrom.credentialsimportresolve_active_certificate_credentialsfrom.operator_scope_portsimportOperatorScopePortsfrom.protocolsimportBrowserSessionFactoryPortfrom.session_typesimportAeatLoginAssertion,AeatSessionifTYPE_CHECKING:from...core.configimportSettings
[docs]@runtime_checkableclassAuthProvider(Protocol):"""Contract implemented by the concrete outbound AEAT providers."""kind:AuthProviderKind
[docs]asyncdefauthenticate(self)->AeatSession:"""Establish an authenticated AEAT session."""...
[docs]asyncdefverify(self,session:AeatSession)->AeatLoginAssertion:"""Verify an acquired AEAT session."""...
[docs]defdescribe(self)->AuthProviderDescription:"""Describe the provider without exposing secret material."""...
[docs]asyncdefclose(self)->None:"""Release every provider-owned resource."""...
[docs]classAuthProviderSelector(Protocol):"""Concrete-provider constructor supplied by outward composition."""def__call__(self,kind:AuthProviderKind,*,settings:Settings,browser_session_factory:BrowserSessionFactoryPort|None=None,certificate_credentials:ActiveCertificateCredentials|None=None,)->AuthProvider:"""Construct the provider selected by ``kind``."""...
[docs]@contextmanagerdefbind_auth_provider_selector(selector:AuthProviderSelector)->Generator[AuthProviderSelector]:"""Bind the outward concrete-provider composition for one runtime scope."""token=_BOUND_AUTH_PROVIDER_SELECTOR.set(selector)try:yieldselectorfinally:_BOUND_AUTH_PROVIDER_SELECTOR.reset(token)
def_auth_provider_selector()->AuthProviderSelector:try:return_BOUND_AUTH_PROVIDER_SELECTOR.get()exceptLookupErrorasexc:raiseInternalInvariantError("AEAT authentication provider composition is not bound")fromexc
[docs]defselect_provider(kind:AuthProviderKind,*,settings:Settings,certificate_secret_backend_factory:CertificateSecretBackendFactory,browser_session_factory:BrowserSessionFactoryPort|None=None,certificate_credentials:ActiveCertificateCredentials|None=None,operator_scope_ports:OperatorScopePorts,)->AuthProvider:"""Construct the concrete outbound provider for ``kind``. Certificate credentials are resolved once at the application boundary before construction so a missing named-source secret cannot fall through to an unrelated global password. """credentials=certificate_credentialsifkindisAuthProviderKind.CERTIFICATEandcredentialsisNone:credentials=resolve_active_certificate_credentials(certificate_secret_backend_factory=certificate_secret_backend_factory,settings=settings,operator_scope_ports=operator_scope_ports,)return_auth_provider_selector()(kind,settings=settings,browser_session_factory=browser_session_factory,certificate_credentials=credentials,)