Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/auth_bpp_webhook' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Y committed Jun 26, 2024
2 parents 6862187 + 223afb6 commit 5753d93
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 14 deletions.
6 changes: 6 additions & 0 deletions config/config-sample-client-localhost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/config-sample-network-localhost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/new-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/samples/bap-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/samples/bap-network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/samples/bpp-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
6 changes: 6 additions & 0 deletions config/samples/bpp-network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ app:
# In minutes
syncInterval: 30
redis_db: 3
<<<<<<< HEAD

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
=======

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""
>>>>>>> origin/auth_bpp_webhook
7 changes: 6 additions & 1 deletion src/controllers/bpp.request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createTelemetryEvent,
processTelemetry
} from "../utils/telemetry.utils";
import { createBppWebhookAuthHeaderConfig } from "../utils/auth.utils";

export const bppNetworkRequestHandler = async (
req: Request,
Expand Down Expand Up @@ -90,7 +91,11 @@ export const bppNetworkRequestSettler = async (
break;
}
case ClientConfigType.webhook: {
requestCallback(requestBody);
let axios_config = {};
if (getConfig().app.useHMACForWebhook) {
axios_config = await createBppWebhookAuthHeaderConfig(requestBody);
}
requestCallback(requestBody, axios_config);
break;
}
case ClientConfigType.messageQueue: {
Expand Down
5 changes: 5 additions & 0 deletions src/schemas/configs/app.config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ export const appConfigSchema = z.object({
unsolicitedWebhook: z.object({
url: z.string().optional()
}).optional(),
<<<<<<< HEAD
openAPIValidator: z.object({
cachedFileLimit: z.number().optional()
}).optional()
=======
useHMACForWebhook: z.boolean().optional(),
sharedKeyForWebhookHMAC: z.string().optional(),
>>>>>>> origin/auth_bpp_webhook
});

export type AppConfigDataType = z.infer<typeof appConfigSchema>;
Expand Down
31 changes: 26 additions & 5 deletions src/utils/auth.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ export const createAuthorizationHeader = async (message: any) => {
getConfig().app.privateKey || ""
);
const subscriber_id = getConfig().app.subscriberId;
const header = `Signature keyId="${subscriber_id}|${
getConfig().app.uniqueKey
}|ed25519",algorithm="ed25519",created="${created}",expires="${expires}",headers="(created) (expires) digest",signature="${signature}"`;
const header = `Signature keyId="${subscriber_id}|${getConfig().app.uniqueKey
}|ed25519",algorithm="ed25519",created="${created}",expires="${expires}",headers="(created) (expires) digest",signature="${signature}"`;
return header;
};

Expand Down Expand Up @@ -204,8 +203,30 @@ export const createAuthHeaderConfig = async (request: any) => {
timeout: getConfig().app.httpTimeout
};
logger.info(
`Axios Config for Request from ${
getConfig().app.mode + " " + getConfig().app.gateway.mode
`Axios Config for Request from ${getConfig().app.mode + " " + getConfig().app.gateway.mode
}:==>\n${JSON.stringify(axios_config)}\n\n`
);
return axios_config;
};

const createBppWebhookAuthHeader = async (request: any) => {
const sodium = _sodium;
const key = getConfig().app.sharedKeyForWebhookHMAC || "";
const keyUint8Array = sodium.from_string(key);
const messageUint8Array = sodium.from_string(JSON.stringify(request));
const hmac = sodium.crypto_auth(messageUint8Array, keyUint8Array);
return sodium.to_hex(hmac);
};

export const createBppWebhookAuthHeaderConfig = async (request: any) => {
const header = await createBppWebhookAuthHeader(request);
const axios_config = {
headers: {
authorization: header
}
};
logger.info(
`Axios Config for Request from ${getConfig().app.mode + " " + getConfig().app.gateway.mode
}:==>\n${JSON.stringify(axios_config)}\n\n`
);
return axios_config;
Expand Down
14 changes: 6 additions & 8 deletions src/utils/callback.utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import axios from "axios";
import axios, { AxiosRequestConfig } from "axios";
import { Exception, ExceptionType } from "../models/exception.model";
import {
BecknErrorDataType,
becknErrorSchema
BecknErrorDataType
} from "../schemas/becknError.schema";
import { requestCallbackSchema } from "../schemas/callbacks/request.callback.schema";
import { responseCallbackSchema } from "../schemas/callbacks/response.callback.schema";
import {
ClientConfigType,
WebhookClientConfigDataType
} from "../schemas/configs/client.config.schema";
import { GatewayMode } from "../schemas/configs/gateway.app.config.schema";
import { getConfig } from "./config.utils";
import logger from "./logger.utils";

async function makeClientCallback(data: any) {
async function makeClientCallback(data: any, axios_config?: AxiosRequestConfig) {
try {
if (getConfig().client.type != ClientConfigType.webhook) {
throw new Exception(
Expand All @@ -30,7 +28,7 @@ async function makeClientCallback(data: any) {
console.log(
`TMTR - ${data?.context?.message_id} - ${data?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW EXIT: ${new Date().valueOf()}`
);
const response = await axios.post(clientConnectionConfig.url, data);
const response = await axios.post(clientConnectionConfig.url, data, axios_config || {});
logger.info(
`Response from Webhook:==>\n ${JSON.stringify(
response.data
Expand Down Expand Up @@ -88,10 +86,10 @@ export async function responseCallback(data: any) {
}
}

export async function requestCallback(data: any) {
export async function requestCallback(data: any, axios_config?: AxiosRequestConfig) {
try {
const callbackData = requestCallbackSchema.parse(data);
await makeClientCallback(callbackData);
await makeClientCallback(callbackData, axios_config);
} catch (error) {
if (error instanceof Exception) {
throw error;
Expand Down

0 comments on commit 5753d93

Please sign in to comment.