Local API authentication and authorization
The local API protects the embedded server with either HTTP Basic authentication or signed JWT bearer tokens. Basic credentials provide a direct administrative path, while JWT scopes let a client receive only the access it needs. This page explains the security boundary behind the local HTTP API; see security and encryption for the wider security model.
Authentication model
Endpoint protection
The embedded server defines two authentication methods: Basic authentication checks the configured username and password, while JWT authentication checks a signed token. The health endpoint is public. The remaining API routes use one of these methods before they run their route-level scope check. The root status endpoint accepts either method but does not ask for a product scope. References: app/…/WebService.kt• L77-109, app/…/WebService.kt• L139-175
Basic authentication is a high-trust path. A valid Basic principal passes every normal scope check, so it can reach all protected API features. The refresh endpoint is the exception because it requires a JWT whose only scope is tokens:refresh. This makes the local username and password equivalent to broad API access and requires the same care as any administrator credential. References: app/…/ScopeAuthorization.kt• L12-45, app/…/AuthRoutes.kt• L35-65
Credential persistence
The local server keeps its Basic credentials and JWT signing secret in the app's settings store. The default username is sms. The password is generated once when it is first needed, and the signing secret is also generated once. Both values then remain stable until an operator changes the password or regenerates the JWT secret. References: app/…/LocalServerSettings.kt• L18-39, app/…/PreferencesStorage.kt• L7-29
The default JWT access-token lifetime is one day. The setting accepts a positive value up to 365 days. Regenerating the secret invalidates tokens signed with the old secret because the server can no longer verify their signatures. The settings screen warns that server setting changes need an app restart before they apply to the running service. References: app/…/LocalServerSettings.kt• L41-63, app/…/LocalServerSettingsFragment.kt• L19-29, app/…/LocalServerSettingsFragment.kt• L67-76, app/…/local_server_preferences.xml• L12-39, app/…/local_server_preferences.xml• L50
Token lifecycle
Token issuance
Clients create a token pair with POST /auth/token. The request names one or more access scopes and can set an access-token lifetime. The endpoint requires Basic authentication or a JWT with tokens:manage; the response contains a Bearer access token, its access-token ID and expiry, and a refresh token. References: app/…/AuthRoutes.kt• L27-50, app/…/TokenRequest.kt• L5-10, app/…/TokenResponse.kt• L6-16
The service trims empty scope values and rejects an empty list or an unsupported scope. It signs both tokens with an HMAC key derived from the stored JWT secret. An access token carries the requested scopes and token_use=access. A refresh token carries only tokens:refresh for its own authorization plus the original access scopes for the next pair. References: app/…/JwtService.kt• L41-57, app/…/JwtService.kt• L161-175, app/…/JwtService.kt• L178-229
The access lifetime controls the requested access token. The refresh lifetime is longer for normal lifetimes and remains bounded by the same 365-day maximum. Clients should therefore use the access token for API work and keep the refresh token for obtaining its replacement pair. References: app/…/JwtService.kt• L178-182, app/…/JwtService.kt• L242-250
JWT validation
Clients send a JWT as a bearer credential in the Authorization header. The server verifies the HMAC signature and issuer, then checks the token ID against the revocation store before it creates an authenticated principal. Expired or otherwise invalid tokens do not reach route authorization. References: app/…/WebService.kt• L97-108, app/…/JwtService.kt• L145-155
The JWT carries the claims that connect cryptographic proof to API policy: issuer, token ID, issue time, expiry, token use and scopes. The token ID is also the key used to revoke the token later. The server does not need to store the JWT text to check revocation; it stores token metadata in the local database. References: app/…/JwtService.kt• L187-203, app/…/Token.kt• L8-24
Refresh and revocation
Clients call POST /auth/token/refresh with the refresh JWT as the bearer credential. The endpoint accepts only a JWT with token_use=refresh, a valid ID and exactly one scope: tokens:refresh. The service reads the original scopes from the token and returns a new access and refresh pair with those scopes. References: app/…/AuthRoutes.kt• L52-107, app/…/ScopeAuthorization.kt• L20-40
Refresh tokens rotate as a single database transaction. The current refresh token is revoked before the new pair is stored, so a second use is treated as replay and fails. DELETE /auth/token/{jti} revokes the named token and any token linked to it as a child. References: app/…/JwtService.kt• L59-143, app/…/TokensDao.kt• L34-59, app/…/TokensDao.kt• L19-32
Scope authorization
Scope grants
An API scope is a named permission for one class of action. The route asks for one scope after authentication. A JWT passes when its scope list contains that permission or the special all:any grant. A malformed scope claim becomes an empty list and cannot authorize a request. References: app/…/AuthScopes.kt• L3-34, app/…/ScopeAuthorization.kt• L20-45
The supported permissions are grouped by product area:
| Area | Scopes |
|---|---|
| Messages | messages:send, messages:read, messages:export, messages:cancel |
| Inbox | inbox:list, inbox:read, inbox:refresh |
| Device | devices:list |
| Webhooks | webhooks:list, webhooks:write, webhooks:delete |
| Settings and logs | settings:read, settings:write, logs:read |
| Token administration | tokens:manage, tokens:refresh |
tokens:refresh is system-issued for refresh tokens. Clients cannot request it as an access-token scope. This keeps a normal access token from being used as a refresh credential. References: app/…/AuthScopes.kt• L26-35
Route permissions
The route layer maps each protected operation to one scope. The same route registration can be mounted under more than one API path, but its permission remains the same.
| API operation | Required scope |
|---|---|
| Device details | devices:list |
| Read or send messages | messages:read, messages:send |
| Cancel or export messages | messages:cancel, messages:export |
| List inbox messages, read attachments or refresh the inbox | inbox:list, inbox:read, inbox:refresh |
| List, create or delete webhooks | webhooks:list, webhooks:write, webhooks:delete |
| Read or change settings | settings:read, settings:write |
| Read logs | logs:read |
| Create or revoke tokens | tokens:manage |
| Refresh a token pair | exact JWT-only tokens:refresh |
This mapping gives a token a narrow role without changing the business service behind a route. A client that needs both read and write behavior must request both permissions, unless it uses the broad Basic credential or all:any. References: app/…/WebService.kt• L151-210, app/…/MessagesRoutes.kt• L53-55, app/…/MessagesRoutes.kt• L137-139, app/…/MessagesRoutes.kt• L211-234, app/…/InboxRoutes.kt• L39-41, app/…/InboxRoutes.kt• L154-181, app/…/WebhooksRoutes.kt• L28-49, app/…/SettingsRoutes.kt• L23-39, app/…/LogsRoutes.kt• L24-27
Credential storage
Basic credential storage
The username, password, JWT secret and default JWT lifetime are local server settings. The settings layer persists them as app preference values, while the running web service reads them when it builds its authentication providers. Issued token metadata has a separate database store because it must support lookup, expiry cleanup and atomic rotation. References: app/…/LocalServerSettings.kt• L7-12, app/…/PreferencesStorage.kt• L14-28, app/…/AppDatabase.kt• L24-35
Token metadata storage
The token table stores each token ID, expiry time, revocation time, token use and parent relationship. It stores metadata rather than the bearer token text. The parent link connects a refresh token to the access token that created it, which lets access-token revocation also revoke its refresh child. Expired rows are removed during token generation or rotation. References: app/…/Token.kt• L8-24, app/…/TokensDao.kt• L16-32, app/…/AppDatabase.kt• L63-69
Authentication flow
The flow below shows how a client moves from stored credentials to a scoped request and how the server handles a refresh.
sequenceDiagram
participant Client
participant API as Embedded API
participant Store as Local stores
Client->>API: Basic credentials or bearer JWT
API->>Store: Check settings or JWT revocation state
Store-->>API: Authentication result
API->>API: Check token scope for route
API-->>Client: Protected response or 401/403
Client->>API: POST /auth/token/refresh with refresh JWT
API->>Store: Validate and rotate refresh token
Store-->>API: New pair or replay/invalid result
API-->>Client: New Bearer pair or 401
Authentication answers whether the caller has a valid credential. Authorization answers whether that credential has the permission for the selected route. Keeping these checks separate allows the same API to support broad local administration and least-privilege JWT clients. References: app/…/WebService.kt• L83-108, app/…/ScopeAuthorization.kt• L12-45, app/…/AuthRoutes.kt• L35-118
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the scope enum, token-scope validation and route checks aligned. A new route scope that is not accepted at token issuance cannot be granted to a client.
- Keep
token_use,scopesandoriginal_scopesconsistent between JWT claims and refresh handling. A mismatch can make valid pairs unusable or allow the wrong token type at the refresh route. - Keep JWT signing-secret changes coordinated with service startup behavior. Existing tokens become invalid after a secret change.
- Keep token parent links and the transaction in refresh rotation aligned. They define replay detection and the reach of revocation.
- Keep the OpenAPI auth descriptions and token response contract aligned with the runtime routes. Clients depend on the
Bearertype, token IDs and expiry fields.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing, expired, badly signed or revoked credential | The API returns 401 Unauthorized. | Send valid Basic credentials or a current bearer JWT. |
| Valid JWT without the route's scope | The API returns 403 Forbidden. | Issue a token with the needed scope or use all:any. |
| Basic credentials are exposed | The caller gains broad access to all normal protected routes. | Treat the stored password as an administrator secret and use narrow JWT scopes for integrations. |
| Access token is sent to the refresh endpoint | The API returns 401 Unauthorized. | Send the refresh token with token_use=refresh and the exact tokens:refresh scope. |
| A refresh token is used twice | The second use is rejected as replay. | Replace the stored pair after each successful refresh. |
| JWT secret is regenerated while clients hold old tokens | Old JWTs fail signature validation. | Issue new token pairs after the secret change. |
Key files
| File | Why to read it |
|---|---|
| app/…/WebService.kt | Shows the authentication providers and the protected route boundary. |
| app/…/AuthRoutes.kt | Defines token creation, refresh and revocation as API operations. |
| app/…/JwtService.kt | Explains JWT claims, token lifetimes, signing and refresh rotation. |
| app/…/ScopeAuthorization.kt | Shows how authenticated principals become route permissions. |
| app/…/AuthScopes.kt | Defines the permission vocabulary accepted by the local API. |
| app/…/LocalServerSettings.kt | Defines persistent Basic credentials, JWT settings and secret regeneration. |
| app/…/Token.kt | Defines the persistent token metadata and access-versus-refresh distinction. |
| app/…/TokensDao.kt | Shows revocation, expiry cleanup and atomic refresh rotation. |
| app/…/MessagesRoutes.kt | Illustrates scope checks on read, write, cancel and export actions. |
| app/…/InboxRoutes.kt | Illustrates separate scopes for inbox listing, attachments and refresh. |