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

feat: No auth needed for route and route #5

Merged
merged 3 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lint-fix": "eslint --fix . --ext .ts,.tsx",
"build": "tsc",
"watch": "tsc -w",
"test": "jest --silent --passWithNoTests",
"test": "jest --silent",
"test-coverage": "jest --coverage",
"release": "yarn run build && yarn run lint && yarn run test",
"clean": "rm -rf build/* node_modules/* dist/* .serverless/* .nyc_output/* lib/*",
Expand Down
12 changes: 12 additions & 0 deletions src/smartHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ describe('verifyAccessToken; scopes are in an array', () => {
});
});

describe('verifyAccessToken; metadata and well-known route', () => {
const cases = [
['metadata', { accessToken: '', operation: 'read', resourceType: 'metadata' }],
['well-known', { accessToken: '', operation: 'read', resourceType: '.well-known' }],
];
const authZHandler: SMARTHandler = new SMARTHandler(authZConfig);
test.each(cases)('CASE: %p', async (_firstArg, request) => {
expect(authZHandler.verifyAccessToken(request as VerifyAccessTokenRequest)).resolves.toEqual({});
});
});

const spaceScopesCases: (string | boolean | VerifyAccessTokenRequest)[][] = [
[
'manyRead_Write',
Expand Down Expand Up @@ -266,6 +277,7 @@ const apiCases: (string | boolean | VerifyAccessTokenRequest | number | any)[][]
false,
],
];

describe("verifyAccessToken; AuthZ's userInfo interactions", () => {
const authZHandler: SMARTHandler = new SMARTHandler(authZConfig);
test.each(apiCases)('CASE: %p', async (_firstArg, request, authRespCode, authRespBody, isValid) => {
Expand Down
12 changes: 10 additions & 2 deletions src/smartHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
WriteRequestAuthorizedRequest,
AccessBulkDataJobRequest,
R4_PATIENT_COMPARTMENT_RESOURCES,
KeyValueMap,
} from 'fhir-works-on-aws-interface';
import axios from 'axios';
import { LaunchType, ScopeType, SMARTConfig } from './smartConfig';
Expand All @@ -36,7 +37,14 @@ export class SMARTHandler implements Authorization {
this.config = config;
}

async verifyAccessToken(request: VerifyAccessTokenRequest) {
async verifyAccessToken(request: VerifyAccessTokenRequest): Promise<KeyValueMap> {
if (
rsmayda marked this conversation as resolved.
Show resolved Hide resolved
request.operation === 'read' &&
(request.resourceType === 'metadata' || request.resourceType === '.well-known')
) {
return {};
}

// The access_token will be verified by hitting the authZUserInfoUrl (token introspection)
// Decoding first to determine if it passes scope & claims check first
const decoded = decode(request.accessToken, { json: true }) || {};
Expand All @@ -58,7 +66,7 @@ export class SMARTHandler implements Authorization {
}
if (!this.areScopesSufficient(scopes, request.operation, request.resourceType)) {
console.error(
`User supplied scopes are insuffiecient\nscopes: ${scopes}\noperation: ${request.operation}\nresourceType: ${request.resourceType}`,
`User supplied scopes are insufficient\nscopes: ${scopes}\noperation: ${request.operation}\nresourceType: ${request.resourceType}`,
);
throw new UnauthorizedError('User does not have permission for requested operation');
}
Expand Down