<aside> 🔁 Synced from GitHub #64db432c8e69. Edit in the paidhr-system-docs repository, not here: changes made in Notion are overwritten.
</aside>
Every request to PaidHR answers three questions before any business logic runs: who is this, which customer's data may they touch, and is this endpoint allowed for them? core-backend answers all three. The other services ask core-backend.
All sign-ins go to core-backend's IdentityController / AccountController and end in IdentityService.Authenticate. The same method handles several kinds of sign-in, chosen by which fields the caller sends:
| Sign-in | Used by | How |
|---|---|---|
| Email + password | Web and mobile users | ASP.NET Identity password check, then organisation status check |
OAuth / SSO (OAuth/Authenticate, Sso/Authenticate) |
Google and Microsoft sign-in | Provider auth code exchanged for a user |
Control (Control/Authenticate) |
PaidHR staff, control panel | Separate session type with its own Control/CheckSession |
| Secret key / config value | Background jobs, integrations | A job signs in as an organisation to run work in its tenant |
Bot (Bot/Authenticate, Bot/Code/*, Bot/TwoFactor) |
USSD/WhatsApp bot | Short-lived bot sessions (UserBotSession) |
Channel OTP (Channel/Otp/Request, /Verify) |
Bot channels | One-time code to a known phone or email |
A sign-in returns a status and a context. The context is written into the token and limits what the token can do:
| Context | Meaning |
|---|---|
Owned |
A normal, complete session for the user's own organisation |
Acquired |
A session obtained on someone's behalf: a job or integration acting as an organisation, or delegated access that signs in as the organisation's primary admin (display name "Delegated Access"). Audit-logged |
TwoFactor |
Password accepted; a 2FA code is still needed. Short expiry |
TwoFactorSetup / TwoFactorSetupRequired |
The user must set up 2FA before continuing |
PasswordResetRequired |
The user must change their password first |
ReadOnly |
A restricted session |
Sign-in is refused when the organisation is not Active or Trial (for example suspended).
A successful sign-in returns an access token (JWT) and a refresh token. The refresh token is saved server-side; expiry times come from configuration, and 2FA-stage tokens get a shorter expiry.
The JWT carries these claims (sensitive ones are encrypted with the platform key):
| Claim | Holds |
|---|---|
username, displayname |
Email and display name |
userid |
Membership user id (encrypted) |
personkey |
The person's public key in the tenant |
organisationid, organisationname |
The customer (id encrypted) |
companyid |
The tenant company id (encrypted) |
secretkey |
The organisation's secret key (encrypted). This is what selects the tenant database. |
ewaid, companyewaid |
The user's and company's ids in ewa-service |
roles, permissions |
Role names and the flattened permission list |
isadmin, context, domain, client |
Admin flag, sign-in context, white-label domain, client type |
flowchart LR
R[Request] --> S[InputSanitizationMiddleware] --> A[JWT authentication]
A --> B[BotRouteAllowlistMiddleware<br/>bot tokens only reach allow-listed routes]
B --> U[RequestUserContextMiddleware<br/>decrypts claims into the request context]
U --> P[GlobalPermissionMiddleware<br/>is this endpoint allowed?]
P --> C[Controller → Service]
C --> T[(Tenant DB chosen<br/>from secretkey)]
RequestUserContextMiddleware reads the bearer token and fills a per-request context (userName, userId, personKey, organisationId, companyId, isAdmin, correlationId). Every service reads it through ICustomContextAccessorService.GetCurrentContext(). The correlation id is taken from the caller's header when present, so logs across services can be joined.
⚠️ The context lives in an AsyncLocal
It flows down into awaited calls but not back up. Background workers and signed channel endpoints must populate the context in the same method that does the work, never inside a helper. Doing it in a helper silently leaves the work with no tenant.
sequenceDiagram
participant Svc as Any service class
participant F as Func
participant M as Membership DB
Svc->>F: need tenant context
F->>F: read token → decrypt "secretkey"
F->>M: Organisations WHERE SecretKey = …
M-->>F: HostDatabaseId
F->>M: HostDatabases + HostServers
M-->>F: server name, database name
F-->>Svc: TenantDbContext on that server/database