Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

feat: Add support for GetSearchFilterBasedOnIdentity #9

Merged
merged 10 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/smartHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AllowedResourceTypesForOperationRequest,
BASE_R4_RESOURCES,
AuthorizationBundleRequest,
GetSearchFilterBasedOnIdentity,
} from 'fhir-works-on-aws-interface';
import { decode } from 'jsonwebtoken';
import { SMARTHandler } from './smartHandler';
Expand Down Expand Up @@ -1002,3 +1003,39 @@ describe('getAllowedResourceTypesForOperation', () => {
);
});
});

describe('getSearchFilterBasedOnIdentity', () => {
const authZHandler: SMARTHandler = new SMARTHandler(authZConfig, apiUrl, '4.0.1');
test('Patient identity', async () => {
// BUILD
const userIdentity = clone(patientIdentityWithoutScopes);
const request: GetSearchFilterBasedOnIdentity = {
userIdentity,
operation: 'search-type',
};

// OPERATE, CHECK
const expectedFilter = [
{
key: '_reference',
logicalOperator: 'OR',
operator: '==',
value: [patientId, `${apiUrl}${patientId}`],
},
];
await expect(authZHandler.getSearchFilterBasedOnIdentity(request)).resolves.toEqual(expectedFilter);
});

test('Practitioner identity', async () => {
// BUILD
const userIdentity = clone(practitionerIdentityWithoutScopes);
const request: GetSearchFilterBasedOnIdentity = {
userIdentity,
operation: 'search-type',
};

// OPERATE, CHECK
const expectedFilter: [] = [];
await expect(authZHandler.getSearchFilterBasedOnIdentity(request)).resolves.toEqual(expectedFilter);
});
});
20 changes: 20 additions & 0 deletions src/smartHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
FhirVersion,
BASE_STU3_RESOURCES,
BulkDataAuth,
GetSearchFilterBasedOnIdentity,
SearchFilter,
} from 'fhir-works-on-aws-interface';
import axios from 'axios';
import {
Expand Down Expand Up @@ -143,6 +145,24 @@ export class SMARTHandler implements Authorization {
}
}

async getSearchFilterBasedOnIdentity(request: GetSearchFilterBasedOnIdentity): Promise<SearchFilter[]> {
const fhirUser = this.getFhirUser(request.userIdentity);
const { hostname, resourceType, id } = fhirUser;

rsmayda marked this conversation as resolved.
Show resolved Hide resolved
if (resourceType !== 'Practitioner' && ['search-type', 'search-system'].includes(request.operation)) {
rsmayda marked this conversation as resolved.
Show resolved Hide resolved
return [
{
key: '_reference',
value: [`${resourceType}/${id}`, `${hostname}${resourceType}/${id}`],
rsmayda marked this conversation as resolved.
Show resolved Hide resolved
operator: '==',
logicalOperator: 'OR',
rsmayda marked this conversation as resolved.
Show resolved Hide resolved
},
];
}

return [];
}

async isBundleRequestAuthorized(request: AuthorizationBundleRequest): Promise<void> {
const { scopes } = request.userIdentity;
request.requests.forEach((req: BatchReadWriteRequest) => {
Expand Down