Skip to content

feat(auth2): Implement serverpod_auth_api_token module - #3618

Closed
tp wants to merge 2 commits into
serverpod:mainfrom
tp:feat/serverpod_auth_api_token
Closed

tp wants to merge 2 commits into
serverpod:mainfrom
tp:feat/serverpod_auth_api_token

Conversation

@tp

@tp tp commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

No description provided.

###
### This does not impact the behavior of the token, but can be useful for debugging.
### For example this could be used to differentiate between anonymous accounts, personal access tokens, or service accounts.
kind: String, default=""

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This might be useful here, to then also later check that tokens of a specific kind are e.g. updated to the latest desired behavior (max lifetime, automatic expiration etc.).

I suppose this could also be useful on the AuthUser itself, if we don't purely assume them being "humans".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this usually called label?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

label to me sounds more like description.

The usage I envision would be more like kind or type, meaning there should be a small, finite set of options (we probably would also need to index these, as they can be passed in the list query).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@SandPod In the auth_session we also store the method for now it was created:

CleanShot 2025-06-10 at 17 11 23@2x

Just FYI, I am also not entirely clear what would be best here.

class: ApiToken
serverOnly: true
table: serverpod_auth_session
fields:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe this also needs something like extraClaims (also exposed on the AuthenticationInfo), so one can easily add extra checks (e.g. what a personal access token should be able to do, if it's e.g. just for a specific resource, etc.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, should we in that case also add it to the db backed session token?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As mentioned earlier, I think it'd be better to use a separate table for any more advanced usage here, to get fully typed access.

"kind" just seems to common that we might want to provide it by default (some for the auth user), but after that more details should probably be looked up in a better interface.

(For the JWT tokens I also would hope that the primary consumer of that info are 3rd party systems extracting the Serverpod-generated token, and not the server itself, which probably has more up to date info about those claims – unless someone wants to really avoid DB lookups.)

@tp
tp force-pushed the feat/serverpod_auth_api_token branch from cd0daf8 to b8ca1c4 Compare June 6, 2025 08:18
@@ -0,0 +1,49 @@
class: ApiToken

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@SandPod As discussed last week, you might want to give the model spec a look here upfront, to make sure that we align on the breadth of this module.

I think the implementation follows from this one quite naturally.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the overall design looks good, I'm wondering if reate-limiting should be something to consider for an api token?

Maybe that should be built into the authenticationHandler directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds nice, I think we could optionally support this (if configured on the token).

Maybe we can even keep the storage requirements low, but using a sliding window (for the configured timeframe) and then just "upsert" a counter (so we always have indexed reads for checks and never COUNT queries). (Though still wondering whether we would store this on the API token row itself or externally (for better history).)

@tp
tp force-pushed the feat/serverpod_auth_api_token branch 4 times, most recently from 35058d6 to 76634d8 Compare June 10, 2025 12:58
@tp
tp force-pushed the feat/serverpod_auth_api_token branch from 76634d8 to 89593b7 Compare June 10, 2025 15:38
@tp
tp marked this pull request as ready for review June 10, 2025 15:46
@tp
tp requested a review from SandPod June 13, 2025 07:20
@tp

tp commented Jun 13, 2025

Copy link
Copy Markdown
Contributor Author

@SandPod I meant to remove you here, but the UI added you again.

As mentioned in #3578 (comment), this can now be closed as the entire functionality is available in the extended serverpod_auth_session module.

I'll do a quick PR to remove the "dummy" API token package shortly.

@tp tp closed this Jun 13, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants