11import { HapiRequest , HapiServer } from "../types" ;
2+ import { createHmacRaw } from "../utils/hmac" ;
23import {
34 CacheService ,
45 NotifyService ,
@@ -130,6 +131,29 @@ export class StatusService {
130131
131132 let newReference ;
132133
134+ /**
135+ * If the OPTIONAL config contains webhookHmacSharedKey, then we send HMAC Auth headers
136+ * This is used to confirm ONLY X-Gov's backend is sending data to our API
137+ * Everyone else will be Rejected
138+ */
139+ const id = request . params ?. id ;
140+ const forms = request . server ?. app ?. forms ;
141+ const model = id && forms ?. [ id ] ;
142+ const hmacKey = model ?. def ?. webhookHmacSharedKey ;
143+ let customSecurityHeaders : Record < string , string > = { } ;
144+
145+ if ( hmacKey ) {
146+ const [ hmacSignature , requestTime , hmacExpiryTime ] = await createHmacRaw (
147+ request . yar . id ,
148+ hmacKey
149+ ) ;
150+ customSecurityHeaders = {
151+ "X-Request-ID" : request . yar . id . toString ( ) ,
152+ "X-HMAC-Signature" : hmacSignature . toString ( ) ,
153+ "X-HMAC-Time" : requestTime . toString ( ) ,
154+ } ;
155+ }
156+
133157 if ( callback ) {
134158 this . logger . info (
135159 [ "StatusService" , "outputRequests" ] ,
@@ -153,7 +177,8 @@ export class StatusService {
153177 firstWebhook . outputData . url ,
154178 { ...formData } ,
155179 "POST" ,
156- firstWebhook . outputData . sendAdditionalPayMetadata
180+ firstWebhook . outputData . sendAdditionalPayMetadata ,
181+ customSecurityHeaders
157182 ) ;
158183 await this . cacheService . mergeState ( request , {
159184 reference : newReference ,
@@ -178,7 +203,8 @@ export class StatusService {
178203 ...formData ,
179204 } ,
180205 "POST" ,
181- sendAdditionalPayMetadata
206+ sendAdditionalPayMetadata ,
207+ customSecurityHeaders
182208 )
183209 ) ,
184210 ] ;
0 commit comments