Skip to content

Authentication & authorisation ​

Luna's auth service handles who a user is (authentication) and what they're allowed to do (authorisation). This page covers how both work end to end; for the detail of how permissions themselves are modelled and checked, see Permissions (CASL).

Authentication ​

Luna authenticates users through the AWS Cognito service, configured with the AND Digital Okta instance as the sole identity provider.

Click image to open full-size with zoom.

authentication

Exchanging the Cognito token ​

Once a user authenticates against Cognito, the app calls the auth service to exchange the Cognito-issued JWT for a Luna-issued JWT that carries the user's roles (services/auth/exchange/src/handlers/exchangeToken.ts). This Luna JWT is what the rest of Luna trusts for authorisation — it's asymmetrically signed so every service can verify it without needing access to the signing key itself.

Authorisation is designed to stay stateless throughout the application's lifetime, to scale to both internal ANDis and external users.

Signing the JWT ​

The Luna JWT is signed using an asymmetric key managed by AWS KMS (sign in services/auth/common/signJwt.ts) — only the auth service has access to the private key. Every other service verifies the JWT using the corresponding public key, fetched from KMS by key alias (packages/auth/src/decodeKmsToken.ts, packages/auth/src/getPublicKey.ts).

The token is hardcoded to a 24-hour lifetime — exp is set to iat plus one day when it's signed.

Authorisation ​

Permissions are not embedded in the JWT itself — only the user's roles are. On each request, authoriseRequest (packages/auth/src/authoriseRequest.ts) verifies the JWT and, when a handler asks for permissions, resolves them dynamically by looking up the roles carried in the JWT against the live roles table (packages/auth/src/getPermissions.ts). checkPermissions (packages/auth/src/permissionCheck.ts) then evaluates the request against those resolved permissions using CASL — see Permissions (CASL) for how roles and permissions are structured, and how to call checkPermissions from a service or the frontend.

Because permissions are resolved live rather than cached in the token, a role change takes effect on the very next permission-checked request — it doesn't have to wait for the JWT to expire.

Lifecycle of a role change ​

The JWT itself still only reflects a user's roles as of the last time it was issued — up to 24 hours stale. That doesn't matter for permissions, since those are resolved fresh against the live roles table on every request rather than read from the token (see Authorisation); only the roles claim itself waits for the next re-issued token to catch up.

Example scenario ​

Joe Blogs in HR signs into Luna and has permission to change someone's salary. At the end of the day, his role is updated to recruitment, which no longer includes that permission:

  1. Joe carries on using parts of Luna unrelated to salaries — no relevant permissions have changed for those, so he shouldn't experience any interruption.
  2. Joe tries to change someone's salary again — he should get an error saying his permissions have been updated due to a role change and he can no longer perform this action, with the option to contact someone if this is a mistake, or navigate away from the restricted area.

Made with ❤️ by the Luna team