@adtrackify/at-service-common 1.0.39 → 1.0.41
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 +21 -17
- package/dist/index.js +166 -151
- package/dist/index.js.map +4 -4
- package/package.json +2 -2
- package/src/clients/internal-api/users-auth-client.ts +9 -2
- package/src/helpers/index.ts +2 -1
- package/src/helpers/input-validation-helper.ts +3 -3
- package/src/helpers/shopify-helper.ts +39 -0
- package/src/libs/index.ts +1 -1
- package/src/libs/shopify.ts +0 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adtrackify/at-service-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.41",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@types/axios": "^0.14.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@adtrackify/at-tracking-event-types": "^1.0.
|
|
41
|
+
"@adtrackify/at-tracking-event-types": "^1.0.22",
|
|
42
42
|
"@babel/cli": "^7.13.16",
|
|
43
43
|
"@babel/core": "^7.13.10",
|
|
44
44
|
"@babel/plugin-proposal-optional-chaining": "^7.13.8",
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { User } from '@adtrackify/at-tracking-event-types';
|
|
2
2
|
import * as log from 'lambda-log';
|
|
3
|
+
import { ApiResponse } from '../../types/api-response';
|
|
4
|
+
import { axiosHttpService } from '../generic/http-client';
|
|
5
|
+
|
|
6
|
+
export interface UserResponseData {
|
|
7
|
+
user: User;
|
|
8
|
+
[ key: string ]: any;
|
|
9
|
+
}
|
|
3
10
|
|
|
4
11
|
export class UsersAuthClient {
|
|
5
12
|
|
|
@@ -33,7 +40,7 @@ export class UsersAuthClient {
|
|
|
33
40
|
return signupUserResponse;
|
|
34
41
|
};
|
|
35
42
|
|
|
36
|
-
getUserByEmail = async (email: string) => {
|
|
43
|
+
getUserByEmail = async (email: string): Promise<ApiResponse<UserResponseData>> => {
|
|
37
44
|
const client = await this.getClient();
|
|
38
45
|
const getUserResponse = await client.get('/lookup', {
|
|
39
46
|
headers: {
|
package/src/helpers/index.ts
CHANGED
|
@@ -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('
|
|
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
package/src/libs/shopify.ts
DELETED
|
@@ -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
|
-
};
|