@@ -27,12 +27,26 @@ class HmacValidator {
27
27
public static HMAC_SHA256_ALGORITHM = "sha256" ;
28
28
public static DATA_SEPARATOR = ":" ;
29
29
30
+ /**
31
+ * Calculate HMAC signature of the payload data
32
+ * @param data payload as String or as NotificationRequestItem
33
+ * @param key HMAC key
34
+ * @returns HMAC signature
35
+ */
30
36
public calculateHmac ( data : string | NotificationRequestItem , key : string ) : string {
31
37
const dataString = typeof data !== "string" ? this . getDataToSign ( data ) : data ;
32
38
const rawKey = Buffer . from ( key , "hex" ) ;
33
39
return createHmac ( HmacValidator . HMAC_SHA256_ALGORITHM , rawKey ) . update ( dataString , "utf8" ) . digest ( "base64" ) ;
34
40
}
35
41
42
+ /**
43
+ * @deprecated use Use validateHMACSignature with correct parameter order instead
44
+ * Validate HMAC signature for Banking webhooks
45
+ * @param hmacKey
46
+ * @param hmacSign
47
+ * @param notification
48
+ * @returns
49
+ */
36
50
public validateBankingHMAC ( hmacKey : string , hmacSign : string , notification : string ) : boolean {
37
51
const expectedSign = createHmac ( HmacValidator . HMAC_SHA256_ALGORITHM , Buffer . from ( hmacSign , "hex" ) ) . update ( notification , "utf8" ) . digest ( "base64" ) ;
38
52
if ( hmacKey ?. length === expectedSign . length ) {
@@ -44,6 +58,30 @@ class HmacValidator {
44
58
return false ;
45
59
}
46
60
61
+ /**
62
+ * Validate HMAC signature for Banking/Management webhooks
63
+ * @param hmacKey HMAC key
64
+ * @param hmacSignature HMAC signature to validate
65
+ * @param data webhook payload (as string)
66
+ * @returns true when HMAC signature is valid
67
+ */
68
+ public validateHMACSignature ( hmacKey : string , hmacSignature : string , data : string ) : boolean {
69
+ const expectedSign = createHmac ( HmacValidator . HMAC_SHA256_ALGORITHM , Buffer . from ( hmacKey , "hex" ) ) . update ( data , "utf8" ) . digest ( "base64" ) ;
70
+ if ( hmacSignature ?. length === expectedSign . length ) {
71
+ return timingSafeEqual (
72
+ Buffer . from ( expectedSign , "base64" ) ,
73
+ Buffer . from ( hmacSignature , "base64" )
74
+ ) ;
75
+ }
76
+ return false ;
77
+ }
78
+
79
+ /**
80
+ * Validate HMAC signature for Payment webhooks
81
+ * @param notificationRequestItem webhook payload (as NotificationRequestItem object)
82
+ * @param key HMAC key
83
+ * @returns true when HMAC signature is valid
84
+ */
47
85
public validateHMAC ( notificationRequestItem : NotificationRequestItem , key : string ) : boolean {
48
86
if ( notificationRequestItem . additionalData ?. [ ApiConstants . HMAC_SIGNATURE ] ) {
49
87
const expectedSign = this . calculateHmac ( notificationRequestItem , key ) ;
@@ -55,7 +93,6 @@ class HmacValidator {
55
93
) ;
56
94
}
57
95
return false ;
58
-
59
96
}
60
97
throw Error ( `Missing ${ ApiConstants . HMAC_SIGNATURE } ` ) ;
61
98
}
@@ -64,6 +101,11 @@ class HmacValidator {
64
101
return ! Object . values ( item ) . every ( ( value ) : boolean => typeof value === "string" ) ;
65
102
}
66
103
104
+ /**
105
+ * extract fields to be used to calculate the HMAC signature
106
+ * @param notificationRequestItem webhook payload
107
+ * @returns data to sign (as string)
108
+ */
67
109
public getDataToSign ( notificationRequestItem : DataToSign ) : string {
68
110
if ( this . isNotificationRequestItem ( notificationRequestItem ) ) {
69
111
const signedDataList = [ ] ;
0 commit comments