@adtrackify/at-service-common 1.0.37 → 1.0.39

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adtrackify/at-service-common",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/src/libs/index.ts CHANGED
@@ -2,3 +2,6 @@ export * from './crypto';
2
2
  export * from './dates';
3
3
  export * from './http-error';
4
4
  export * from './http-status-codes';
5
+ export * from './shopify';
6
+ export * from './url';
7
+
@@ -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
+ };
@@ -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
+ };