Skip to content

defaultAuthenticationHandler should use a JWT #1720

Closed

Description

@lukehutch

Is your feature request related to a problem? Please describe.

On await session.auth.authenticatedUserId, defaultAuthenticationHandler calls

    var authKey = await tempSession.dbNext.findById<AuthKey>(keyId);

That means that every HTTPS request hits the database once before the request can even be handled. This database roundtrip could dramatically decrease server performance (e.g. if all endpoints require the user to be logged in, and all HTTPS requests run one additional lightweight query, then verifying the user for every session would decrease the server performance and therefore the scalability by 50%).

Describe the solution you'd like

There needs to be a better AuthenticationHandler that does not hit the database.

The userId and the UserInfo object should be serialized and then encrypted with salt, along with a time to live, then base64-encoded to create a cookie that is shipped to the user. The user can send that cookie back to the server anytime, and the server can trust that cookie as long as the time to live has not been exceeded (because only the server is able to decrypt it).

This will prevent the need to hit the database at all, unless the client doesn't yet have this cookie (on first connect), or the time to live has been exceeded -- in which case, the database is queried, and a new cookie is generated.

Activity

  1. Isakdl commented on Jan 8, 2024

    @Isakdl
    Collaborator

    I think a simple JWT implementation would be sufficient for this (and a common web standard).

  2. lukehutch commented on Jan 8, 2024

    @lukehutch
    ContributorAuthor

    You're right, I forgot that there is a standard for this these days!

    Here's a library that implements this for Dart:

    https://pub.dev/packages/dart_jsonwebtoken

    I don't think it would be much work to utilize this in Serverpod.

  3. changed the title [-]Need cryptographic replacement for `defaultAuthenticationHandler` that does not hit the database[/-] [+]`defaultAuthenticationHandler` should use a JWT so that it does not hit the database[/+] on Jan 17, 2024
  4. changed the title [-]`defaultAuthenticationHandler` should use a JWT so that it does not hit the database[/-] [+]`defaultAuthenticationHandler` should use a JWT[/+] on Jan 17, 2024
  5. added
    enhancement
    area: authentication
    on Jul 8, 2024
  6. marcelomendoncasoares commented on Mar 26, 2026

    @marcelomendoncasoares
    Collaborator

    Closing since JWT is now the default on the templates since Serverpod 3.0 with the new auth.

  7. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: authenticationenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions