@adtrackify/at-service-common 1.0.39 → 1.0.40

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.39",
3
+ "version": "1.0.40",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,3 +1,4 @@
1
1
  export * from './input-validation-helper';
2
2
  export * from './logging-helper';
3
- export * from './response-helper'
3
+ export * from './response-helper';
4
+ export * from './shopify-helper';
@@ -1,13 +1,13 @@
1
- import { HttpError } from '../libs/http-error';
2
- import * as log from 'lambda-log';
3
1
  import Joi from 'joi';
2
+ import * as log from 'lambda-log';
3
+ import { HttpError } from '../libs/http-error';
4
4
 
5
5
  export const validateInput = (schema: Joi.ObjectSchema<any>, input: any) => {
6
6
  const { error, value } = schema.validate(input);
7
7
  if (error) {
8
8
  log.info('', { error });
9
9
 
10
- const httperr = HttpError.badRequest('Input Validation Failure', {
10
+ const httperr = HttpError.badRequest('Bad Request', {
11
11
  errors: error.details.map(detail => ({
12
12
  message: detail?.message,
13
13
  key: detail?.context?.key,
@@ -0,0 +1,39 @@
1
+ import { createHmac } from 'crypto';
2
+ import * as log from 'lambda-log';
3
+ import { HttpError } from '../libs';
4
+ import { mapObjectToQueryString } from '../libs/url';
5
+ export interface ShopifyRequestValidationParameters {
6
+ code: string,
7
+ hmac?: string,
8
+ shop: string,
9
+ state: string,
10
+ timestamp: string;
11
+ }
12
+
13
+ export const isShopifyRequestValid = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string): boolean => {
14
+ // remove hmac if it exists
15
+ // map input to query string
16
+ // generate hash using api secret key and validate it matches hmac
17
+ delete validationParams.hmac;
18
+ const hmacString = mapObjectToQueryString(validationParams);
19
+
20
+
21
+ const generatedHash = createHmac('sha256', shopifyAppApiSecret)
22
+ .update(hmacString)
23
+ .digest('hex');
24
+
25
+ return generatedHash === validationHmac;
26
+ };
27
+
28
+ export const validateShopifyRequest = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string) => {
29
+ log.info('Validating shopify request is authentic', { validationParams });
30
+ const isValid = isShopifyRequestValid(validationParams, validationHmac as string, shopifyAppApiSecret);
31
+ if (!isValid) {
32
+ const message = 'Failed: Shopify Request hmac validation';
33
+ log.error(message);
34
+ throw HttpError.badRequest(message);
35
+ }
36
+ log.info('Sucess: Shopify Request hmac validation');
37
+ return true;
38
+ }
39
+
package/src/libs/index.ts CHANGED
@@ -1,7 +1,7 @@
1
+ export * from '../helpers/shopify-helper';
1
2
  export * from './crypto';
2
3
  export * from './dates';
3
4
  export * from './http-error';
4
5
  export * from './http-status-codes';
5
- export * from './shopify';
6
6
  export * from './url';
7
7
 
@@ -1,25 +0,0 @@
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
- };