Security & Authentication
This page describes how SPICE authenticates users and services, how access is authorised and scoped, and how data is protected. It is written for implementers and deployers. It describes mechanisms and guarantees, not configuration values. For the legal and privacy position, see Data Privacy & Security.
Overview
Centralised authentication. A dedicated authentication service is the only component that checks credentials and issues sessions. Every other backend service delegates the question "who is this caller?" to it, directly or through a signed assertion described below.
Default-deny, role-based authorisation. Each backend API is protected by a permission table that maps an HTTP method and path pattern to the roles allowed to call it. A request that matches no permission is rejected.
Scoped data access. Users see data for their organisation (tenant) and, for field and supervisory roles, for the part of the administrative hierarchy they are assigned to.
Protected data. Traffic is expected to be encrypted in transit, passwords are stored as salted one-way hashes when server-side password hashing is enabled, session tokens are encrypted, and the mobile app keeps its local database and credentials encrypted on the device.
Audited changes. Create, update and delete operations on core records are written to an audit trail.
Web app / Mobile app / Reporting web
| TLS
v
API gateway / reverse proxy ---- routes by path ----> backend services
(each runs the same
authentication filter)
|
validate session | verify signed
(cache lookup) v assertion locally
authentication serviceAuthentication
Signing in
Users sign in with a username (or another registered identifier such as a phone number or email address) and a password. The client sends the sign-in request to the authentication service over TLS, identifying which kind of client it is (web application, mobile application, or a service account used by background jobs).
The authentication service:
Looks up the account and rejects accounts that are blocked, deactivated or have no password set.
Verifies the password against the stored hash.
Confirms the user holds at least one role that is permitted to use the requesting client. A valid password alone is not enough: a role assigned only for the web application cannot be used to sign in on mobile, and vice versa.
Records the sign-in time, creates the session, and returns the user's profile, roles and organisation context to the client.
Passwords are never logged in clear text. Credentials are transformed by the client before they are sent. When server-side password hashing is enabled, which every production deployment should do (see the deployer checklist), the server stores only a salted, memory-hard one-way hash of the received value (Argon2id), and deployments upgrading from an earlier credential format can migrate in place.
Tokens and sessions
On successful sign-in the authentication service issues a session token. The token is an encrypted JSON Web Token (JWE): its contents (user identity, roles, organisations and client type) are encrypted with the authentication service's public key, so only the authentication service can read them. The token carries an expiry time, and the session is also tracked server-side in a shared cache, keyed by user and token.
Web. The token is delivered in a cookie that is
HttpOnlyandSecure, so page scripts cannot read it. Outside development, the cookie uses the strictest same-site setting. The browser application sends it automatically with every request.Mobile and other clients. The token is returned in a response header and sent back as a bearer credential on each request. The mobile app keeps it in encrypted on-device storage.
Expiry. A session has a configurable idle lifetime. Every successful validation extends it (a sliding window), and the web cookie is re-issued with the extended lifetime. When the lifetime lapses, or when the server-side session entry is removed, the token is rejected even if it has not reached its own expiry time.
Sign-out and revocation. Signing out removes the server-side session. The platform can revoke all of a user's sessions at once, and does so in administrative flows such as removing a user's access.
Session-expiry handling in clients. When any API call returns an authentication failure, the web application clears its in-memory state and returns the user to the sign-in screen; the mobile app broadcasts a session-expired event that routes the user back to sign-in.
There is no long-lived refresh token. Users re-authenticate when a session lapses.
Mobile offline sign-in
The mobile app is designed for places with poor connectivity. If the device has no network, a user who has previously signed in on that device can sign in against credentials cached in the app's encrypted storage and continue working with the encrypted local database. Server calls (including synchronisation) still require a valid server session; when connectivity returns and the server rejects a stale session, the user is asked to sign in again.
Service-to-service authentication
Backend services are protected by the same filter as user-facing calls, so an internal call is authenticated like any other request. Two mechanisms are used, and they are layered:
Forwarded user token. A service calling another service on behalf of a user forwards the user's session token. The receiving service asks the authentication service to validate it.
Signed assertion (fast path). To avoid asking the authentication service to validate the same session on every hop, the authentication service returns, alongside each validated session, a very short-lived, signed assertion of the already-validated identity. It is signed with an elliptic-curve key (ES256). Only the authentication service holds the signing key; all other services are configured with the verification (public) key only, so a compromised downstream service cannot mint identities. The assertion is forwarded on downstream calls and verified locally by the receiving service. It deliberately does not contain the live session token. If the assertion is missing, invalid or expired, the receiving service falls back to full token validation.
Even when a request is accepted through an assertion, the receiving service still applies its own role-to-API permission check. A trusted identity does not bypass authorisation.
Identity in asynchronous processing
Some work is not performed inside the user's request: mobile synchronisation, for example, places the user's payload on a message queue, and a consumer processes it later. The user's session cannot be forwarded across the queue, so SPICE uses a dedicated, least-privilege service account for this background work. The consumer signs in as that account, and attaches the identifier of the real user on whose behalf it is acting so that downstream services can apply that user's data scope (for example, the correct district) instead of the service account's own.
Inside a service, work that is fanned out onto parallel threads explicitly carries the caller's identity context (user, tenant and assertion) onto the worker threads, and clears it afterwards, so identity does not leak between concurrent requests.
Authorisation: roles, permissions and scope
Roles and API permissions
Authorisation is role-based:
Users are assigned one or more roles. Roles are data, not code, and the set of roles in a deployment is configurable.
A permission table records, per backend service, which roles may call which HTTP method and path pattern. Each service loads its permissions into a shared cache (refreshed periodically) and evaluates every authenticated request against them.
A request is allowed only if one of the caller's roles is listed for a matching API pattern. Otherwise it is rejected with an authentication/authorisation error.
A small number of well-defined cases bypass the table: platform super-administrators, the background-job service account, and the roles that the mobile application is allowed to use. Treat these as highly privileged and assign them sparingly.
A small, explicit list of endpoints (for example health checks and the sign-in and password-reset flows) is configured as open and does not require a session. Everything else requires authentication.
The permission table is configuration. Deployers who add custom roles or endpoints must add matching permission rows, and should review the open-endpoint list before go-live.
Scoping by organisation and administrative hierarchy
Authorisation answers "may this role call this API?". Scoping answers "which records may this user see or change?".
Organisation (tenant). Users belong to one or more organisations. Tenant-aware records are stamped with the tenant of the creating context. Write operations that name a tenant are validated against the tenants the user belongs to, and deleting a record that does not belong to the caller's selected tenant is refused. Privileged administrators and the mobile roles are exempt from the per-request tenant check, because their scope is defined by other assignments (below).
Administrative hierarchy and facility. Field and facility users are assigned to places in the hierarchy (for example village, health facility, district or region, depending on how the programme defines levels). Queries for households, members, follow-ups, referrals and reports are restricted to the places assigned to the user. Supervisory roles can be scoped to a set of areas, to a wider division, or to the whole programme; a supervisor with no assigned area sees nothing rather than everything.
Clinical data partitioning. Clinical records in the FHIR server can be partitioned by administrative area. The caller's district is carried in the identity context, so reads and writes issued on a user's behalf resolve to that user's partition. The deployer can enable or disable partitioning by configuration.
Reporting. The reporting web application authenticates with the same mechanism and applies role-based access to reports; analytics dashboards apply row-level restrictions per user group.
Passwords, lockout and reset
Account creation. An administrator creates the account. The new user receives a time-limited link by email or SMS to set their own password.
Lockout. After a configurable number of consecutive failed sign-ins (default five), the account is blocked and further attempts are refused for a configurable cooling-off period, even with the correct password. The counter resets on success. Administrators with the necessary permission can unlock an account manually.
Forgotten password. The user requests a reset. The system sends a signed link that expires after a configurable time and is stored only until used. The number of reset requests is rate-limited per user per time window. The web application shows a neutral confirmation message that does not state whether the account exists.
Setting a new password. The new password must differ from the current one. Completing a reset invalidates the link, clears the failed-attempt counters and unblocks the account.
Changing a password. A signed-in user must supply their current password.
Password strength. The server rejects empty or malformed credentials; strength rules are validated by the web client when the password is chosen.
Data protection
In transit
All client-to-server traffic is expected to use TLS, terminated at the gateway or load balancer. Session cookies are marked Secure. The deployer is responsible for certificates and for ensuring internal service ports are not exposed outside the private network.
On the device
The mobile application stores clinical and administrative data in a local database that is encrypted with SQLCipher (AES-256). Authentication tokens, cached sign-in details and other preferences are kept in encrypted preference storage backed by the platform's secure key store. The application opts out of platform cloud backup and device-to-device extraction of its data. Release builds are signed with the deployer's own signing key.
At rest on the server
With server-side password hashing enabled, passwords are stored as salted Argon2id hashes. Session tokens are encrypted. Database and storage encryption at rest is a deployment concern; deployers should enable volume or database encryption and encrypt backups.
Sensitive fields and logging
The audit trail never records credential or token fields (password, hashes, session tokens, reset tokens).
The sign-in request body should be excluded from gateway, proxy and monitoring logs.
Audit trail
Core records are audited at the persistence layer rather than in each API, so every service gets the same behaviour. For each create, update or delete on an audited record, one audit entry is written containing the entity, the action, a field-level before/after difference, the acting user, the tenant and a timestamp. Changes made in one transaction share a change-set identifier, so one logical action reads back as one event.
Audit entries are written after the business transaction commits, in their own transaction. An audit failure therefore cannot fail or roll back the user's save, and a change that rolls back leaves no audit entry. The trade-off is that if the process stops between commit and audit write, that change is not audited. High-churn bookkeeping columns and all credential and token fields are excluded from the difference, and individual entities can opt out of auditing where recording changes would add noise without value.
Sign-in time and token metadata (client type, application version) are recorded per session.
Consent and terms
User terms. A user must accept the application's terms and conditions on first use; acceptance is recorded against the user account and can be accepted once.
Beneficiary consent. Registration and screening workflows capture the consent of the household or person as part of the clinical record.
API gateway and reverse proxy expectations
SPICE assumes a single public entry point (an API gateway or reverse proxy) in front of the backend services. Deployers should configure it to:
terminate TLS and redirect plain HTTP, with modern protocol versions only;
route by service path prefix and expose only the APIs clients need, keeping the authentication service's sign-in and logout paths reachable but not its internal validation paths;
pass through the authentication cookie, bearer header and client-type header unchanged, and not log credentials or session tokens;
apply request size limits, timeouts and rate limiting, particularly on sign-in and password-reset endpoints, as an additional layer above the application's own lockout;
restrict the allowed browser origins (cross-origin resource sharing) to the application's real origins, since the services allow credentialed cross-origin calls;
keep management, metrics and API-documentation endpoints off the public route, or protect them;
be the only network path to the services: backend services, the cache, the message broker and databases should not be reachable from the internet.
Deployer security checklist
Related pages
Last updated