ballerinax/health.fhir.r4.authz Ballerina library

1.0.0
FHIR R4 Authorization Library

A reusable Ballerina library that provides role-based access control for FHIR R4 APIs. It evaluates whether a user is authorized to access patient data based on their role: Patient, Practitioner, or Privileged User.

Authorization Flow

The library uses a cascading authorization check:

  1. Patient - Is the user a patient accessing their own data? (JWT claim must match the requested patientId)
  2. Practitioner - Is the user a practitioner associated with the patient? (customizable logic)
  3. Privileged User - Does the user have a privileged claim set to true? (checks JWT)

If no patientId is provided in the request (bulk data access), only privileged user authorization is checked.

Usage

Basic Usage (Default Configuration)

Copy
import ballerinax/health.fhir.r4.authz;
import ballerinax/health.fhir.r4;

r4:AuthzResponse response = authz:authorize(authzRequest);

With default configuration:

  • Patient ID is read from the "patient" JWT claim
  • Practitioner ID is read from the "practitioner" JWT claim
  • Practitioner authorization denies all requests (you must provide your own logic)
  • Privileged user authorization checks the JWT claim specified in privilegedClaimUrl

Custom Configuration

Copy
import ballerinax/health.fhir.r4.authz;
import ballerinax/health.fhir.r4;

authz:AuthzConfig config = {
    patientIdClaim: "sub",
    practitionerIdClaim: "doctor_id",
    authorizePractitioner: myPractitionerAuthFn,
    authorizePrivilegedUser: myPrivilegedUserAuthFn
};

r4:AuthzResponse response = authz:authorize(authzRequest, config);

Custom Practitioner Authorization

The default practitioner authorization denies all requests. You should provide your own implementation that checks whether a practitioner is associated with the patient (e.g., via a database lookup).

Copy
isolated function myPractitionerAuthFn(string patientId, string practitionerId) returns r4:AuthzResponse {
    // Example: check a database to see if the practitioner is associated with the patient
    boolean isAssociated = check db->queryRow(
        `SELECT COUNT(*) > 0 FROM patient_practitioner 
         WHERE patient_id = ${patientId} AND practitioner_id = ${practitionerId}`
    );
    if (isAssociated) {
        return {isAuthorized: true, scope: r4:PRACTITIONER};
    }
    return {isAuthorized: false};
}

Custom Privileged User Authorization

The default implementation checks the JWT for a claim (specified by privilegedClaimUrl in the request) set to "true". You can override this with your own logic.

Copy
isolated function myPrivilegedUserAuthFn(r4:AuthzRequest & readonly authzRequest) returns r4:AuthzResponse {
    // Example: check for a specific role claim
    anydata|error role = authz:getClaimValue("http://example.org/claims/role", authzRequest);
    if (role is string && role == "admin") {
        return {isAuthorized: true, scope: r4:PRIVILEGED};
    }
    return {isAuthorized: false};
}

API Reference

Functions

FunctionDescription
authorize(r4:AuthzRequest & readonly authzRequest, AuthzConfig config = {}) returns r4:AuthzResponseMain authorization function with cascading role checks
getClaimValue(string claimName, r4:AuthzRequest payload) returns anydata|errorExtract a JWT claim value from an authorization request
defaultAuthorizePrivilegedUser(r4:AuthzRequest & readonly authzRequest) returns r4:AuthzResponseDefault privileged user check (validates JWT privileged claim)
defaultAuthorizePractitioner(string patientId, string practitionerId) returns r4:AuthzResponseDefault practitioner check (denies all — override with your own)

Types

TypeDescription
AuthzConfigConfiguration record with claim names and custom authorization functions
AuthorizePractitionerFunctionFunction type for custom practitioner authorization logic
AuthorizePrivilegedUserFunctionFunction type for custom privileged user authorization logic

AuthzConfig Fields

FieldTypeDefaultDescription
patientIdClaimstring"patient"JWT claim name for patient ID
practitionerIdClaimstring"practitioner"JWT claim name for practitioner ID
authorizePractitionerAuthorizePractitionerFunctiondefaultAuthorizePractitionerCustom practitioner authorization function
authorizePrivilegedUserAuthorizePrivilegedUserFunctiondefaultAuthorizePrivilegedUserCustom privileged user authorization function

Import

import ballerinax/health.fhir.r4.authz;Copy

Other versions

1.0.0

Metadata

Released date: 2 days ago

Version: 1.0.0


Compatibility

Platform: any

Ballerina version: 2201.12.8

GraalVM compatible: Yes


Pull count

Total: 5

Current verison: 5


Weekly downloads



Keywords

Healthcare

FHIR

Authorization


Contributors