Skip to content

Custom session data #3121

Closed
Closed

Description

@vanlooverenkoen

Problem to Solve

We are integrating with a 3rd party custom auth server that uses JWT tokens to validate the session.

We would like to have a way to pass the 3rd party JWT token to the session so it can be used in serverpod to do api call to the 3rd party on behalve of the user that is logged in.

  • login
  • register
  • profile
  • change password
  • delete account

are all handled by the 3rd party.

Right now it is only possible to save the token during login in the database and retreive it when doing one of these calls. I'm not really a fan because this puts the responsability on the server instead of token storage on the mobile device itself.

Proposal

It would be cool to have a field called extraData on Session so we can save all kind of data in there.

Use Case

The extra data could contain the following

  • a 3rd party auth token
  • a custom user role
  • user data we want to have available at any time

Alternatives

We could adapt all our request to pass the token which will make the api a lot harder to use.

Additional context

No response

How experienced are you with this library?

Intermediate - Familiar with the basics or have used it in a few projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    enhancement
    on Jan 14, 2025
  2. SandPod commented on Jan 14, 2025

    @SandPod
    Contributor

    We have discussed adding an additional Map to the AuthenticationInfo object that is returned as a result from a successfull authentication, defined here:

    It is available on Session under the authenticated property.

    If I understand you correctly, you have implemented your own authenticationHandler and having the possibility to populate the AuthenticationInfo object with additional information satisfy your usecase?

  3. added
    area: authentication
    on Jan 14, 2025
  4. vanlooverenkoen commented on Jan 14, 2025

    @vanlooverenkoen
    ContributorAuthor

    Ah yes indeed this is probably what I need! I will do the implementation tomorrow and let you know if it fully fit's all usecases I described.

  5. vanlooverenkoen commented on Jan 14, 2025

    @vanlooverenkoen
    ContributorAuthor

    Ah sorry I thought it was already implemented. But yes what you proposed is correct!

    I have my own authenticationHandler indeed.

  6. SandPod commented on Jan 15, 2025

    @SandPod
    Contributor

    As a temporary workaround, you could populate the dynamic userObject field on Session to store any information that should be associated with the authenticated user.

  7. vanlooverenkoen commented on Jan 15, 2025

    @vanlooverenkoen
    ContributorAuthor

    When should that happen? When the "3rd party token" is created during login? Or at every api call insert the "3rd party token"?

    Because I don't want to store the "3rd party token" in the serverpod database. I want to have that on the users phone/device. Is that possible?

  8. SandPod commented on Jan 16, 2025

    @SandPod
    Contributor

    With that clarification, I think I might have misunderstood your request.

    You are saying that you have additional information available on the client, which is not the auth token used to authenticate towards server, that should be included with every request.

    Is that a more accurate description of the feature you are requesting?

    I assumed that the information was already available in the token used to authenticate with to the server, if so, the information could simply have been taken out during the normal authentication flow for the token.

  9. vanlooverenkoen commented on Jan 16, 2025

    @vanlooverenkoen
    ContributorAuthor

    So basicly this is what I need

    Login:

    • MOBILE: User logs in
    • SERVERPOD: Login request is called
    • 3RD PARTY AUTH SERVICE: Login in validated on 3rd party auth service
    • 3RD PARTY AUTH SERVICE: Returns 3rd party token & 3rd party refresh token
    • SERVERPOD: Creates a "serverpod auth key" and returns a response like this that also includes the 3rd party token & 3rd party refresh token
    • MOBILE: Stores the serverpod auth key & 3rd party token & 3rd party refresh token

    Any other API call

    • MOBILE: Performs a request
    • MOBILE: Adds the serverpod auth key & 3rd party token & 3rd party refresh token to that request
    • SERVERPOD: Validates the session at the 3rd party service
    • 3RD PARTY AUTH SERVICE: Validates if the 3rd party token is still valid. If not a 401 is returned.
    • SERVERPOD: returns the correct data is the token was valid, if the token was invalid a 401 was returned
    • MOBILE: If the correct data was returned & a valid token was there, show the data in the app. If a 401 was returned try to reresh

    Refresh:

    • MOBILE: Performs refresh api call (with refresh token)
    • SERVERPOD: calls the 3RD PARTY AUTH SERVICE to refresh the token with the token provided by mobile.
    • 3RD PARTY AUTH SERVICE: Validates the token. & returns new token & refresh token if valid. otherwise return 401
    • SERVERPOD: Creates a "serverpod auth key" and returns a response like this that also includes the 3rd party token & 3rd party refresh token

    All of this should happen automatically by serverpod from the moment I returned the "auth key + extra data"

    Other concepts like JWT:
    A similar concept i there with jwt tokens. you can add data to a jwt token on serverside that is always in there, from the moment the data is removed, or someone tried to temper with the jwt tokens are invalid

    In the end it should be possible to add extra data to a token when loggin in that is passed to every request. In my case it is the 3rd party token

  10. SandPod commented on Jan 16, 2025

    @SandPod
    Contributor

    Thank you for the clarification!

    Can I conclude that this information should then be integrated with the token issued by Serverpod?

    In short, you would like to have the possibility to add optional additional information to any auth key created by the Serverpod authentication module before it is sent and saved in the client. That information should then be available during authenticating requests to the server?

  11. vanlooverenkoen commented on Jan 16, 2025

    @vanlooverenkoen
    ContributorAuthor

    Exactly!!

  12. wstrange commented on Jan 23, 2025

    @wstrange
    Contributor

    I have a related problem (I think) where this could come in to play.

    Consider the case where the same authenticated user ([email protected] using gmail auth), can log on to two or more different organizations. In one org (say Acme) Bob is an admin. In another org (Coyote), Bob is a regular user.

    The session object should reflect the org that bob is trying to log in to. The client must present the choice at login (Acme or Coyote?), but once Bob has logged in, the session should reflect the correct context. So the endpoint logic should be able to query the session for this info to make the correct authZ decisions.

    Hope that makes sense...

  13. marcelomendoncasoares commented on Mar 27, 2026

    @marcelomendoncasoares
    Collaborator

    Can I conclude that this information should then be integrated with the token issued by Serverpod?

    In short, you would like to have the possibility to add optional additional information to any auth key created by the Serverpod authentication module before it is sent and saved in the client. That information should then be available during authenticating requests to the server?

    Closing as completed since this is possible using the callbacks to attach metadata to tokens on the new auth.

  14. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Mar 27, 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