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