@or-sdk/authorizer 0.24.14-beta.944.0 → 0.24.14-beta.976.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 (44) hide show
  1. package/dist/cjs/Basic/BasicAuth.js +2 -2
  2. package/dist/cjs/Basic/BasicAuth.js.map +1 -1
  3. package/dist/cjs/Basic/BasicCollection.js +2 -6
  4. package/dist/cjs/Basic/BasicCollection.js.map +1 -1
  5. package/dist/cjs/OAuth/OAuth.js +49 -75
  6. package/dist/cjs/OAuth/OAuth.js.map +1 -1
  7. package/dist/cjs/OAuth/OAuthCollection.js +4 -8
  8. package/dist/cjs/OAuth/OAuthCollection.js.map +1 -1
  9. package/dist/cjs/OAuth/types.js.map +1 -1
  10. package/dist/cjs/Token/TokenAuth.js +2 -2
  11. package/dist/cjs/Token/TokenAuth.js.map +1 -1
  12. package/dist/cjs/Token/TokenCollection.js +2 -6
  13. package/dist/cjs/Token/TokenCollection.js.map +1 -1
  14. package/dist/esm/Basic/BasicAuth.js +2 -2
  15. package/dist/esm/Basic/BasicAuth.js.map +1 -1
  16. package/dist/esm/Basic/BasicCollection.js +2 -5
  17. package/dist/esm/Basic/BasicCollection.js.map +1 -1
  18. package/dist/esm/OAuth/OAuth.js +35 -57
  19. package/dist/esm/OAuth/OAuth.js.map +1 -1
  20. package/dist/esm/OAuth/OAuthCollection.js +4 -7
  21. package/dist/esm/OAuth/OAuthCollection.js.map +1 -1
  22. package/dist/esm/OAuth/types.js.map +1 -1
  23. package/dist/esm/Token/TokenAuth.js +2 -2
  24. package/dist/esm/Token/TokenAuth.js.map +1 -1
  25. package/dist/esm/Token/TokenCollection.js +2 -5
  26. package/dist/esm/Token/TokenCollection.js.map +1 -1
  27. package/dist/types/Basic/BasicCollection.d.ts +1 -2
  28. package/dist/types/Basic/BasicCollection.d.ts.map +1 -1
  29. package/dist/types/OAuth/OAuth.d.ts +1 -1
  30. package/dist/types/OAuth/OAuth.d.ts.map +1 -1
  31. package/dist/types/OAuth/OAuthCollection.d.ts +2 -3
  32. package/dist/types/OAuth/OAuthCollection.d.ts.map +1 -1
  33. package/dist/types/OAuth/types.d.ts +0 -5
  34. package/dist/types/OAuth/types.d.ts.map +1 -1
  35. package/dist/types/Token/TokenCollection.d.ts +1 -2
  36. package/dist/types/Token/TokenCollection.d.ts.map +1 -1
  37. package/package.json +4 -6
  38. package/src/Basic/BasicAuth.ts +2 -3
  39. package/src/Basic/BasicCollection.ts +4 -5
  40. package/src/OAuth/OAuth.ts +51 -105
  41. package/src/OAuth/OAuthCollection.ts +8 -9
  42. package/src/OAuth/types.ts +2 -27
  43. package/src/Token/TokenAuth.ts +2 -2
  44. package/src/Token/TokenCollection.ts +4 -5
@@ -1,7 +1,5 @@
1
1
  import { KeyValueStorage } from '@or-sdk/key-value-storage';
2
- import { Providers } from '@or-sdk/providers';
3
- import { EVENT_MANAGER_SERVICE_KEY } from '@or-sdk/event-manager';
4
- import { Discovery } from '@or-sdk/discovery';
2
+ import { EventManager } from '@or-sdk/event-manager';
5
3
  import { timeout } from '@or-sdk/base';
6
4
 
7
5
  import { v4 as uuidv4 } from 'uuid';
@@ -35,26 +33,11 @@ export class OAuth {
35
33
  private readonly serviceName: string;
36
34
  private readonly keyValueCollection: string;
37
35
  private readonly keyValueStorage: KeyValueStorage;
38
- private readonly providers: Providers;
36
+ private readonly eventManager: EventManager;
39
37
 
40
38
  constructor(params: OAuthConfig) {
41
- const {
42
- authKey,
43
- discoveryUrl,
44
- eventManagerUrl,
45
- keyValueCollection,
46
- providersAccountId,
47
- serviceName,
48
- token,
49
- } = params;
50
-
51
- if (!(authKey && serviceName && authKey)) {
52
- throw new Error('Invalit OAuth params passed.');
53
- }
54
-
55
- if (!discoveryUrl && !(eventManagerUrl && providersAccountId)) {
56
- throw new Error('OAuth require Discovery URL or both Event Manager URL and Providers Account ID');
57
- }
39
+ const { token, discoveryUrl, authKey, serviceName, keyValueCollection } =
40
+ params;
58
41
 
59
42
  this.authKey = authKey;
60
43
  this.serviceName = serviceName;
@@ -65,18 +48,17 @@ export class OAuth {
65
48
  discoveryUrl,
66
49
  });
67
50
 
68
- this.providers = new Providers({
51
+ this.eventManager = new EventManager({
69
52
  token,
70
53
  discoveryUrl,
71
- eventManagerUrl,
72
- providersAccountId,
54
+ requestAccountId: true,
55
+ requestProvidersAccountId: true,
73
56
  });
74
57
  }
75
58
 
76
59
  /**
77
60
  * Creates a new auth record in specified collection with the given config.
78
- * @returns Object with OAuth instance connected to created auth and authorizerUrl
79
- * to redirect the user to for completing authorization
61
+ * @returns Object with OAuth instance connected to created auth and authorizerUrl to redirect the user to for completing authorization
80
62
  *
81
63
  * ```typescript
82
64
  * const {
@@ -88,12 +70,10 @@ export class OAuth {
88
70
  * serviceName: '__authorization_service_test_service',
89
71
  * authName: 'my-auth-name',
90
72
  * appId: 'my-app-id',
91
- * keyValueCollection: 'custom_collection_name' // Pass this if you using custom name for key-value collection
92
- * // that differs from serviceName
73
+ * keyValueCollection: 'custom_collection_name' // Pass this if you using custom name for key-value collection that differs from serviceName
93
74
  * });
94
75
  *
95
- * // if you want to use returned instance, you must call a method that returns a promise
96
- * // that will be resolved when the user completes authorization process
76
+ * // if you want to use returned instance, you must call a method that returns a promise that will be resolved when the user completes authorization process
97
77
  * // if not, you can omit this step
98
78
  * try {
99
79
  * await instance.waitForCompletion()
@@ -105,58 +85,19 @@ export class OAuth {
105
85
  static async create(
106
86
  params: CreateOAuthConfig
107
87
  ): Promise<CreateOAuthResult> {
108
-
109
- if (!(
110
- params.serviceName &&
111
- params.authName &&
112
- params.token &&
113
- params.appId
114
- ) || !(
115
- params.discoveryUrl ||
116
- (
117
- params.accountId &&
118
- params.providersAccountId &&
119
- params.eventManagerUrl
120
- )
121
- )) {
122
- throw new Error('Invalid OAuth config passed');
88
+ if (
89
+ !(
90
+ params.serviceName &&
91
+ params.authName &&
92
+ params.discoveryUrl &&
93
+ params.token &&
94
+ params.appId
95
+ )
96
+ ) {
97
+ throw new Error('Invalid config passed');
123
98
  }
124
99
 
125
- const {
126
- appId,
127
- authName,
128
- discoveryUrl,
129
- scope,
130
- serviceName,
131
- token,
132
- } = params;
133
-
134
- let {
135
- accountId,
136
- eventManagerUrl,
137
- providersAccountId,
138
- } = params;
139
-
140
-
141
- // If AccountID, EventManagerURL or Providers Account ID are not available, fetch them from Discovery
142
- if (discoveryUrl && !(accountId && eventManagerUrl && providersAccountId)) {
143
- const discovery = new Discovery({
144
- token,
145
- discoveryUrl,
146
- });
147
-
148
- if (!accountId) {
149
- accountId = await discovery.getCurrentAccountId();
150
- }
151
-
152
- if (!eventManagerUrl) {
153
- eventManagerUrl = await discovery.getServiceUrl(EVENT_MANAGER_SERVICE_KEY);
154
- }
155
-
156
- if (!providersAccountId) {
157
- providersAccountId = await discovery.getProvidersAccountId();
158
- }
159
- }
100
+ const { serviceName, authName, discoveryUrl, token, appId, scope } = params;
160
101
 
161
102
  const keyValueCollection = params.keyValueCollection || serviceName;
162
103
 
@@ -165,24 +106,27 @@ export class OAuth {
165
106
  discoveryUrl,
166
107
  });
167
108
 
168
- const providers = new Providers({
109
+ const eventManager = new EventManager({
169
110
  token,
170
111
  discoveryUrl,
171
- eventManagerUrl,
172
- providersAccountId,
112
+ requestAccountId: true,
113
+ requestProvidersAccountId: true,
173
114
  });
174
115
 
175
- const services = await providers.makeRequest<{
116
+ await eventManager.init();
117
+
118
+ const serviceDefinitionProviderRoute = `http/${eventManager.providersAccountId}${SERVICE_PROVIDER_PATH}`;
119
+
120
+ const services = await eventManager.makeRequest<{
176
121
  [key: string]: ServiceDefinitionConfig;
177
122
  }>({
178
123
  method: 'GET',
179
- route: SERVICE_PROVIDER_PATH,
124
+ route: serviceDefinitionProviderRoute,
180
125
  params: {
181
126
  type: 'list',
182
127
  },
183
128
  });
184
129
 
185
-
186
130
  const currentServiceData = services[serviceName];
187
131
 
188
132
  const apps = await keyValueStorage.getValueByKey(
@@ -190,7 +134,6 @@ export class OAuth {
190
134
  '__authorizer_apps'
191
135
  );
192
136
 
193
- // eslint-disable-next-line
194
137
  const currentApp: OAuthApp = (apps.value as any).find(
195
138
  (app: { label: string; value: OAuthApp; }) => app.value.appId === appId
196
139
  ).value;
@@ -202,14 +145,14 @@ export class OAuth {
202
145
  );
203
146
 
204
147
  const id = uuidv4();
205
- const authKey = createAuthKey(id, authName, keyValueCollection, accountId || null);
148
+ const authKey = createAuthKey(id, authName, keyValueCollection, eventManager.currentAccountId);
206
149
 
207
150
  const additionalBodyData = {};
208
151
  const additionalHeaders = {};
209
152
 
210
- const redirectProviderUrl = `${eventManagerUrl}/http/${providersAccountId}${OAUTH_REDIRECT_PROVIDER_PATH}`;
153
+ const emUrl = eventManager.serviceUrl;
154
+ const redirectProviderUrl = `${emUrl}/http/${eventManager.providersAccountId}${OAUTH_REDIRECT_PROVIDER_PATH}`;
211
155
 
212
- // eslint-disable-next-line
213
156
  const authConfigs: any = {
214
157
  ...additionalBodyData,
215
158
  grant_type: 'authorization_code',
@@ -238,7 +181,7 @@ export class OAuth {
238
181
  serviceConfigName: serviceName,
239
182
  name: authName,
240
183
  displayServiceName: serviceDefinition.displayServiceName,
241
- accountId: accountId,
184
+ accountId: eventManager.currentAccountId,
242
185
  };
243
186
 
244
187
  const authDataExpire = Date.now() + TEMPORARY_DATA_EXPIRATION_TIME;
@@ -270,11 +213,9 @@ export class OAuth {
270
213
  const authorizeUrl = authUrl.href;
271
214
 
272
215
  const newOAuth = new OAuth({
216
+ serviceName,
273
217
  authKey,
274
218
  discoveryUrl,
275
- eventManagerUrl,
276
- providersAccountId,
277
- serviceName,
278
219
  token,
279
220
  });
280
221
 
@@ -310,12 +251,15 @@ export class OAuth {
310
251
  }
311
252
 
312
253
  private async getServiceDefinition(currentApp: OAuthApp): Promise<ServiceDefinition> {
254
+ await this.eventManager.init();
255
+
256
+ const serviceDefinitionProviderRoute = `http/${this.eventManager.providersAccountId}${SERVICE_PROVIDER_PATH}`;
313
257
 
314
- const services = await this.providers.makeRequest<{
258
+ const services = await this.eventManager.makeRequest<{
315
259
  [key: string]: ServiceDefinitionConfig;
316
260
  }>({
317
261
  method: 'GET',
318
- route: SERVICE_PROVIDER_PATH,
262
+ route: serviceDefinitionProviderRoute,
319
263
  params: {
320
264
  type: 'list',
321
265
  },
@@ -355,7 +299,7 @@ export class OAuth {
355
299
  while (spentTime < pollTimeout) {
356
300
  const record = await this.keyValueStorage.getValueByKey(
357
301
  this.keyValueCollection,
358
- encodeURIComponent(this.authKey)
302
+ this.authKey
359
303
  );
360
304
 
361
305
  if (record.value) {
@@ -393,7 +337,7 @@ export class OAuth {
393
337
 
394
338
  const { value } = await this.keyValueStorage.getValueByKey(
395
339
  this.serviceName,
396
- encodeURIComponent(this.authKey)
340
+ this.authKey
397
341
  );
398
342
 
399
343
  if (!value) {
@@ -407,14 +351,16 @@ export class OAuth {
407
351
  return authData;
408
352
  }
409
353
 
410
- if (!isExpired(authData.created_at, authData.expires_in)) {
354
+ const expired = isExpired(authData.created_at, authData.expires_in);
355
+
356
+ if (!expired) {
411
357
  return authData;
412
358
  }
413
359
 
414
360
  await this.refresh();
415
361
 
416
362
  const { value: refreshedAuthData } =
417
- await this.keyValueStorage.getValueByKey(this.serviceName, encodeURIComponent(this.authKey));
363
+ await this.keyValueStorage.getValueByKey(this.serviceName, this.authKey);
418
364
 
419
365
  return refreshedAuthData as OAuthData;
420
366
  }
@@ -425,7 +371,7 @@ export class OAuth {
425
371
  public async refresh(): Promise<void> {
426
372
  const { value } = await this.keyValueStorage.getValueByKey(
427
373
  this.serviceName,
428
- encodeURIComponent(this.authKey)
374
+ this.authKey
429
375
  );
430
376
 
431
377
  if (!value) {
@@ -446,9 +392,9 @@ export class OAuth {
446
392
 
447
393
  const serviceDefinition = await this.getServiceDefinition(currentApp);
448
394
 
449
- await this.providers.makeRequest({
395
+ await this.eventManager.makeRequest({
450
396
  method: 'POST',
451
- route: OAUTH_REDIRECT_PROVIDER_PATH,
397
+ route: new URL(authData.redirect_uri).pathname,
452
398
  data: {
453
399
  refreshTokenUrl: authData.refreshUri,
454
400
  sendDataType: serviceDefinition.requestDataType,
@@ -473,7 +419,7 @@ export class OAuth {
473
419
 
474
420
  const { value } = await this.keyValueStorage.getValueByKey(
475
421
  this.serviceName,
476
- encodeURIComponent(this.authKey)
422
+ this.authKey
477
423
  );
478
424
 
479
425
  if (!value) {
@@ -505,7 +451,7 @@ export class OAuth {
505
451
  public async delete(): Promise<void> {
506
452
  await this.keyValueStorage.deleteKey(
507
453
  this.serviceName,
508
- encodeURIComponent(this.authKey)
454
+ this.authKey
509
455
  );
510
456
 
511
457
  this.status = AuthStatus.DELETED;
@@ -1,4 +1,4 @@
1
- import { Token, List, makeList } from '@or-sdk/base';
1
+ import { Token } from '@or-sdk/base';
2
2
  import { KeyValueStorage } from '@or-sdk/key-value-storage';
3
3
  import { OAuth } from './OAuth';
4
4
 
@@ -27,25 +27,25 @@ export class OAuthCollection {
27
27
  /**
28
28
  * Returns array of auth keys from this collection
29
29
  */
30
- public async listAuthorizations(): Promise<List<string>> {
30
+ public async listAuthorizations(): Promise<string[]> {
31
31
  const { items: records } = await this.keyValueStorage.listKeys(
32
32
  this.keyValueCollection
33
33
  );
34
34
 
35
- return makeList<string>(records.map((record) => record.key).filter(key => key.includes('::oauth')));
35
+ return records.map((record) => record.key);
36
36
  }
37
37
 
38
38
  /**
39
39
  * Returns array of OAuth applications for this service
40
40
  */
41
- public async listOAuthApps(): Promise<List<OAuthApp>> {
41
+ public async listOAuthApps(): Promise<OAuthApp[]> {
42
42
  const appsStorageRecord = await this.keyValueStorage.getValueByKey(
43
43
  this.keyValueCollection,
44
44
  '__authorizer_apps'
45
45
  );
46
46
 
47
47
  if (!appsStorageRecord.value) {
48
- return makeList<OAuthApp>([]);
48
+ return [];
49
49
  }
50
50
 
51
51
  const appsRecords = appsStorageRecord.value as Array<{
@@ -53,20 +53,19 @@ export class OAuthCollection {
53
53
  value: Omit<OAuthApp, 'name'>;
54
54
  }>;
55
55
 
56
- return makeList<OAuthApp>(appsRecords.map((record) => ({
56
+ return appsRecords.map((record) => ({
57
57
  name: record.label,
58
58
  ...record.value,
59
- })));
59
+ }));
60
60
  }
61
61
 
62
62
  /**
63
63
  * Returns OAuth instance for given key
64
64
  */
65
65
  public async getAuthorization(key: string): Promise<OAuth> {
66
- if (!key.includes('::oauth')) throw new Error('The OAuth authorization key should contain "oauth" type specifier.');
67
66
  const record = await this.keyValueStorage.getValueByKey(
68
67
  this.keyValueCollection,
69
- encodeURIComponent(key)
68
+ key
70
69
  );
71
70
  if (!record.value) {
72
71
  throw new Error('This authorization does not exist');
@@ -10,8 +10,6 @@ export type OAuthConfig = {
10
10
  serviceName: string;
11
11
  keyValueCollection?: string;
12
12
  authKey: string;
13
- eventManagerUrl?: string;
14
- providersAccountId?: string;
15
13
  };
16
14
 
17
15
  export type OAuthData = {
@@ -31,8 +29,7 @@ export type OAuthData = {
31
29
 
32
30
  export type CreateOAuthConfig = {
33
31
  /**
34
- * Name of OAuth service definition.
35
- * If service definition is not found in global library, it will be taken from local library
32
+ * Name of OAuth service definition. If service definition is not found in global library, it will be taken from local library
36
33
  */
37
34
  serviceName: string;
38
35
 
@@ -58,31 +55,10 @@ export type CreateOAuthConfig = {
58
55
  */
59
56
  token: Token;
60
57
 
61
- /*
62
- * URL of Discovery API
63
- */
64
58
  discoveryUrl: string;
65
-
66
- /*
67
- * URL of Event manager API
68
- */
69
- eventManagerUrl?: string;
70
-
71
- /*
72
- * Account ID of current account
73
- */
74
- accountId?: string;
75
-
76
- /*
77
- * Account ID of provider account
78
- */
79
- providersAccountId?: string;
80
59
  };
81
60
 
82
- export type CreateOAuthInCollectionConfig = Omit<CreateOAuthConfig, 'discoveryUrl' |
83
- 'serviceName' |
84
- 'token' |
85
- 'keyValueCollection'>;
61
+ export type CreateOAuthInCollectionConfig = Omit<CreateOAuthConfig, 'discoveryUrl' | 'serviceName' | 'token' | 'keyValueCollection'>;
86
62
 
87
63
  export type CreateOAuthResult = {
88
64
  authorizeUrl: string;
@@ -140,7 +116,6 @@ export type ServiceDefinitionConfig = {
140
116
  scopeType: ScopeType;
141
117
  authRequestAdditionalParams: string;
142
118
  codeExchangeRequestAdditionalParams: string;
143
- // eslint-disable-next-line
144
119
  environments?: Record<string, any>;
145
120
  authLinkAdditionalParams?: Record<string, string>[];
146
121
  displayServiceName?: string;
@@ -89,7 +89,7 @@ export class TokenAuth {
89
89
  * Returns authorization data
90
90
  */
91
91
  public async getAuthData(): Promise<TokenAuthData> {
92
- const { value } = await this.keyValueStorage.getValueByKey(this.serviceName, encodeURIComponent(this.authKey));
92
+ const { value } = await this.keyValueStorage.getValueByKey(this.serviceName, this.authKey);
93
93
  return value as TokenAuthData;
94
94
  }
95
95
 
@@ -97,6 +97,6 @@ export class TokenAuth {
97
97
  * Deletes authorization from collection
98
98
  */
99
99
  public async delete(): Promise<void> {
100
- await this.keyValueStorage.deleteKey(this.serviceName, encodeURIComponent(this.authKey));
100
+ await this.keyValueStorage.deleteKey(this.serviceName, this.authKey);
101
101
  }
102
102
  }
@@ -1,4 +1,4 @@
1
- import { Token, makeList, List } from '@or-sdk/base';
1
+ import { Token } from '@or-sdk/base';
2
2
  import { KeyValueStorage } from '@or-sdk/key-value-storage';
3
3
  import { TokenAuth } from './TokenAuth';
4
4
 
@@ -28,18 +28,17 @@ export class TokenCollection {
28
28
  /**
29
29
  * Returns array of auth keys from this collection
30
30
  */
31
- public async listAuthorizations(): Promise<List<string>> {
31
+ public async listAuthorizations(): Promise<string[]> {
32
32
  const { items: records } = await this.keyValueStorage.listKeys(this.serviceName);
33
33
 
34
- return makeList<string>(records.map((record) => record.key).filter(key => key.includes('::token')));
34
+ return records.map((record) => record.key);
35
35
  }
36
36
 
37
37
  /**
38
38
  * Returns TokenAuth instance for given key
39
39
  */
40
40
  public async getAuthorization(key: string): Promise<TokenAuth> {
41
- if (!key.includes('::token')) throw new Error('The Token authorization key should contain "token" type specifier.');
42
- const record = await this.keyValueStorage.getValueByKey(this.serviceName, encodeURIComponent(key));
41
+ const record = await this.keyValueStorage.getValueByKey(this.serviceName, key);
43
42
  if (!record.value) {
44
43
  throw new Error('Authorization does not exist');
45
44
  }