Skip to content

Comments

Refactor authentication document creation to use Pydantic models (PP-3675)#3066

Draft
jonathangreen wants to merge 5 commits intomainfrom
chore/authentication-document-refactor
Draft

Refactor authentication document creation to use Pydantic models (PP-3675)#3066
jonathangreen wants to merge 5 commits intomainfrom
chore/authentication-document-refactor

Conversation

@jonathangreen
Copy link
Member

Description

Refactor the authentication document creation pipeline to use structured Pydantic models (BaseOpdsModel subclasses) instead of building raw Python dicts. This brings the Authentication for OPDS document generation in line with the rest of the OPDS2 serialization in the codebase.

Key changes:

  • Expand standard OPDS Authentication 1.0 models in opds/authentication.py, making Authentication and AuthenticationDocument generic over their link and auth types using covariant TypeVars with PEP 696 defaults
  • Create opds/palace_authentication.py with Palace-specific extension models (PalaceAuthentication, PalaceAuthenticationDocument, PalaceAuthenticationLink, input descriptors, features, announcements, etc.)
  • Change OPDSAuthenticationFlow._authentication_flow_document() return type from dict[str, Any] to PalaceAuthentication
  • Update all authentication providers (basic, basic token, SAML, OIDC) to return Pydantic models
  • Refactor LibraryAuthenticator.create_authentication_document() to build a PalaceAuthenticationDocument model and serialize via model_dump()
  • Remove the legacy AuthenticationForOPDSDocument class

Motivation and Context

This lays groundwork for PP-3675. The previous dict-based approach lacked type safety, validation, and consistency with the rest of the OPDS2 feed serialization. By using Pydantic models, we get structured validation, consistent serialization via model_dump(), and better IDE/type-checker support.

Palace extensions are kept separate from pure OPDS spec models following the existing pattern (opds/palace.py for OPDS2 feeds).

How Has This Been Tested?

  • New unit tests for standard OPDS auth models (tests/manager/opds/test_authentication.py)
  • New unit tests for Palace auth extensions (tests/manager/opds/test_palace_authentication.py)
  • Updated existing tests for all affected providers and the authenticator
  • All tests pass, mypy passes, pre-commit passes

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

Replace raw dict-based authentication document construction with
structured Pydantic models (BaseOpdsModel subclasses), providing type
safety, validation, and consistent serialization.

- Add Palace-specific auth models in opds/palace_authentication.py
  (PalaceAuthentication, PalaceAuthenticationDocument, etc.)
- Move MEDIA_TYPE/LINK_RELATION constants to AuthenticationDocument
- Change OPDSAuthenticationFlow to return PalaceAuthentication models
- Update all providers (basic, basic_token, SAML, OIDC) accordingly
- Remove legacy AuthenticationForOPDSDocument class
- Serialize via model_dump() instead of manual dict construction
…ments

Make Authentication and AuthenticationDocument generic over their link
and authentication types using covariant TypeVars with PEP 696 defaults.
Since BaseOpdsModel is frozen (immutable), covariance is safe and allows
Palace subclasses to parametrize with concrete types instead of overriding
fields with incompatible types.
Add a type mismatch check in authentication_flow_document to ensure
implementations set the correct type, and remove duplicate MEDIA_TYPE
and LINK_RELATION from PalaceAuthenticationDocument since they are
inherited from AuthenticationDocument.
Add None check for library() return in BasicTokenAuthenticationProvider
and remove unused type: ignore[method-assign] comments in tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant