Skip to content

docs(auth2): Showcase "service account" authentication #3578

Open

Description

@tp

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:

@doNotGenerate
class ServiceAccountEndpoint {
   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.

Activity

  1. self-assigned this
    on May 23, 2025
  2. added
    area: authentication
    on May 23, 2025
  3. tp commented on Jun 13, 2025

    @tp
    ContributorAuthor

    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.

  4. changed the title [-]Add "service account" authentication[/-] [+]docs(auth2): Showcase "service account" authentication[/+] on Jun 13, 2025
  5. moved this to Later 🥳 in Serverpod Frameworkson Jun 16, 2025
  6. moved this from Planned 🔭 to Backlog 🗂️ in Serverpod Frameworkson Aug 12, 2025
  7. added
    documentation
    on Oct 8, 2025
  8. added this to the 3.0 Release milestone on Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: authenticationdocumentation

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions