@emilgroup/customer-sdk-node 1.1.1 → 1.4.0

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.
Files changed (37) hide show
  1. package/.openapi-generator/FILES +7 -0
  2. package/README.md +22 -5
  3. package/api/customers-api.ts +549 -8
  4. package/base.ts +150 -9
  5. package/dist/api/customers-api.d.ts +324 -8
  6. package/dist/api/customers-api.js +398 -8
  7. package/dist/base.d.ts +12 -3
  8. package/dist/base.js +192 -13
  9. package/dist/models/get-customer-response-class.d.ts +24 -0
  10. package/dist/models/get-customer-response-class.js +15 -0
  11. package/dist/models/index.d.ts +7 -0
  12. package/dist/models/index.js +7 -0
  13. package/dist/models/initiate-auth-response-class.d.ts +4 -4
  14. package/dist/models/list-documents-response-class.d.ts +30 -0
  15. package/dist/models/list-documents-response-class.js +15 -0
  16. package/dist/models/request-policy-update-request-dto.d.ts +42 -0
  17. package/dist/models/request-policy-update-request-dto.js +15 -0
  18. package/dist/models/request-policy-update-response-class.d.ts +25 -0
  19. package/dist/models/request-policy-update-response-class.js +15 -0
  20. package/dist/models/respond-to-auth-challenge-class.d.ts +4 -4
  21. package/dist/models/update-account-request-dto.d.ts +96 -0
  22. package/dist/models/update-account-request-dto.js +21 -0
  23. package/dist/models/update-customer-request-dto.d.ts +31 -0
  24. package/dist/models/update-customer-request-dto.js +15 -0
  25. package/dist/models/update-customer-response-class.d.ts +24 -0
  26. package/dist/models/update-customer-response-class.js +15 -0
  27. package/models/get-customer-response-class.ts +30 -0
  28. package/models/index.ts +7 -0
  29. package/models/initiate-auth-response-class.ts +4 -4
  30. package/models/list-documents-response-class.ts +36 -0
  31. package/models/request-policy-update-request-dto.ts +48 -0
  32. package/models/request-policy-update-response-class.ts +31 -0
  33. package/models/respond-to-auth-challenge-class.ts +4 -4
  34. package/models/update-account-request-dto.ts +105 -0
  35. package/models/update-customer-request-dto.ts +37 -0
  36. package/models/update-customer-response-class.ts +30 -0
  37. package/package.json +4 -3
package/base.ts CHANGED
@@ -17,6 +17,7 @@ import { Configuration } from "./configuration";
17
17
  // Some imports not used depending on template conditions
18
18
  // @ts-ignore
19
19
  import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
20
+ import { InitiateAuthResponseClass, RespondToAuthChallengeClass, RespondToAuthChallengeRequestDto } from "./models";
20
21
  import * as fs from 'fs';
21
22
  import * as path from 'path';
22
23
  import * as os from 'os';
