@adtrackify/at-service-common 1.0.37 → 1.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +17 -0
- package/dist/index.js +25 -1
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
- package/src/libs/index.ts +3 -0
- package/src/libs/shopify.ts +25 -0
- package/src/libs/url.ts +10 -0
package/package.json
CHANGED
package/src/libs/index.ts
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createHmac } from 'crypto';
|
|
2
|
+
import { mapObjectToQueryString } from './url';
|
|
3
|
+
|
|
4
|
+
export interface ShopifyRequestValidationParameters {
|
|
5
|
+
code: string,
|
|
6
|
+
hmac?: string,
|
|
7
|
+
shop: string,
|
|
8
|
+
state: string,
|
|
9
|
+
timestamp: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const validateShopifyAppParameters = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string) => {
|
|
13
|
+
// remove hmac if it exists
|
|
14
|
+
// map input to query string
|
|
15
|
+
// generate hash using api secret key and validate it matches hmac
|
|
16
|
+
delete validationParams.hmac;
|
|
17
|
+
const hmacString = mapObjectToQueryString(validationParams);
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const generatedHash = createHmac('sha256', shopifyAppApiSecret)
|
|
21
|
+
.update(hmacString)
|
|
22
|
+
.digest('hex');
|
|
23
|
+
|
|
24
|
+
return generatedHash === validationHmac;
|
|
25
|
+
};
|
package/src/libs/url.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Record<string, string> is any object
|
|
2
|
+
export const mapObjectToQueryString = (inputObj: any): string => {
|
|
3
|
+
const qsp = Object.entries(inputObj).sort((a, b) => a[ 0 ] < b[ 0 ] ? -1 : 1);
|
|
4
|
+
const urlParams = new URLSearchParams();
|
|
5
|
+
qsp.map(p => {
|
|
6
|
+
urlParams.append(p[ 0 ], p[ 1 ] as string);
|
|
7
|
+
});
|
|
8
|
+
const qs = urlParams.toString();
|
|
9
|
+
return qs;
|
|
10
|
+
};
|