@maxzima/wa-communicator 1.0.26 → 1.0.28

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.
@@ -9,6 +9,7 @@ declare const enum CommunicatorAuthAPIEndpointEnum {
9
9
  SIGN_IN = "sign-in",
10
10
  CHALLENGE = "sign-in/challenge",
11
11
  EMAIL = "sign-in/email",
12
+ SSE_AUTH = "sse/app-auth",
12
13
  SIGN_UP = "sign-up",
13
14
  SESSION = "sign-in/session"
14
15
  }
@@ -1,10 +1,14 @@
1
- import type { TCreateSessionRequestData, TEmailUpdateRequestData, TGetCurrentUserProfileRequestData, TRegistrationRequestData, TResponse, TSetupChallengeRequestData, TSignInByMobileRequestData, TSignUpByMobileRequestData, TVerifyChallengeRequestData, TStorageRequestData, TGetRestrictedCountriesData } from "@root/types";
1
+ import { TCreateSessionRequestData, TEmailUpdateRequestData, TGetCurrentUserProfileRequestData, TRegistrationRequestData, TResponse, TSetupChallengeRequestData, TSignInByMobileRequestData, TSignUpByMobileRequestData, TVerifyChallengeRequestData, TStorageRequestData, TGetRestrictedCountriesData, TSetupSSERequestData, TSignInByAppRequestData } from "@root/types";
2
2
  export declare class Communicator {
3
3
  private props;
4
+ private sseAuth;
4
5
  constructor(props: TCommunicatorProps);
5
6
  signIn(data: TSignInByMobileRequestData): Promise<TResponse>;
7
+ signInByApp(data: TSignInByAppRequestData): Promise<TResponse>;
6
8
  signUp(data: TSignUpByMobileRequestData): Promise<TResponse>;
7
9
  setupChallenge(data: TSetupChallengeRequestData): Promise<TResponse>;
10
+ listenSSEAuth(data: TSetupSSERequestData): void;
11
+ closeSSEAuth(): void;
8
12
  verifyChallenge(data: TVerifyChallengeRequestData): Promise<TResponse>;
9
13
  emailUpdate(data: TEmailUpdateRequestData): Promise<TResponse>;
10
14
  createSession(data: TCreateSessionRequestData): Promise<TResponse>;
@@ -1,5 +1,6 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { CommunicatorDefaultRequestData } from "./../Constants/Communicator";
3
+ import { jsonParse } from "./../Utils/jsonParseSafe";
3
4
  export class Communicator {
4
5
  constructor(props) {
5
6
  this.getHeaders = (token) => {
@@ -54,6 +55,22 @@ export class Communicator {
54
55
  });
55
56
  });
56
57
  }
58
+ signInByApp(data) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const headers = this.getHeaders(data.authToken);
61
+ const body = JSON.stringify({
62
+ type: data.type,
63
+ value: data.value,
64
+ request_id: data.requestId,
65
+ });
66
+ return yield this.send({
67
+ method: 'POST',
68
+ url: `${this.props.clientAuthApiBaseUrl}${"sign-in/challenge"}`,
69
+ headers,
70
+ body,
71
+ });
72
+ });
73
+ }
57
74
  signUp(data) {
58
75
  return __awaiter(this, void 0, void 0, function* () {
59
76
  const headers = this.getHeaders();
@@ -83,6 +100,54 @@ export class Communicator {
83
100
  });
84
101
  });
85
102
  }
103
+ listenSSEAuth(data) {
104
+ if (this.sseAuth) {
105
+ this.closeSSEAuth();
106
+ }
107
+ const params = {
108
+ request_id: data.requestId,
109
+ mobile: data.phoneNumber,
110
+ };
111
+ const url = `${this.props.clientAuthSSEBaseUrl}${"sse/app-auth"}`;
112
+ this.sseAuth = new EventSource(`${url}?${this.processingQueryParams(params)}`, {
113
+ withCredentials: true,
114
+ });
115
+ if (this.sseAuth) {
116
+ this.sseAuth.addEventListener('open', (event) => {
117
+ if (this.props.env !== 'prod') {
118
+ console.log('sse open', event);
119
+ }
120
+ if (typeof data.onOpen === "function") {
121
+ data.onOpen();
122
+ }
123
+ });
124
+ this.sseAuth.addEventListener('message', (event) => {
125
+ const responseData = jsonParse(event.data || '');
126
+ if (this.props.env !== 'prod') {
127
+ console.log('sse message', event, responseData);
128
+ }
129
+ if (typeof data.onMessage === "function") {
130
+ data.onMessage(responseData);
131
+ }
132
+ });
133
+ this.sseAuth.addEventListener('error', (event) => {
134
+ const responseData = jsonParse(event.data || '');
135
+ if (this.props.env !== 'prod') {
136
+ console.log('sse error', event, responseData);
137
+ }
138
+ if (typeof data.onError === "function") {
139
+ data.onError();
140
+ }
141
+ });
142
+ }
143
+ }
144
+ closeSSEAuth() {
145
+ if (this.sseAuth) {
146
+ console.log(this.sseAuth.readyState);
147
+ this.sseAuth.close();
148
+ this.sseAuth = null;
149
+ }
150
+ }
86
151
  verifyChallenge(data) {
87
152
  return __awaiter(this, void 0, void 0, function* () {
88
153
  const headers = this.getHeaders(data.authToken);
@@ -5,6 +5,8 @@ declare type TDefaultRequestData = {
5
5
  AUDIT_SOURCE_TYPE: import('@root/types').TAuditSource;
6
6
  };
7
7
  declare type TCommunicatorProps = {
8
+ env?: 'local' | 'dev' | 'prod';
8
9
  clientAuthApiBaseUrl: string;
10
+ clientAuthSSEBaseUrl: string;
9
11
  clientFAPIBaseUrl: string;
10
12
  };
@@ -0,0 +1 @@
1
+ export declare function jsonParse<ResultType = unknown>(data: string): ResultType;
@@ -0,0 +1,10 @@
1
+ export function jsonParse(data) {
2
+ let ret = null;
3
+ try {
4
+ ret = JSON.parse(data);
5
+ }
6
+ catch (e) {
7
+ ret = null;
8
+ }
9
+ return ret;
10
+ }
@@ -1,4 +1,4 @@
1
- declare type TChallenge = 'email_otp' | 'mobile_otp' | 'pin';
1
+ declare type TChallenge = 'email_otp' | 'mobile_otp' | 'pin' | 'out_of_band';
2
2
  declare type TAuditSource = 'Backend' | 'SelfService';
3
3
  declare type TRegistrationRequestBodySourceDeviceType = 'mobile' | 'regular';
4
4
  declare type TRequestProps = {
@@ -22,6 +22,11 @@ declare type TSignInStartRequestBody = {
22
22
  meta: TAccountMeta;
23
23
  mobile?: string;
24
24
  };
25
+ declare type TSignInAppRequestBody = {
26
+ type: 'out_of_band';
27
+ value: string;
28
+ request_id: string;
29
+ };
25
30
  declare type TAccountMeta = {
26
31
  audit_source_type: TAuditSource;
27
32
  browser_page_resolution: string;
@@ -42,10 +47,20 @@ declare type TSignInByMobileRequestData = {
42
47
  deviceId: string;
43
48
  browserData: TAccountBrowserData;
44
49
  };
50
+ declare type TSignInByAppRequestData = {
51
+ authToken: string;
52
+ type: 'out_of_band';
53
+ value: string;
54
+ requestId: string;
55
+ };
45
56
  declare type TSignInByMobileResponse = {
46
57
  auth_token: string;
47
58
  challenges: TChallenge[];
48
59
  };
60
+ declare type TSignInBySSEResponse = {
61
+ request_id: string;
62
+ mobile: string;
63
+ };
49
64
  declare type TSignUpStartRequestBody = {
50
65
  locale: string;
51
66
  mobile: string;
@@ -60,6 +75,23 @@ declare type TSetupChallengeRequestData = {
60
75
  type: TChallenge;
61
76
  authToken: string;
62
77
  };
78
+ declare type TSetupSSERequestData = {
79
+ requestId: string;
80
+ phoneNumber: string;
81
+ onOpen: () => void;
82
+ onError: () => void;
83
+ onMessage: (responseData: TSSEResponseData) => void;
84
+ };
85
+ declare type TSSEResponseData = {
86
+ otp?: string;
87
+ decline_reason?: string;
88
+ message?: string;
89
+ error_code?: number;
90
+ error_text?: string;
91
+ error_context?: {
92
+ next_attempts_after?: string;
93
+ };
94
+ };
63
95
  declare type TSetupChallengeResponse = {
64
96
  attempts_left: number;
65
97
  masked_email: string;
@@ -137,4 +169,4 @@ declare type TStorageRequestData = {
137
169
  declare type TGetRestrictedCountriesData = {
138
170
  entityType?: import('@root/enums').CountryTypeEnum;
139
171
  };
140
- export { TRegistrationRequestBodySourceDeviceType, TChallenge, TAuditSource, TRequestProps, TRequestQueryParams, TResponse, TSignInStartRequestBody, TAccountMeta, TAccountBrowserData, TSignInByMobileRequestData, TSignInByMobileResponse, TSignUpStartRequestBody, TSignUpByMobileRequestData, TSetupChallengeRequestData, TSetupChallengeResponse, TVerifyChallengeRequestBody, TVerifyChallengeRequestData, TVerifyChallengeResponse, TEmailUpdateRequestData, TCreateSessionRequestData, TCreateSessionResponse, TGetCurrentUserProfileResponseUser, TGetCurrentUserProfileRequestData, TRegistrationRequestData, TStorageRequestData, TGetRestrictedCountriesData, };
172
+ export { TRegistrationRequestBodySourceDeviceType, TChallenge, TAuditSource, TRequestProps, TRequestQueryParams, TResponse, TSignInStartRequestBody, TSignInAppRequestBody, TAccountMeta, TAccountBrowserData, TSignInByMobileRequestData, TSignInByAppRequestData, TSignInByMobileResponse, TSignInBySSEResponse, TSignUpStartRequestBody, TSignUpByMobileRequestData, TSetupChallengeRequestData, TSetupSSERequestData, TSSEResponseData, TSetupChallengeResponse, TVerifyChallengeRequestBody, TVerifyChallengeRequestData, TVerifyChallengeResponse, TEmailUpdateRequestData, TCreateSessionRequestData, TCreateSessionResponse, TGetCurrentUserProfileResponseUser, TGetCurrentUserProfileRequestData, TRegistrationRequestData, TStorageRequestData, TGetRestrictedCountriesData, };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.26",
2
+ "version": "1.0.28",
3
3
  "name": "@maxzima/wa-communicator",
4
4
  "description": "",
5
5
  "author": "Noname",
@@ -10,6 +10,7 @@ const enum CommunicatorAuthAPIEndpointEnum {
10
10
  SIGN_IN = 'sign-in',
11
11
  CHALLENGE = 'sign-in/challenge',
12
12
  EMAIL = 'sign-in/email',
13
+ SSE_AUTH = 'sse/app-auth',
13
14
  SIGN_UP = 'sign-up',
14
15
  SESSION = 'sign-in/session',
15
16
  }
@@ -1,4 +1,4 @@
1
- import type {
1
+ import {
2
2
  TAccountBrowserData,
3
3
  TAccountMeta,
4
4
  TCreateSessionRequestData,
@@ -16,12 +16,19 @@ import type {
16
16
  TVerifyChallengeRequestBody,
17
17
  TVerifyChallengeRequestData,
18
18
  TStorageRequestData,
19
- TGetRestrictedCountriesData
19
+ TGetRestrictedCountriesData,
20
+ TSetupSSERequestData,
21
+ TSignInAppRequestBody,
22
+ TSignInByAppRequestData,
23
+ TSignInBySSEResponse,
24
+ TSSEResponseData,
20
25
  } from "@root/types";
21
26
  import {CommunicatorDefaultRequestData} from "./../Constants/Communicator";
27
+ import {jsonParse} from "./../Utils/jsonParseSafe";
22
28
 
23
29
  export class Communicator {
24
30
  private props: TCommunicatorProps;
31
+ private sseAuth: EventSource;
25
32
 
26
33
  constructor(props: TCommunicatorProps) {
27
34
  this.props = props;
@@ -48,6 +55,22 @@ export class Communicator {
48
55
  });
49
56
  }
50
57
 
58
+ public async signInByApp(data: TSignInByAppRequestData) {
59
+ const headers = this.getHeaders(data.authToken);
60
+ const body = JSON.stringify({
61
+ type: data.type,
62
+ value: data.value,
63
+ request_id: data.requestId,
64
+ } as TSignInAppRequestBody);
65
+
66
+ return await this.send({
67
+ method: 'POST',
68
+ url: `${this.props.clientAuthApiBaseUrl}${CommunicatorAuthAPIEndpointEnum.CHALLENGE}`,
69
+ headers,
70
+ body,
71
+ });
72
+ }
73
+
51
74
  public async signUp(data: TSignUpByMobileRequestData) {
52
75
  const headers = this.getHeaders();
53
76
  const body = JSON.stringify({
@@ -77,6 +100,60 @@ export class Communicator {
77
100
  });
78
101
  }
79
102
 
103
+ public listenSSEAuth(data: TSetupSSERequestData) {
104
+ if (this.sseAuth) {
105
+ this.closeSSEAuth();
106
+ }
107
+
108
+ const params: TSignInBySSEResponse = {
109
+ request_id: data.requestId,
110
+ mobile: data.phoneNumber,
111
+ }
112
+ const url = `${this.props.clientAuthSSEBaseUrl}${CommunicatorAuthAPIEndpointEnum.SSE_AUTH}`;
113
+ this.sseAuth = new EventSource(`${url}?${this.processingQueryParams(params)}`, {
114
+ withCredentials: true,
115
+ });
116
+
117
+ if (this.sseAuth) {
118
+ this.sseAuth.addEventListener('open', (event: MessageEvent) => {
119
+ if (this.props.env !== 'prod') {
120
+ console.log('sse open', event);
121
+ }
122
+ if (typeof data.onOpen === "function") {
123
+ data.onOpen();
124
+ }
125
+ });
126
+
127
+ this.sseAuth.addEventListener('message', (event: MessageEvent) => {
128
+ const responseData: TSSEResponseData = jsonParse(event.data || '');
129
+ if (this.props.env !== 'prod') {
130
+ console.log('sse message', event, responseData);
131
+ }
132
+ if (typeof data.onMessage === "function") {
133
+ data.onMessage(responseData);
134
+ }
135
+ });
136
+
137
+ this.sseAuth.addEventListener('error', (event: MessageEvent) => {
138
+ const responseData: TSSEResponseData = jsonParse(event.data || '');
139
+ if (this.props.env !== 'prod') {
140
+ console.log('sse error', event, responseData);
141
+ }
142
+ if (typeof data.onError === "function") {
143
+ data.onError();
144
+ }
145
+ });
146
+ }
147
+ }
148
+
149
+ public closeSSEAuth() {
150
+ if (this.sseAuth) {
151
+ console.log(this.sseAuth.readyState);
152
+ this.sseAuth.close();
153
+ this.sseAuth = null;
154
+ }
155
+ }
156
+
80
157
  public async verifyChallenge(data: TVerifyChallengeRequestData) {
81
158
  const headers = this.getHeaders(data.authToken);
82
159
  const body = JSON.stringify({
@@ -6,6 +6,8 @@ type TDefaultRequestData = {
6
6
  }
7
7
 
8
8
  type TCommunicatorProps = {
9
+ env?: 'local' | 'dev' | 'prod';
9
10
  clientAuthApiBaseUrl: string;
11
+ clientAuthSSEBaseUrl: string;
10
12
  clientFAPIBaseUrl: string;
11
13
  }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * JSON Parse
3
+ * @param {string} data
4
+ * @returns {Object}
5
+ */
6
+ export function jsonParse<ResultType = unknown>(data: string): ResultType {
7
+ let ret = null;
8
+ try {
9
+ ret = JSON.parse(data);
10
+ } catch (e) {
11
+ ret = null;
12
+ }
13
+ return ret;
14
+ }
@@ -1,4 +1,4 @@
1
- type TChallenge = 'email_otp' | 'mobile_otp' | 'pin';
1
+ type TChallenge = 'email_otp' | 'mobile_otp' | 'pin' | 'out_of_band';
2
2
  type TAuditSource = 'Backend' | 'SelfService';
3
3
  type TRegistrationRequestBodySourceDeviceType = 'mobile' | 'regular';
4
4
 
@@ -28,6 +28,12 @@ type TSignInStartRequestBody = {
28
28
  mobile?: string,
29
29
  }
30
30
 
31
+ type TSignInAppRequestBody = {
32
+ type: 'out_of_band',
33
+ value: string,
34
+ request_id: string,
35
+ }
36
+
31
37
  type TAccountMeta = {
32
38
  audit_source_type: TAuditSource,
33
39
  browser_page_resolution: string,
@@ -51,10 +57,22 @@ type TSignInByMobileRequestData = {
51
57
  browserData: TAccountBrowserData,
52
58
  }
53
59
 
60
+ type TSignInByAppRequestData = {
61
+ authToken: string,
62
+ type: 'out_of_band',
63
+ value: string,
64
+ requestId: string,
65
+ }
66
+
54
67
  type TSignInByMobileResponse = {
55
68
  auth_token: string,
56
69
  challenges: TChallenge[],
57
70
  }
71
+
72
+ type TSignInBySSEResponse = {
73
+ request_id: string,
74
+ mobile: string,
75
+ }
58
76
  /* endregion signIn */
59
77
 
60
78
  /* region signUp */
@@ -77,6 +95,25 @@ type TSetupChallengeRequestData = {
77
95
  authToken: string,
78
96
  }
79
97
 
98
+ type TSetupSSERequestData = {
99
+ requestId: string,
100
+ phoneNumber: string,
101
+ onOpen: () => void,
102
+ onError: () => void,
103
+ onMessage: (responseData: TSSEResponseData) => void,
104
+ }
105
+
106
+ type TSSEResponseData = {
107
+ otp?: string,
108
+ decline_reason?: string,
109
+ message?: string,
110
+ error_code?: number,
111
+ error_text?: string,
112
+ error_context?: {
113
+ next_attempts_after?: string,
114
+ }
115
+ }
116
+
80
117
  type TSetupChallengeResponse = {
81
118
  attempts_left: number,
82
119
  masked_email: string,
@@ -200,13 +237,18 @@ export {
200
237
  TRequestQueryParams,
201
238
  TResponse,
202
239
  TSignInStartRequestBody,
240
+ TSignInAppRequestBody,
203
241
  TAccountMeta,
204
242
  TAccountBrowserData,
205
243
  TSignInByMobileRequestData,
244
+ TSignInByAppRequestData,
206
245
  TSignInByMobileResponse,
246
+ TSignInBySSEResponse,
207
247
  TSignUpStartRequestBody,
208
248
  TSignUpByMobileRequestData,
209
249
  TSetupChallengeRequestData,
250
+ TSetupSSERequestData,
251
+ TSSEResponseData,
210
252
  TSetupChallengeResponse,
211
253
  TVerifyChallengeRequestBody,
212
254
  TVerifyChallengeRequestData,
@@ -135,6 +135,19 @@ Object {
135
135
  }
136
136
  `;
137
137
 
138
+ exports[`Communicator requests signInByApp 1`] = `
139
+ Object {
140
+ "body": "{\\"type\\":\\"out_of_band\\",\\"value\\":\\"123456\\",\\"request_id\\":\\"7ae2cefb-2f3b-4ecf-b478-67486aca2eaf\\"}",
141
+ "headers": Object {
142
+ "Accept": "application/json, text/plain, */*",
143
+ "Authorization": "Bearer _authToken_",
144
+ "Content-Type": "application/json",
145
+ },
146
+ "method": "POST",
147
+ "url": "https://google.com/sign-in/challenge",
148
+ }
149
+ `;
150
+
138
151
  exports[`Communicator requests signUp 1`] = `
139
152
  Object {
140
153
  "body": "{\\"locale\\":\\"en\\",\\"mobile\\":\\"+16234401486\\",\\"timezone_name\\":\\"Europe/Tallin\\"}",
@@ -11,6 +11,7 @@ import {
11
11
  registrationData,
12
12
  setupChallengeData,
13
13
  signInData,
14
+ signInByAppData,
14
15
  signUpData,
15
16
  verifyChallengeData,
16
17
  setStorageValuesData,
@@ -64,6 +65,11 @@ describe('Communicator requests', () => {
64
65
  expect(signInRequestData).toMatchSnapshot();
65
66
  });
66
67
 
68
+ test('signInByApp', async () => {
69
+ const signInRequestData = await communicator.signInByApp(signInByAppData);
70
+ expect(signInRequestData).toMatchSnapshot();
71
+ });
72
+
67
73
  test('signUp', async () => {
68
74
  const signUpRequestData = await communicator.signUp(signUpData);
69
75
  expect(signUpRequestData).toMatchSnapshot();
@@ -3,6 +3,7 @@ import {TAccountBrowserData, TChallenge, TRegistrationRequestBodySourceDeviceTyp
3
3
  const communicatorTestData = {
4
4
  initProps: {
5
5
  clientAuthApiBaseUrl: 'https://google.com/',
6
+ clientAuthSSEBaseUrl: 'https://google.com/',
6
7
  clientFAPIBaseUrl: 'https://google.com/',
7
8
  },
8
9
  browserData: {
@@ -19,6 +20,7 @@ const communicatorTestData = {
19
20
  companyName: '_companyName_',
20
21
  deviceType: 'mobile' as TRegistrationRequestBodySourceDeviceType,
21
22
  deviceId: 'ov4lNmyk7armoC5DMiOpL5hl846kVQgS',
23
+ requestId: '7ae2cefb-2f3b-4ecf-b478-67486aca2eaf',
22
24
  otpCode: '123456',
23
25
  countryCode: 'CAN',
24
26
  authToken: '_authToken_',
@@ -6,6 +6,7 @@ import type {
6
6
  TRegistrationRequestData,
7
7
  TSetupChallengeRequestData,
8
8
  TSignInByMobileRequestData,
9
+ TSignInByAppRequestData,
9
10
  TSignUpByMobileRequestData,
10
11
  TStorageRequestData,
11
12
  TVerifyChallengeRequestData,
@@ -19,6 +20,13 @@ const signInData: TSignInByMobileRequestData = {
19
20
  browserData: communicatorTestData.browserData,
20
21
  };
21
22
 
23
+ const signInByAppData: TSignInByAppRequestData = {
24
+ authToken: communicatorTestData.authToken,
25
+ type: 'out_of_band',
26
+ value: communicatorTestData.otpCode,
27
+ requestId: communicatorTestData.requestId,
28
+ };
29
+
22
30
  const signUpData: TSignUpByMobileRequestData = {
23
31
  locale: communicatorTestData.locale,
24
32
  phoneNumber: communicatorTestData.phoneNumber,
@@ -75,6 +83,7 @@ const getRestrictedCountriesData: TGetRestrictedCountriesData = {
75
83
 
76
84
  export {
77
85
  signInData,
86
+ signInByAppData,
78
87
  signUpData,
79
88
  setupChallengeData,
80
89
  verifyChallengeData,