@@ -69,6 +70,7 @@ export class BaseAPI {
69
70
  protected configuration: Configuration | undefined;
70
71
  private username?: string;
71
72
  private password?: string;
73
+ private isCustomerToken = false;
72
74
 
73
75
  constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected axios: AxiosInstance = globalAxios) {
74
76
  if (configuration) {
@@ -83,35 +85,169 @@ export class BaseAPI {
83
85
  this.attachInterceptor(axios);
84
86
  }
85
87
 
88
+ async initialize(env: Environment = Environment.Production) {
89
+ this.configuration.basePath = env;
90
+
91
+ await this.loadCredentials();
92
+
93
+ if (this.username) {
94
+ await this.authorize(this.username, this.password);
95
+ this.password = null; // to avoid keeping password loaded in memory.
96
+ }
97
+ }
98
+
99
+ private async loadCredentials() {
100
+ try {
101
+ await this.readConfigFile();
102
+ } catch (error) {
103
+ console.warn(`No credentials file found. Check that ${filePath} exists.`);
104
+ }
105
+
106
+ this.readEnvVariables();
107
+
108
+ if (!this.username) {
109
+ console.info(`No credentials found in credentials file or environment variables. Either provide some or use
110
+ authorize() function.`);
111
+ }
112
+ }
113
+
114
+ private async readConfigFile() {
115
+ const file = await fs.promises.readFile(filePath, 'utf-8');
116
+
117
+ const lines = file.split(os.EOL)
118
+ .filter(Boolean);
119
+
120
+ lines.forEach((line: string) => {
121
+ if (line.startsWith(KEY_USERNAME)) {
122
+ this.username = line.length > KEY_USERNAME.length + 1 ? line.substring(KEY_USERNAME.length + 1) : '';
123
+ } else if (line.startsWith(KEY_PASSWORD)) {
124
+ this.password = line.length > KEY_PASSWORD.length + 1 ? line.substring(KEY_PASSWORD.length + 1) : '';
125
+ }
126
+ });
127
+ }
128
+
129
+ private readEnvVariables(): boolean {
130
+ if (process.env.EMIL_USERNAME) {
131
+ this.username = process.env.EMIL_USERNAME;
132
+ this.password = process.env.EMIL_PASSWORD || '';
133
+
134
+ return true;
135
+ }
136
+
137
+ return false;
138
+ }
139
+
86
140
  selectEnvironment(env: Environment) {
87
141
  this.configuration.basePath = env;
88
142
  }
89
143
 
90
- async authorize(username: string, password: string, tenantSlug: string): Promise<void> {
144
+ async authorize(username: string, password: string): Promise<void> {
91
145
  const options: AxiosRequestConfig = {
92
146
  method: 'POST',
93
- url: `${this.configuration.basePath}/v1/customers/login`,
94
- headers: {'Content-Type': 'application/json'},
147
+ url: `${this.configuration.basePath}/authservice/v1/login`,
148
+ headers: { 'Content-Type': 'application/json' },
95
149
  data: {
96
- username,
150
+ username,
97
151
  password,
98
- tenantSlug,
99
152
  },
100
153
  withCredentials: true,
101
154
  };
102
155
 
103
156
  const response = await globalAxios.request<LoginClass>(options);
104
-
157
+
105
158
  const { data: { accessToken } } = response;
106
159
  const refreshToken = this.extractRefreshToken(response)
107
160
 
108
161
  this.configuration.username = username;
109
- this.configuration.tenantSlug = tenantSlug;
110
162
  this.configuration.accessToken = `Bearer ${accessToken}`;
111
163
  this.configuration.refreshToken = refreshToken;
112
164
  }
113
165
 
114
- async refreshTokenInternal(): Promise<string> {
166
+ async initiateAuthorization(username: string, password: string, tenantSlug: string):
167
+ Promise<AxiosResponse<InitiateAuthResponseClass>> {
168
+ const options: AxiosRequestConfig = {
169
+ method: 'POST',
170
+ url: `${this.configuration.basePath}/v1/customers/auth/initiate`,
171
+ headers: { 'Content-Type': 'application/json' },
172
+ data: {
173
+ authFlow: "USER_PASSWORD_AUTH",
174
+ authParameters: {
175
+ "USERNAME": username,
176
+ "PASSWORD": password,
177
+ },
178
+ tenantSlug,
179
+ },
180
+ withCredentials: true,
181
+ };
182
+
183
+ const response = await globalAxios.request<InitiateAuthResponseClass>(options);
184
+
185
+ if (response.data.authenticationResult) {
186
+ const { accessToken } = response.data.authenticationResult;
187
+ const refreshToken = this.extractRefreshToken(response)
188
+
189
+ this.configuration.username = username;
190
+ this.configuration.tenantSlug = tenantSlug;
191
+ this.configuration.accessToken = `Bearer ${accessToken}`;
192
+ this.configuration.refreshToken = refreshToken;
193
+
194
+ this.isCustomerToken = true;
195
+ }
196
+
197
+ return response;
198
+ }
199
+
200
+ async respondToAuthorizationChallenge(respondToAuthChallengeRequestDto: RespondToAuthChallengeRequestDto):
201
+ Promise<AxiosResponse<RespondToAuthChallengeClass>> {
202
+ const options: AxiosRequestConfig = {
203
+ method: 'POST',
204
+ url: `${this.configuration.basePath}/v1/customers/auth/respond`,
205
+ headers: { 'Content-Type': 'application/json' },
206
+ data: {
207
+ ...respondToAuthChallengeRequestDto,
208
+ },
209
+ withCredentials: true,
210
+ };
211
+
212
+ const response = await globalAxios.request<RespondToAuthChallengeClass>(options);
213
+
214
+ if (response.data.authenticationResult) {
215
+ const { accessToken } = response.data.authenticationResult;
216
+ const refreshToken = this.extractRefreshToken(response)
217
+
218
+ this.configuration.accessToken = `Bearer ${accessToken}`;
219
+ this.configuration.refreshToken = refreshToken;
220
+
221
+ this.isCustomerToken = true;
222
+ }
223
+
224
+ return response;
225
+ }
226
+
227
+ async refreshTokenTenant(): Promise<string> {
228
+ const { username, refreshToken } = this.configuration;
229
+
230
+ if (!username || !refreshToken) {
231
+ return '';
232
+ }
233
+
234
+ const options: AxiosRequestConfig = {
235
+ method: 'POST',
236
+ url: `${this.configuration.basePath}/authservice/v1/refresh-token`,
237
+ headers: {
238
+ 'Content-Type': 'application/json',
239
+ Cookie: refreshToken,
240
+ },
241
+ data: { username: username },
242
+ withCredentials: true,
243
+ };
244
+
245
+ const { data: { accessToken } } = await globalAxios.request<LoginClass>(options);
246
+
247
+ return accessToken;
248
+ }
249
+
250
+ async refreshTokenCustomer(): Promise<string> {
115
251
  const { username, refreshToken, tenantSlug } = this.configuration;
116
252
 
117
253
  if (!username || !refreshToken) {
@@ -156,7 +292,12 @@ export class BaseAPI {
156
292
  if (err.response.status === 401 && !originalConfig._retry) {
157
293
  originalConfig._retry = true;
158
294
  try {
159
- const tokenString = await this.refreshTokenInternal();
295
+ let tokenString: string;
296
+ if (this.isCustomerToken) {
297
+ tokenString = await this.refreshTokenCustomer();
298
+ } else {
299
+ tokenString = await this.refreshTokenTenant();
300
+ }
160
301
  const accessToken = `Bearer ${tokenString}`;
161
302
 
162
303
  const localVarHeaderParameter = {} as any;