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
✅
registrationAgentcan accessbirthEvent—placeOfEventincludes theiradministrativeAreaId, and the event type matchesevent: ['birth']in their scope.❌
registrationAgentcannot accessdeathEvent—placeOfEventincludes theiradministrativeAreaId, but the event type does not matchevent: ['birth']in their scope.
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
✅
registrationAgentcan accessbirthEvent—placeOfEventincludes theiradministrativeAreaId, and the event type matchesevent: ['birth']in the first scope.✅
registrationAgentcan accessdeathEvent—placeOfEventincludes theirprimaryOfficeId, and the event type matchesevent: ['death']in the second scope.
Last updated