From a5f538eb62a45751cba83b06dde2cc3ba5e157b4 Mon Sep 17 00:00:00 2001 From: Hyper3x Date: Tue, 12 Nov 2024 11:35:49 +0530 Subject: [PATCH] Required changes done --- .../features/registration/root-resolvers.ts | 75 ++++++------------- .../features/registration/token-exchange.ts | 61 --------------- ...0220826062206-channels-v1.0.1-to-v1.1.0.ts | 4 +- packages/workflow/src/config/routes.ts | 10 --- 4 files changed, 23 insertions(+), 127 deletions(-) delete mode 100644 packages/gateway/src/features/registration/token-exchange.ts diff --git a/packages/gateway/src/features/registration/root-resolvers.ts b/packages/gateway/src/features/registration/root-resolvers.ts index 26f27e9ea6d..6ffbbc7c449 100644 --- a/packages/gateway/src/features/registration/root-resolvers.ts +++ b/packages/gateway/src/features/registration/root-resolvers.ts @@ -63,7 +63,6 @@ import { confirmRegistration } from '@gateway/workflow/index' import { getRecordById } from '@gateway/records' -import { tokenExchangeHandler } from './token-exchange' async function getAnonymousToken() { const res = await fetch(new URL('/anonymous-token', AUTH_URL).toString()) @@ -639,78 +638,46 @@ export const resolvers: GQLResolver = { return taskEntry.resource.id }, - async confirmRegistration(_, { id, details }, { headers: authHeader }) { - const token = authHeader.Authorization.replace("Bearer ", "").trim(); - const recordSpecificToken = await tokenExchangeHandler( - token, - authHeader, - id - ) - - authHeader.Authorization = `Bearer ${recordSpecificToken.access_token}` - + async confirmRegistration(_, { id, details }, { headers: authHeader }) { if (!inScope(authHeader, ['record.confirm-registration'])) { - throw new Error('User does not have a Confirm Registration scope'); + throw new Error('User does not have a Confirm Registration scope') } if (!hasRecordAccess(authHeader, id)) { - throw new Error('User does not have access to the record'); + throw new Error('User does not have access to the record') } try { - const taskEntry = await confirmRegistration( - id, - authHeader, - { - error: details.error, - registrationNumber: details.registrationNumber, - childIdentifiers: details.identifiers, - compositionId: id - } - ); - - return taskEntry.resource.id; + const taskEntry = await confirmRegistration(id, authHeader, { + error: details.error, + registrationNumber: details.registrationNumber, + childIdentifiers: details.identifiers, + compositionId: id + }) + + return taskEntry.resource.id } catch (error) { - throw new Error(`Failed to confirm registration: ${error.message}`); + throw new Error(`Failed to confirm registration: ${error.message}`) } }, - async rejectRegistration( - _, - { id, details }, - { headers: authHeader } - ) { - - const token = authHeader.Authorization.replace("Bearer ", "").trim(); - const recordSpecificToken = await tokenExchangeHandler( - token, - authHeader, - id - ) - - authHeader.Authorization = `Bearer ${recordSpecificToken.access_token}` - + async rejectRegistration(_, { id, details }, { headers: authHeader }) { if (!inScope(authHeader, ['record.reject-registration'])) { - throw new Error('User does not have a Reject Registration" scope'); + throw new Error('User does not have a Reject Registration" scope') } if (!hasRecordAccess(authHeader, id)) { - throw new Error('User does not have access to the record'); + throw new Error('User does not have access to the record') } try { - const taskEntry = await rejectRegistration( - id, - authHeader, - { - comment: details.comment || 'No comment provided', - reason: details.reason - } - ); + const taskEntry = await rejectRegistration(id, authHeader, { + comment: details.comment || 'No comment provided', + reason: details.reason + }) - // Return the task ID from the rejected record - return taskEntry.resource.id; + return taskEntry.resource.id } catch (error) { - throw new Error(`Error in rejectRegistration: ${error.message}`); + throw new Error(`Error in rejectRegistration: ${error.message}`) } } } diff --git a/packages/gateway/src/features/registration/token-exchange.ts b/packages/gateway/src/features/registration/token-exchange.ts deleted file mode 100644 index 624ea864d12..00000000000 --- a/packages/gateway/src/features/registration/token-exchange.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * OpenCRVS is also distributed under the terms of the Civil Registration - * & Healthcare Disclaimer located at http://opencrvs.org/license. - * - * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. - */ -import { AUTH_URL } from '@gateway/constants' -import { IAuthHeader } from '@opencrvs/commons' -import fetch from 'node-fetch' - -export async function tokenExchangeHandler( - token: string | undefined, - headers: IAuthHeader, - recordId: string | undefined -): Promise { - // recordId = undefined - const grantType = 'urn:opencrvs:oauth:grant-type:token-exchange' - const subject_token_type = 'urn:ietf:params:oauth:token-type:access_token' - const requested_token_type = - 'urn:opencrvs:oauth:token-type:single_record_token' - - try { - if (!recordId) - throw new Error( - 'Error occured when calling the token exchange handler. Record ID not found!' - ) - if (!token) - throw new Error( - 'Error occured when calling the token exchange handler. Token not found!' - ) - const authUrl = new URL( - `token?grant_type=${grantType}&subject_token=${token}&subject_token_type=${subject_token_type}&requested_token_type=${requested_token_type}&record_id=${recordId}`, - AUTH_URL - ).toString() - - const res = await fetch(authUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - ...headers - } - }) - if (!res.ok) { - const errorData = await res.json(); - console.error('Token exchange error response:', errorData); - throw new Error( - `Error occured when calling the token exchange handler. ${JSON.stringify( - errorData - )}` - ) - } - const body = await res.json() - return body - } catch (error) { - throw new Error(`Token exchange execution failed!. ${error}`) - } -} \ No newline at end of file diff --git a/packages/migration/src/migrations/openhim/20220826062206-channels-v1.0.1-to-v1.1.0.ts b/packages/migration/src/migrations/openhim/20220826062206-channels-v1.0.1-to-v1.1.0.ts index 9ac521a9a35..94822e47b2d 100644 --- a/packages/migration/src/migrations/openhim/20220826062206-channels-v1.0.1-to-v1.1.0.ts +++ b/packages/migration/src/migrations/openhim/20220826062206-channels-v1.0.1-to-v1.1.0.ts @@ -40,7 +40,7 @@ const newChannel: Channel = { secured: false, host: 'workflow', port: 5050, - path: '/records/{id}/confirm', + path: '/confirm/registration', pathTransform: '', primary: true, username: '', @@ -52,7 +52,7 @@ const newChannel: Channel = { rewriteUrlsConfig: [], name: 'Confirm Registration', description: 'Confirm registration & assign a BRN', - urlPattern: '^/records/{id}/confirm$', + urlPattern: '^/confirm/registration$', priority: 1, maxBodyAgeDays: 30, matchContentRegex: null, diff --git a/packages/workflow/src/config/routes.ts b/packages/workflow/src/config/routes.ts index 3513daabf88..f7a649ded9e 100644 --- a/packages/workflow/src/config/routes.ts +++ b/packages/workflow/src/config/routes.ts @@ -59,16 +59,6 @@ export const getRoutes = () => { description: 'Health check endpoint' } }, - // { - // method: 'POST', - // path: '/confirm/registration', - // handler: markEventAsRegisteredCallbackHandler, - // config: { - // tags: ['api'], - // description: - // 'Register event based on tracking id and registration number.' - // } - // }, { method: 'POST', path: '/records/{id}/confirm',