Skip to content

add auth verification to fastapi#11704

Merged
cdrini merged 22 commits intomasterfrom
fastapi-auth
Jan 23, 2026
Merged

add auth verification to fastapi#11704
cdrini merged 22 commits intomasterfrom
fastapi-auth

Conversation

@RayBB
Copy link
Copy Markdown
Collaborator

@RayBB RayBB commented Jan 18, 2026

This pull request adds FastAPI-based authentication endpoints to Open Library, enabling login, logout, and authentication checks using the same session cookie format as the legacy system. It introduces new authentication dependencies, refactors cookie generation logic for reuse, and includes a test script for local validation. These changes lay the groundwork for a modern API-based authentication flow while maintaining compatibility with existing web.py logic.

FastAPI Authentication Endpoints:

  • Added openlibrary/fastapi/account.py with endpoints for login, logout, and authentication status checks, reusing existing account auditing and session cookie logic for compatibility with the legacy system.
  • Integrated the new account router into the FastAPI application in openlibrary/asgi_app.py, making the endpoints available.

Authentication Logic and Utilities:

  • Introduced openlibrary/fastapi/auth.py, providing dependencies for extracting and verifying authenticated users from session cookies, including models and helper functions for authentication in FastAPI routes.
  • Refactored session cookie generation logic into a reusable function generate_login_code_for_user in openlibrary/accounts/model.py and updated the Account.generate_login_code method to use it, ensuring consistent cookie format between legacy and new endpoints. [1] [2]

Testing and Validation:

  • Added test_fastapi_auth.sh, a shell script for local testing of FastAPI authentication endpoints, covering login, logout, session cookie handling, and compatibility checks.

Related to #11133

@RayBB RayBB marked this pull request as ready for review January 20, 2026 18:41
Copy link
Copy Markdown
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm! We tested all the helper endpoints and they did the correct thing (logout excepting). Will bring up with @mekarpeles to do a quick pass of the cookie handling stuff since he's more familiar with authentication.



@router.post("/account/logout")
async def logout(request: Request) -> Response:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint wasn't working correctly ; we think it might be something related to webpy / fastapi storing the session cookie slightly differently?

Here's the set-cookie header from the two systems:

fastapi:
set-cookie
	session=""; expires=Wed, 21 Jan 2026 18:38:46 GMT; Max-Age=0; Path=/; SameSite=lax
set-cookie
	pd=""; expires=Wed, 21 Jan 2026 18:38:46 GMT; Max-Age=0; Path=/; SameSite=lax
set-cookie
	sfw=""; expires=Wed, 21 Jan 2026 18:38:46 GMT; Max-Age=0; Path=/; SameSite=lax


webpy:
set-cookie
	pd=; expires=Sun, 15 May 1994 16:50:45 GMT; Path=/
set-cookie
	sfw=; expires=Sun, 15 May 1994 16:50:45 GMT; Path=/
set-cookie
	session=; expires=Sun, 15 May 1994 16:50:45 GMT; Path=/

Copy link
Copy Markdown
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mekarpeles and I reviewed and this looks good! Marking as need changes to delete those files we talked about.

@RayBB RayBB requested a review from cdrini January 22, 2026 20:53
@RayBB
Copy link
Copy Markdown
Collaborator Author

RayBB commented Jan 22, 2026

@cdrini all changes are addressed

Copy link
Copy Markdown
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@cdrini cdrini merged commit 12ad252 into master Jan 23, 2026
8 checks passed
@cdrini cdrini deleted the fastapi-auth branch January 23, 2026 19:59
lokesh pushed a commit to lokesh/openlibrary that referenced this pull request Feb 4, 2026
* add auth verification to fastapi

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix linter bugs

* more specific errors handling

* better naming for types

* add auth endpoints

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove extra secret key function def

* remove extra comments

* better name

* add notes of things to delete

* add notes of things to delete

* cleaner generate_login_code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add missing import

* remove extra verify_hash def

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove auth middleware that's not needed at this moment

* better comments

* delete ai generated docs

* move auth tests to python

* tests don't run in Ci now

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

3 participants