For the complete documentation index, see llms.txt. This page is also available as Markdown.

How to configure scopes

Scopes are the mechanism that grants users access to perform role-specific actions. At a high level, a scope has two properties:

  • Type — which action the user can take with the scope (e.g. record.create, record.read, user.create).

  • Options — the limitations under which the action can be performed (e.g. a user can only search birth events that took place in their administrative area: { type: 'record.search', options: { event: ['birth'], placeOfEvent: 'administrativeArea' } }).

To perform actions, a user must be assigned to a role with appropriate scopes. You can find the available scopes here.

To get started, let's look at the example configuration. opencrvs-core exposes a helper for defining scopes, and provides type information.

Example: scope that allows user to search for birth records which took place within their administrative area.

import { defineScopes } from '@opencrvs/toolkit/scopes'
const scopes = defineScopes([{
    type: 'record.search',
    options: {
        event: ['birth'],
        placeOfEvent: 'administrativeArea'
    }
}]) // ['type=record.search&placeOfEvent=administrativeArea&event=birth']

Scopes are encoded as query strings and included in the JWT provided at user login. The token is passed around the system, and user access is determined by its scope contents.

Example: JWT payload for the example user

{
  "scope": [
    "type=record.search&placeOfEvent=administrativeArea&event=birth",
  ],
  "userType": "user",
  "role": "REGISTRATION_AGENT",
  "iat": 1778663757,
  "exp": 1779268557,
  "aud": [
    "opencrvs:events-user",
  ],
  "iss": "opencrvs:auth-service",
  "sub": "783a5bbc-d0f0-4fce-a65e-2afc868d2e41"
}

How scope options are evaluated

Example 1: Single scope

  • registrationAgent can access birthEventplaceOfEvent includes their administrativeAreaId, and the event type matches event: ['birth'] in their scope.

  • registrationAgent cannot access deathEventplaceOfEvent includes their administrativeAreaId, but the event type does not match event: ['birth'] in their scope.

Scope options are combined using AND. For access to be granted, all of the conditions defined in the options must be met.

Using multiple scopes for the same scope type

Sometimes access conditions are too complex to define with a single scope. You can provide multiple scopes for the same type — these are combined using OR, meaning it is sufficient for one scope to match completely.

Example 2: Two scopes for the same type

  • registrationAgent can access birthEventplaceOfEvent includes their administrativeAreaId, and the event type matches event: ['birth'] in the first scope.

  • registrationAgent can access deathEventplaceOfEvent includes their primaryOfficeId, and the event type matches event: ['death'] in the second scope.

Last updated