@or-sdk/authorizer 0.25.0-beta.990.0 → 0.25.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.
- package/CHANGELOG.md +317 -0
- package/README.md +12 -32
- package/dist/cjs/Basic/BasicCollection.js +166 -26
- package/dist/cjs/Basic/BasicCollection.js.map +1 -1
- package/dist/cjs/Basic/utils/createAuthKey.js +4 -0
- package/dist/cjs/Basic/utils/createAuthKey.js.map +1 -1
- package/dist/cjs/OAuth/OAuth.js +92 -138
- package/dist/cjs/OAuth/OAuth.js.map +1 -1
- package/dist/cjs/OAuth/index.js +1 -3
- package/dist/cjs/OAuth/index.js.map +1 -1
- package/dist/cjs/OAuth/types.js +1 -1
- package/dist/cjs/OAuth/types.js.map +1 -1
- package/dist/cjs/OAuth/utils/ServiceDefinition.js.map +1 -1
- package/dist/cjs/OAuth/utils/createAuthKey.js +4 -0
- package/dist/cjs/OAuth/utils/createAuthKey.js.map +1 -1
- package/dist/cjs/OAuth/utils/createOAuthHelper.js +379 -0
- package/dist/cjs/OAuth/utils/createOAuthHelper.js.map +1 -0
- package/dist/cjs/OAuth/utils/formatScope.js.map +1 -1
- package/dist/cjs/{OAuth → OAuthCollection}/OAuthCollection.js +132 -28
- package/dist/cjs/OAuthCollection/OAuthCollection.js.map +1 -0
- package/dist/cjs/OAuthCollection/index.js +21 -0
- package/dist/cjs/OAuthCollection/index.js.map +1 -0
- package/dist/cjs/Token/TokenCollection.js +166 -21
- package/dist/cjs/Token/TokenCollection.js.map +1 -1
- package/dist/cjs/Token/utils/createAuthKey.js +4 -0
- package/dist/cjs/Token/utils/createAuthKey.js.map +1 -1
- package/dist/cjs/constants.js +3 -2
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/Basic/BasicCollection.js +115 -17
- package/dist/esm/Basic/BasicCollection.js.map +1 -1
- package/dist/esm/Basic/utils/createAuthKey.js +3 -0
- package/dist/esm/Basic/utils/createAuthKey.js.map +1 -1
- package/dist/esm/OAuth/OAuth.js +56 -115
- package/dist/esm/OAuth/OAuth.js.map +1 -1
- package/dist/esm/OAuth/index.js +0 -1
- package/dist/esm/OAuth/index.js.map +1 -1
- package/dist/esm/OAuth/types.js.map +1 -1
- package/dist/esm/OAuth/utils/ServiceDefinition.js.map +1 -1
- package/dist/esm/OAuth/utils/createAuthKey.js +3 -0
- package/dist/esm/OAuth/utils/createAuthKey.js.map +1 -1
- package/dist/esm/OAuth/utils/createOAuthHelper.js +262 -0
- package/dist/esm/OAuth/utils/createOAuthHelper.js.map +1 -0
- package/dist/esm/OAuth/utils/formatScope.js.map +1 -1
- package/dist/esm/OAuthCollection/OAuthCollection.js +190 -0
- package/dist/esm/OAuthCollection/OAuthCollection.js.map +1 -0
- package/dist/esm/OAuthCollection/index.js +3 -0
- package/dist/esm/OAuthCollection/index.js.map +1 -0
- package/dist/esm/Token/TokenCollection.js +115 -12
- package/dist/esm/Token/TokenCollection.js.map +1 -1
- package/dist/esm/Token/utils/createAuthKey.js +3 -0
- package/dist/esm/Token/utils/createAuthKey.js.map +1 -1
- package/dist/esm/constants.js +1 -0
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/Basic/BasicCollection.d.ts +6 -4
- package/dist/types/Basic/BasicCollection.d.ts.map +1 -1
- package/dist/types/Basic/types.d.ts +20 -11
- package/dist/types/Basic/types.d.ts.map +1 -1
- package/dist/types/Basic/utils/createAuthKey.d.ts +1 -0
- package/dist/types/Basic/utils/createAuthKey.d.ts.map +1 -1
- package/dist/types/OAuth/OAuth.d.ts +8 -5
- package/dist/types/OAuth/OAuth.d.ts.map +1 -1
- package/dist/types/OAuth/index.d.ts +0 -1
- package/dist/types/OAuth/index.d.ts.map +1 -1
- package/dist/types/OAuth/types.d.ts +95 -19
- package/dist/types/OAuth/types.d.ts.map +1 -1
- package/dist/types/OAuth/utils/createAuthKey.d.ts +1 -0
- package/dist/types/OAuth/utils/createAuthKey.d.ts.map +1 -1
- package/dist/types/OAuth/utils/createOAuthHelper.d.ts +33 -0
- package/dist/types/OAuth/utils/createOAuthHelper.d.ts.map +1 -0
- package/dist/types/OAuthCollection/OAuthCollection.d.ts +27 -0
- package/dist/types/OAuthCollection/OAuthCollection.d.ts.map +1 -0
- package/dist/types/OAuthCollection/index.d.ts +3 -0
- package/dist/types/OAuthCollection/index.d.ts.map +1 -0
- package/dist/types/Token/TokenCollection.d.ts +5 -2
- package/dist/types/Token/TokenCollection.d.ts.map +1 -1
- package/dist/types/Token/types.d.ts +19 -5
- package/dist/types/Token/types.d.ts.map +1 -1
- package/dist/types/Token/utils/createAuthKey.d.ts +1 -0
- package/dist/types/Token/utils/createAuthKey.d.ts.map +1 -1
- package/dist/types/constants.d.ts +1 -0
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +32 -26
- package/src/Basic/BasicCollection.ts +170 -18
- package/src/Basic/types.ts +17 -14
- package/src/Basic/utils/createAuthKey.ts +4 -0
- package/src/OAuth/OAuth.ts +111 -195
- package/src/OAuth/index.ts +0 -1
- package/src/OAuth/types.ts +171 -15
- package/src/OAuth/utils/createAuthKey.ts +8 -0
- package/src/OAuth/utils/createOAuthHelper.ts +374 -0
- package/src/OAuthCollection/OAuthCollection.ts +348 -0
- package/src/OAuthCollection/index.ts +3 -0
- package/src/Token/TokenCollection.ts +174 -16
- package/src/Token/types.ts +15 -0
- package/src/Token/utils/createAuthKey.ts +4 -0
- package/src/constants.ts +1 -0
- package/src/index.ts +2 -1
- package/dist/cjs/OAuth/OAuthCollection.js.map +0 -1
- package/dist/esm/OAuth/OAuthCollection.js +0 -120
- package/dist/esm/OAuth/OAuthCollection.js.map +0 -1
- package/dist/types/OAuth/OAuthCollection.d.ts +0 -21
- package/dist/types/OAuth/OAuthCollection.d.ts.map +0 -1
- package/src/OAuth/OAuthCollection.ts +0 -206
package/src/OAuth/OAuth.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
2
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';
|
|
5
3
|
import { timeout } from '@or-sdk/base';
|
|
6
4
|
|
|
7
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
8
|
-
|
|
9
5
|
import {
|
|
10
6
|
OAuthConfig,
|
|
11
7
|
OAuthData,
|
|
@@ -13,27 +9,29 @@ import {
|
|
|
13
9
|
CreateOAuthConfig,
|
|
14
10
|
CreateOAuthResult,
|
|
15
11
|
ServiceDefinitionConfig,
|
|
12
|
+
OAuthDynamicCollection,
|
|
13
|
+
OAuthInitData,
|
|
14
|
+
StepContext,
|
|
16
15
|
} from './types';
|
|
17
16
|
|
|
18
17
|
import {
|
|
19
18
|
SERVICE_PROVIDER_PATH,
|
|
20
19
|
OAUTH_REDIRECT_PROVIDER_PATH,
|
|
21
|
-
PREDEFINED_APP,
|
|
22
|
-
TEMPORARY_DATA_EXPIRATION_TIME,
|
|
23
20
|
AuthStatus,
|
|
24
21
|
} from '../constants';
|
|
25
22
|
|
|
26
|
-
import { formatScope } from './utils/formatScope';
|
|
27
23
|
import { isExpired } from './utils/isExpired';
|
|
28
24
|
import { ServiceDefinition } from './utils/ServiceDefinition';
|
|
29
|
-
import
|
|
25
|
+
import OAuthCreator from './utils/createOAuthHelper';
|
|
30
26
|
|
|
31
27
|
export class OAuth {
|
|
32
28
|
private status = AuthStatus.READY;
|
|
33
29
|
|
|
34
|
-
private
|
|
35
|
-
private
|
|
36
|
-
private
|
|
30
|
+
private authKey: string;
|
|
31
|
+
private serviceName: string;
|
|
32
|
+
private keyValueCollection: string;
|
|
33
|
+
private dynamicCollection: string | undefined;
|
|
34
|
+
private authName: string | undefined;
|
|
37
35
|
private readonly keyValueStorage: KeyValueStorage;
|
|
38
36
|
private readonly providers: Providers;
|
|
39
37
|
|
|
@@ -41,15 +39,17 @@ export class OAuth {
|
|
|
41
39
|
const {
|
|
42
40
|
accountId,
|
|
43
41
|
authKey,
|
|
42
|
+
authName,
|
|
44
43
|
discoveryUrl,
|
|
45
44
|
eventManagerUrl,
|
|
46
45
|
keyValueCollection,
|
|
47
46
|
providersAccountId,
|
|
47
|
+
dynamicCollection,
|
|
48
48
|
serviceName,
|
|
49
49
|
token,
|
|
50
50
|
} = params;
|
|
51
51
|
|
|
52
|
-
if (!(authKey && serviceName
|
|
52
|
+
if (!(authKey && serviceName)) {
|
|
53
53
|
throw new Error('Invalit OAuth params passed.');
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -57,8 +57,14 @@ export class OAuth {
|
|
|
57
57
|
throw new Error('OAuth require Discovery URL or both Event Manager URL and Providers Account ID');
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
if (dynamicCollection && !authName) {
|
|
61
|
+
throw new Error('Dynamic authorization require Authorization name.');
|
|
62
|
+
}
|
|
63
|
+
|
|
60
64
|
this.authKey = authKey;
|
|
65
|
+
this.authName = authName;
|
|
61
66
|
this.serviceName = serviceName;
|
|
67
|
+
this.dynamicCollection = dynamicCollection;
|
|
62
68
|
this.keyValueCollection = keyValueCollection || serviceName;
|
|
63
69
|
|
|
64
70
|
this.keyValueStorage = new KeyValueStorage({
|
|
@@ -75,6 +81,40 @@ export class OAuth {
|
|
|
75
81
|
});
|
|
76
82
|
}
|
|
77
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Changes the target authorization of instance
|
|
86
|
+
*/
|
|
87
|
+
public async init(params: OAuthInitData) {
|
|
88
|
+
this.serviceName = params.serviceName || this.serviceName;
|
|
89
|
+
this.authKey = params.authKey || this.authKey;
|
|
90
|
+
this.authName = params.authName || this.authName;
|
|
91
|
+
this.keyValueCollection = params.keyValueCollection || this.keyValueCollection;
|
|
92
|
+
this.dynamicCollection = params.dynamicCollection || this.dynamicCollection;
|
|
93
|
+
|
|
94
|
+
if (this.dynamicCollection && ! this.authName) {
|
|
95
|
+
throw new Error('Dynamic collection requires an authName to initialize');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (params.authName && this.dynamicCollection) {
|
|
99
|
+
const collection = await this.keyValueStorage.getValueByKey(
|
|
100
|
+
'__authorizer_dynamic_collections',
|
|
101
|
+
this.dynamicCollection
|
|
102
|
+
).then(res => res.value) as OAuthDynamicCollection;
|
|
103
|
+
|
|
104
|
+
if (!collection) {
|
|
105
|
+
throw new Error('Dynamic collection is invalid');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this.authKey = collection.authorizations[this.authName as string];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.status = await this.keyValueStorage.getValueByKey(
|
|
112
|
+
this.keyValueCollection,
|
|
113
|
+
this.authKey
|
|
114
|
+
).then(res => res.value ? AuthStatus.READY : AuthStatus.PENDING)
|
|
115
|
+
.catch(e => { throw new Error('Could not request authorization: ' + e.message); });
|
|
116
|
+
}
|
|
117
|
+
|
|
78
118
|
/**
|
|
79
119
|
* Creates a new auth record in specified collection with the given config.
|
|
80
120
|
* @returns Object with OAuth instance connected to created auth and authorizerUrl
|
|
@@ -86,12 +126,28 @@ export class OAuth {
|
|
|
86
126
|
* instance // new instance
|
|
87
127
|
* } = await OAuth.create({
|
|
88
128
|
* token: 'my-account-token-string',
|
|
89
|
-
* discoveryUrl: 'discovery.
|
|
129
|
+
* discoveryUrl: 'https://discovery.qa.api.onereach.ai/',
|
|
90
130
|
* serviceName: '__authorization_service_test_service',
|
|
91
131
|
* authName: 'my-auth-name',
|
|
92
132
|
* appId: 'my-app-id',
|
|
93
|
-
*
|
|
94
|
-
*
|
|
133
|
+
*
|
|
134
|
+
* // Optional params
|
|
135
|
+
* sdkUrl: 'https://sdkapi.qa.api.onereach.ai' // SDK API url. If passed, won't be fetched from discovery.
|
|
136
|
+
* eventManagerUrl: 'https://em.qa.api.onereach.ai'// Event Manager url. If passed, won't be fetched from discovery.
|
|
137
|
+
* providersAccountId: 'providers-account-id' // ID of provider account.
|
|
138
|
+
* // If passed, won't be fetched from discovery.
|
|
139
|
+
* keyValueCollection: 'custom_collection_name' // Pass this if you using custom name for key-value collection
|
|
140
|
+
* // that differs from serviceName.
|
|
141
|
+
* destinationAccount: 'CUSTOM' or 'PROVIDER' // Allows to save authorization data to custom account.
|
|
142
|
+
* customAccountId: 'custom-account-uuid-v4' // Account ID for destinationAccount == "CUSTOM".
|
|
143
|
+
* accountId: 'current-account-ID' // Account ID of current account (see crossAccount).
|
|
144
|
+
* crossAccount: boolean (default: true) // Treat accountId as custom account ID (SUPER-ADMIN only).
|
|
145
|
+
* // If crossAccount=false, account ID is treated
|
|
146
|
+
* // as current account ID.
|
|
147
|
+
* useNextProvider: boolean (default: false) // Use authorizer-next redirect endpoint.
|
|
148
|
+
* userScope: 'user:read' // Scopes for Slack service.
|
|
149
|
+
* useNonce: true // Allows to use Nonce to avoid repetition attacks.
|
|
150
|
+
* authKey: 'old::auth::key' // If passed, will reauthorize an existing authorization.
|
|
95
151
|
* });
|
|
96
152
|
*
|
|
97
153
|
* // if you want to use returned instance, you must call a method that returns a promise
|
|
@@ -108,179 +164,9 @@ export class OAuth {
|
|
|
108
164
|
params: CreateOAuthConfig
|
|
109
165
|
): Promise<CreateOAuthResult> {
|
|
110
166
|
|
|
111
|
-
|
|
112
|
-
params.serviceName &&
|
|
113
|
-
params.authName &&
|
|
114
|
-
params.token &&
|
|
115
|
-
params.appId
|
|
116
|
-
) || !(
|
|
117
|
-
params.discoveryUrl ||
|
|
118
|
-
(
|
|
119
|
-
params.accountId &&
|
|
120
|
-
params.providersAccountId &&
|
|
121
|
-
params.eventManagerUrl
|
|
122
|
-
)
|
|
123
|
-
)) {
|
|
124
|
-
throw new Error('Invalid OAuth config passed');
|
|
125
|
-
}
|
|
126
|
-
|
|
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
|
-
}
|
|
162
|
-
|
|
163
|
-
const keyValueCollection = params.keyValueCollection || serviceName;
|
|
164
|
-
|
|
165
|
-
const keyValueStorage = new KeyValueStorage({
|
|
166
|
-
token,
|
|
167
|
-
discoveryUrl,
|
|
168
|
-
accountId,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const providers = new Providers({
|
|
172
|
-
token,
|
|
173
|
-
discoveryUrl,
|
|
174
|
-
eventManagerUrl,
|
|
175
|
-
providersAccountId,
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
const services = await providers.makeRequest<{
|
|
179
|
-
[key: string]: ServiceDefinitionConfig;
|
|
180
|
-
}>({
|
|
181
|
-
method: 'GET',
|
|
182
|
-
route: SERVICE_PROVIDER_PATH,
|
|
183
|
-
params: {
|
|
184
|
-
type: 'list',
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const currentServiceData = services[serviceName];
|
|
190
|
-
|
|
191
|
-
const apps = await keyValueStorage.getValueByKey(
|
|
192
|
-
keyValueCollection,
|
|
193
|
-
'__authorizer_apps'
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
// eslint-disable-next-line
|
|
197
|
-
const currentApp: OAuthApp = (apps.value as any).find(
|
|
198
|
-
(app: { label: string; value: OAuthApp; }) => app.value.appId === appId
|
|
199
|
-
).value;
|
|
200
|
-
|
|
201
|
-
const serviceDefinition = new ServiceDefinition(
|
|
202
|
-
currentServiceData,
|
|
203
|
-
currentApp.authLinkParams,
|
|
204
|
-
currentApp.environment
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const id = uuidv4();
|
|
208
|
-
const authKey = createAuthKey(id, authName, keyValueCollection, accountId || null);
|
|
209
|
-
|
|
210
|
-
const additionalBodyData = {};
|
|
211
|
-
const additionalHeaders = {};
|
|
212
|
-
|
|
213
|
-
const redirectProviderUrl = `${eventManagerUrl}/http/${providersAccountId}${OAUTH_REDIRECT_PROVIDER_PATH}`;
|
|
214
|
-
|
|
215
|
-
// eslint-disable-next-line
|
|
216
|
-
const authConfigs: any = {
|
|
217
|
-
...additionalBodyData,
|
|
218
|
-
grant_type: 'authorization_code',
|
|
219
|
-
redirect_uri: redirectProviderUrl,
|
|
220
|
-
appId,
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
if (scope) {
|
|
224
|
-
const formattedScope = formatScope(scope, serviceDefinition.scopeType);
|
|
225
|
-
authConfigs.scope = formattedScope;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const configs = {
|
|
229
|
-
[serviceDefinition.requestDataType]: authConfigs,
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const tempAuthData = {
|
|
233
|
-
expiresInDefaultValue: serviceDefinition.expiresInDefaultValue,
|
|
234
|
-
urlToExchangeToken: serviceDefinition.exchangeTokenUri,
|
|
235
|
-
refreshUri: serviceDefinition.refreshUri,
|
|
236
|
-
additionalHeaders,
|
|
237
|
-
configs,
|
|
238
|
-
isCustomApp: appId !== PREDEFINED_APP,
|
|
239
|
-
requestDataType: serviceDefinition.requestDataType,
|
|
240
|
-
service: keyValueCollection,
|
|
241
|
-
serviceConfigName: serviceName,
|
|
242
|
-
name: authName,
|
|
243
|
-
displayServiceName: serviceDefinition.displayServiceName,
|
|
244
|
-
accountId: accountId,
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const authDataExpire = Date.now() + TEMPORARY_DATA_EXPIRATION_TIME;
|
|
248
|
-
await keyValueStorage.setValueByKey(
|
|
249
|
-
'__authorizer_temp-uuid',
|
|
250
|
-
id,
|
|
251
|
-
tempAuthData,
|
|
252
|
-
authDataExpire
|
|
253
|
-
);
|
|
254
|
-
|
|
255
|
-
const authUrl = new URL(serviceDefinition.authorizeUri);
|
|
256
|
-
const additionalParams: { [key: string]: string; } = JSON.parse(
|
|
257
|
-
serviceDefinition.authRequestAdditionalParams
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
Object.entries(additionalParams.queryParams).forEach(([key, value]) => {
|
|
261
|
-
authUrl.searchParams.append(key, value);
|
|
262
|
-
});
|
|
263
|
-
authUrl.searchParams.append('response_type', 'code');
|
|
264
|
-
authUrl.searchParams.append('client_id', currentApp.clientId);
|
|
265
|
-
authUrl.searchParams.append('redirect_uri', redirectProviderUrl);
|
|
266
|
-
authUrl.searchParams.append('state', authKey);
|
|
267
|
-
|
|
268
|
-
if (scope) {
|
|
269
|
-
const formattedScope = formatScope(scope, serviceDefinition.scopeType);
|
|
270
|
-
authUrl.searchParams.append('scope', formattedScope);
|
|
271
|
-
}
|
|
167
|
+
const { authorizeUrl, oAuthParams } = await (new OAuthCreator(params)).getOAuthParams();
|
|
272
168
|
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
const newOAuth = new OAuth({
|
|
276
|
-
accountId,
|
|
277
|
-
authKey,
|
|
278
|
-
discoveryUrl,
|
|
279
|
-
eventManagerUrl,
|
|
280
|
-
providersAccountId,
|
|
281
|
-
serviceName,
|
|
282
|
-
token,
|
|
283
|
-
});
|
|
169
|
+
const newOAuth = new OAuth(oAuthParams);
|
|
284
170
|
|
|
285
171
|
newOAuth.status = AuthStatus.PENDING;
|
|
286
172
|
|
|
@@ -390,13 +276,26 @@ export class OAuth {
|
|
|
390
276
|
* const authData = await oAuthInstance.getAuthData()
|
|
391
277
|
* ```
|
|
392
278
|
*/
|
|
393
|
-
public async getAuthData(): Promise<OAuthData> {
|
|
394
|
-
if (
|
|
395
|
-
|
|
279
|
+
public async getAuthData(stepThis: StepContext): Promise<OAuthData> {
|
|
280
|
+
if (stepThis) {
|
|
281
|
+
if (this.authKey === 'inherited') {
|
|
282
|
+
this.authKey = stepThis.getShared('shared_' + this.keyValueCollection) as string;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const ttl = (new Date(stepThis.session.expirationDate).getTime() - Date.now()) + 24 * 60 * 60 * 1000;
|
|
286
|
+
stepThis.setShared('shared_' + this.keyValueCollection, this.authKey, ttl);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (this.status !== AuthStatus.READY && !await this.keyValueStorage.getValueByKey(
|
|
290
|
+
this.keyValueCollection,
|
|
291
|
+
this.authKey)) {
|
|
292
|
+
throw new Error('This authorization is invalid or not ready');
|
|
293
|
+
} else {
|
|
294
|
+
this.status = AuthStatus.READY;
|
|
396
295
|
}
|
|
397
296
|
|
|
398
297
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
399
|
-
this.
|
|
298
|
+
this.keyValueCollection,
|
|
400
299
|
encodeURIComponent(this.authKey)
|
|
401
300
|
);
|
|
402
301
|
|
|
@@ -418,7 +317,7 @@ export class OAuth {
|
|
|
418
317
|
await this.refresh();
|
|
419
318
|
|
|
420
319
|
const { value: refreshedAuthData } =
|
|
421
|
-
await this.keyValueStorage.getValueByKey(this.
|
|
320
|
+
await this.keyValueStorage.getValueByKey(this.keyValueCollection, encodeURIComponent(this.authKey));
|
|
422
321
|
|
|
423
322
|
return refreshedAuthData as OAuthData;
|
|
424
323
|
}
|
|
@@ -428,7 +327,7 @@ export class OAuth {
|
|
|
428
327
|
*/
|
|
429
328
|
public async refresh(): Promise<void> {
|
|
430
329
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
431
|
-
this.
|
|
330
|
+
this.keyValueCollection,
|
|
432
331
|
encodeURIComponent(this.authKey)
|
|
433
332
|
);
|
|
434
333
|
|
|
@@ -476,7 +375,7 @@ export class OAuth {
|
|
|
476
375
|
}
|
|
477
376
|
|
|
478
377
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
479
|
-
this.
|
|
378
|
+
this.keyValueCollection,
|
|
480
379
|
encodeURIComponent(this.authKey)
|
|
481
380
|
);
|
|
482
381
|
|
|
@@ -508,10 +407,27 @@ export class OAuth {
|
|
|
508
407
|
*/
|
|
509
408
|
public async delete(): Promise<void> {
|
|
510
409
|
await this.keyValueStorage.deleteKey(
|
|
511
|
-
this.
|
|
410
|
+
this.keyValueCollection,
|
|
512
411
|
encodeURIComponent(this.authKey)
|
|
513
412
|
);
|
|
514
413
|
|
|
414
|
+
if (this.dynamicCollection) {
|
|
415
|
+
const { value } = await this.keyValueStorage.getValueByKey(
|
|
416
|
+
'__authorizer_dynamic_collections',
|
|
417
|
+
this.dynamicCollection
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
if (value) {
|
|
421
|
+
const collection = value as OAuthDynamicCollection;
|
|
422
|
+
delete collection.authorizations[this.authName as string];
|
|
423
|
+
|
|
424
|
+
await this.keyValueStorage.setValueByKey(
|
|
425
|
+
'__authorizer_dynamic_collections',
|
|
426
|
+
this.dynamicCollection, collection
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
515
431
|
this.status = AuthStatus.DELETED;
|
|
516
432
|
}
|
|
517
433
|
}
|
package/src/OAuth/index.ts
CHANGED
package/src/OAuth/types.ts
CHANGED
|
@@ -6,13 +6,16 @@ export type OAuthConfig = {
|
|
|
6
6
|
* token or function which return token
|
|
7
7
|
*/
|
|
8
8
|
token: Token;
|
|
9
|
+
authKey: string;
|
|
9
10
|
discoveryUrl: string;
|
|
10
11
|
serviceName: string;
|
|
11
|
-
keyValueCollection?: string;
|
|
12
|
-
authKey: string;
|
|
13
12
|
accountId?: string;
|
|
13
|
+
authName?: string;
|
|
14
|
+
dynamicCollection?: string;
|
|
14
15
|
eventManagerUrl?: string;
|
|
16
|
+
keyValueCollection?: string;
|
|
15
17
|
providersAccountId?: string;
|
|
18
|
+
sdkUrl?: string;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
21
|
export type OAuthData = {
|
|
@@ -30,6 +33,14 @@ export type OAuthData = {
|
|
|
30
33
|
accountId: string;
|
|
31
34
|
};
|
|
32
35
|
|
|
36
|
+
export type OAuthInitData = {
|
|
37
|
+
serviceName: string;
|
|
38
|
+
keyValueCollection: string;
|
|
39
|
+
authKey?: string;
|
|
40
|
+
authName?: string;
|
|
41
|
+
dynamicCollection: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
33
44
|
export type CreateOAuthConfig = {
|
|
34
45
|
/**
|
|
35
46
|
* Name of OAuth service definition.
|
|
@@ -59,25 +70,81 @@ export type CreateOAuthConfig = {
|
|
|
59
70
|
*/
|
|
60
71
|
token: Token;
|
|
61
72
|
|
|
62
|
-
|
|
73
|
+
/**
|
|
63
74
|
* URL of Discovery API
|
|
64
75
|
*/
|
|
65
76
|
discoveryUrl: string;
|
|
66
77
|
|
|
67
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Account where to save the authorization data
|
|
80
|
+
*/
|
|
81
|
+
destinationAccount: 'CURRENT' | 'PROVIDER' | 'CUSTOM';
|
|
82
|
+
|
|
83
|
+
/**
|
|
68
84
|
* URL of Event manager API
|
|
69
85
|
*/
|
|
70
86
|
eventManagerUrl?: string;
|
|
71
87
|
|
|
72
|
-
|
|
88
|
+
/**
|
|
73
89
|
* Account ID of current account
|
|
74
90
|
*/
|
|
75
91
|
accountId?: string;
|
|
76
92
|
|
|
77
|
-
|
|
93
|
+
/**
|
|
78
94
|
* Account ID of provider account
|
|
79
95
|
*/
|
|
80
96
|
providersAccountId?: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Name of the dynamic collection where to store authorization data
|
|
100
|
+
*/
|
|
101
|
+
dynamicCollection?: string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* User scopes (Slack only)
|
|
105
|
+
*/
|
|
106
|
+
userScope?: string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Flag whether nonce should be used
|
|
110
|
+
*/
|
|
111
|
+
useNonce?: boolean;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Custom account ID (if destinationAccount === "CUSTOM")
|
|
115
|
+
*/
|
|
116
|
+
customAccountId?: string;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Url of OneReach SDK api
|
|
120
|
+
*/
|
|
121
|
+
sdkUrl?: string;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Flag to choose if token should be used with non-original accountId (SUPER-ADMIN only)
|
|
125
|
+
*/
|
|
126
|
+
crossAccount?: boolean;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Flag to choose if alternative provider should be used for handling redirect
|
|
130
|
+
*/
|
|
131
|
+
useNextProvider?: boolean;
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Additional headers for code exchange request
|
|
136
|
+
*/
|
|
137
|
+
additionalHeaders?: Record<string, unknown>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Additional body data for code exchange request
|
|
141
|
+
*/
|
|
142
|
+
additionalBodyData?: Record<string, unknown>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Existing authorization key for reauthorization.
|
|
146
|
+
*/
|
|
147
|
+
authKey?: string;
|
|
81
148
|
};
|
|
82
149
|
|
|
83
150
|
export type CreateOAuthInCollectionConfig = Omit<CreateOAuthConfig, 'discoveryUrl' |
|
|
@@ -117,44 +184,133 @@ export type CreateOAuthAppConfig = {
|
|
|
117
184
|
|
|
118
185
|
export type OAuthCollectionConfig = {
|
|
119
186
|
/**
|
|
120
|
-
*
|
|
187
|
+
* Token or function which return token
|
|
121
188
|
*/
|
|
122
189
|
token: Token;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* URL of Discovery API
|
|
193
|
+
*/
|
|
123
194
|
discoveryUrl: string;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Account ID of current account
|
|
198
|
+
*/
|
|
124
199
|
accountId?: string;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Name of OAuth service definition.
|
|
203
|
+
* If service definition is not found in global library, it will be taken from local library
|
|
204
|
+
*/
|
|
125
205
|
serviceName?: string;
|
|
206
|
+
|
|
126
207
|
/**
|
|
127
208
|
* Pass this if your using custom name for key-value collection that differs from serviceName
|
|
128
209
|
*/
|
|
129
210
|
keyValueCollection?: string;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Account ID of provider account
|
|
214
|
+
*/
|
|
215
|
+
providersAccountId?: string;
|
|
216
|
+
|
|
217
|
+
/*
|
|
218
|
+
* URL of Event manager API
|
|
219
|
+
*/
|
|
220
|
+
eventManagerUrl?: string;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Url of OneReach SDK api
|
|
224
|
+
*/
|
|
225
|
+
sdkUrl?: string;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Flag to treat accountId as custom, if false, accountId treated as current (default:true)
|
|
229
|
+
*/
|
|
230
|
+
crossAccount: boolean;
|
|
130
231
|
};
|
|
131
232
|
|
|
132
233
|
export type OAuthCollectionInitConfig = Pick<OAuthCollectionConfig, 'serviceName' | 'keyValueCollection'>;
|
|
133
234
|
|
|
134
235
|
export type ServiceDefinitionConfig = {
|
|
135
|
-
serviceName: string;
|
|
136
|
-
requestDataType: string;
|
|
137
|
-
grantType?: string;
|
|
138
236
|
authorizeUri: string;
|
|
237
|
+
authRequestAdditionalParams: string;
|
|
238
|
+
codeExchangeRequestAdditionalParams: string;
|
|
139
239
|
exchangeTokenUri: string;
|
|
140
|
-
useRefresh: boolean;
|
|
141
|
-
expiresInPropertyName: string;
|
|
142
240
|
expiresInDefaultValue?: number;
|
|
241
|
+
expiresInPropertyName: string;
|
|
242
|
+
grantType?: string;
|
|
143
243
|
refreshUri?: string;
|
|
244
|
+
requestDataType: string;
|
|
144
245
|
scopeType: ScopeType;
|
|
145
|
-
|
|
146
|
-
|
|
246
|
+
serviceName: string;
|
|
247
|
+
useRefresh: boolean;
|
|
147
248
|
// eslint-disable-next-line
|
|
148
249
|
environments?: Record<string, any>;
|
|
149
250
|
authLinkAdditionalParams?: Record<string, string>[];
|
|
150
251
|
displayServiceName?: string;
|
|
151
252
|
};
|
|
152
253
|
|
|
254
|
+
export type OAuthService = {
|
|
255
|
+
additionalFieldsForApp: unknown[];
|
|
256
|
+
addNonceToAuthRequest: boolean;
|
|
257
|
+
allowedScopes: [];
|
|
258
|
+
appHelp: string;
|
|
259
|
+
appTerm: string;
|
|
260
|
+
authLinkAdditionalParams: Record<string, string>[];
|
|
261
|
+
authorizationAndScopeHelp: string;
|
|
262
|
+
authorizeUri: string;
|
|
263
|
+
authRequestAdditionalParams: string;
|
|
264
|
+
codeExchangeRequestAdditionalParams: string;
|
|
265
|
+
defaultEnvironmentName: string;
|
|
266
|
+
disallowedScopes: [];
|
|
267
|
+
displayServiceName: string;
|
|
268
|
+
environments: Record<string, unknown>;
|
|
269
|
+
exchangeTokenUrl: string;
|
|
270
|
+
expiresInDefaultValue: number;
|
|
271
|
+
expiresInPropertyValue: string;
|
|
272
|
+
predefinedApps: [];
|
|
273
|
+
refreshUrl: string;
|
|
274
|
+
requestDataType: 'form' | 'json' | 'formData';
|
|
275
|
+
revokeHttpMethod: '' | 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
276
|
+
revokeUrl: string;
|
|
277
|
+
scopesDocumentationLink: string;
|
|
278
|
+
scopeType: 'JSON_ARRAY' | 'SPACE_DELIMITED' | 'COMMA_DELIMITED' | 'COMMA_DELIMITED_WITHOUT_WHITESPACE';
|
|
279
|
+
serviceName: string;
|
|
280
|
+
useRefresh: boolean;
|
|
281
|
+
grantType?: string;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export type OAuthDynamicCollection = {
|
|
285
|
+
name: string;
|
|
286
|
+
type: string;
|
|
287
|
+
service: string;
|
|
288
|
+
serviceConfigName: string;
|
|
289
|
+
authorizations: {
|
|
290
|
+
[key: string]: string;
|
|
291
|
+
};
|
|
292
|
+
appId?: string;
|
|
293
|
+
scope?: string;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
export type LocalService = {
|
|
297
|
+
key: string;
|
|
298
|
+
value: ServiceDefinitionConfig;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export type StepContext = {
|
|
302
|
+
setShared(key: string, value: unknown, ttl: number): void;
|
|
303
|
+
getShared(key: string): unknown;
|
|
304
|
+
session: {
|
|
305
|
+
expirationDate: number;
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
|
|
153
309
|
export enum ScopeType {
|
|
154
310
|
SPACE_DELIMITED = 'SPACE_DELIMITED',
|
|
155
311
|
COMMA_DELIMITED = 'COMMA_DELIMITED',
|
|
156
312
|
COMMA_DELIMITED_WITHOUT_WHITESPACE = 'COMMA_DELIMITED_WITHOUT_WHITESPACE',
|
|
157
|
-
JSON_ARRAY = 'JSON_ARRAY'
|
|
313
|
+
JSON_ARRAY = 'JSON_ARRAY',
|
|
158
314
|
}
|
|
159
315
|
|
|
160
316
|
|
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
export const createAuthKey = (id: string, authName: string, keyValueCollection: string, currentAccountId: string | null): string => {
|
|
2
2
|
return `${id}::oauth::${authName}::${keyValueCollection}::${currentAccountId}`;
|
|
3
3
|
};
|
|
4
|
+
|
|
5
|
+
export function createDynamicKey(
|
|
6
|
+
id: string,
|
|
7
|
+
dynamicCollection: string,
|
|
8
|
+
serviceName: string,
|
|
9
|
+
currentAccountId: string | null): string {
|
|
10
|
+
return `${id}::oauth-collection::${dynamicCollection} authorization::${serviceName}::${currentAccountId}`;
|
|
11
|
+
}
|