You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to facility external services accessing a Serverpod API with the new auth modules from #3126, we want to add the ability to add "service accounts".
Unlike the usual authentication providers (Email, Google, etc.) these would not refer to an AuthUser, but comprise a distinct entity.
The model might look like this:
ServiceAccount
id: Uuid
# Salted and peppered like we do for the email
# The actual secret is only returned once to the original creator,
# and can not be requested again.
secretHash: String
description: String
scopeNames: Set<String>
createdAt: DateTime, default=now
# Maybe defaults to 1 year in the examples
expires: DateTime?
# This will be set during the creation, but is optional such that the user can be deleted without loosing any service accounts it has created
# (This is of course problematic if they retain a copy of the secret, but then admins will just have to rotate them)
createdBy: AuthUser?, relation(optional)
The endpoint (abstract, so consumer can opt to modify it) might look like this:
@doNotGenerateclassServiceAccountEndpoint {
requiresLogin =>true;
requiredScopes => {admin}
/// Returns the service account key in the form to be used with the `authenticationHandler` /// Possibly like `base64('spsa:$id:$secret')`Future<String> createServiceAccount(Session, {Set<Scope>? scopes, String? description, DateTime? expiresAt,}) { … }
Future<List<ServiceAccountModel>> listServiceAccounts(…) { … }
Future<void> deleteServiceAccount(…) { … }
}
Additionally, we extract the business logic into a ServiceAccounts class.
And of course, the module will need to provide an authenticationHandler implementation.
This is now support by the more flexible serverpod_auth_session module in #3631.
Since we decided to not create a separate entity for "service account", but rather let them be AuthUsers (so permission management is simple throughout an app and does not have to check many different types or work across distinct database tables), a normal session like this can also be used for service accounts.
When creating a "service account" session, the following considerations are worth taking:
Should a fixed expiresAt be set, so that the session key ("API token") for the service account expires at the latest after for example 1 year?
This would also ensure a minimum baseline of API key-rotations in the system
Would an expireAfterUnusedFor of e.g. 2 weeks makes sense, to quickly shut down any service account access keys that are not in active use? This would make special sense if the intended use is a continuous or at least daily job.
The nice property of the session keys is that the client must not rotate anything, so they can be configured once and will work for the lifetime of the token.
So while nothing new needs to be implemented here, we should document how serverpod_auth_session can be used for both "normal" user sessions and for example service accounts with different configurations.
Activity
tp commentedon Jun 13, 2025
This is now support by the more flexible
serverpod_auth_sessionmodule in #3631.Since we decided to not create a separate entity for "service account", but rather let them be
AuthUsers (so permission management is simple throughout an app and does not have to check many different types or work across distinct database tables), a normal session like this can also be used for service accounts.When creating a "service account" session, the following considerations are worth taking:
expiresAtbe set, so that the session key ("API token") for the service account expires at the latest after for example 1 year?expireAfterUnusedForof e.g. 2 weeks makes sense, to quickly shut down any service account access keys that are not in active use? This would make special sense if the intended use is a continuous or at least daily job.The nice property of the session keys is that the client must not rotate anything, so they can be configured once and will work for the lifetime of the token.
So while nothing new needs to be implemented here, we should document how
serverpod_auth_sessioncan be used for both "normal" user sessions and for example service accounts with different configurations.[-]Add "service account" authentication[/-][+]docs(auth2): Showcase "service account" authentication[/+]