@or-sdk/authorizer 0.25.0-beta.647.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 +341 -0
- package/README.md +12 -32
- package/dist/cjs/Basic/BasicCollection.js +261 -28
- package/dist/cjs/Basic/BasicCollection.js.map +1 -1
- package/dist/cjs/Basic/index.js +21 -0
- package/dist/cjs/Basic/index.js.map +1 -0
- 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 +115 -132
- package/dist/cjs/OAuth/OAuth.js.map +1 -1
- package/dist/cjs/OAuth/index.js +21 -0
- package/dist/cjs/OAuth/index.js.map +1 -0
- 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/OAuthCollection/OAuthCollection.js +312 -0
- 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 +238 -27
- package/dist/cjs/Token/TokenCollection.js.map +1 -1
- package/dist/cjs/Token/index.js +21 -0
- package/dist/cjs/Token/index.js.map +1 -0
- 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 +9 -13
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/Basic/BasicCollection.js +204 -33
- package/dist/esm/Basic/BasicCollection.js.map +1 -1
- package/dist/esm/Basic/index.js +3 -0
- package/dist/esm/Basic/index.js.map +1 -0
- 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 +189 -211
- package/dist/esm/OAuth/OAuth.js.map +1 -1
- package/dist/esm/OAuth/index.js +3 -0
- package/dist/esm/OAuth/index.js.map +1 -0
- package/dist/esm/OAuth/types.js.map +1 -1
- package/dist/esm/OAuth/utils/ServiceDefinition.js +0 -17
- 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 +181 -32
- package/dist/esm/Token/TokenCollection.js.map +1 -1
- package/dist/esm/Token/index.js +3 -0
- package/dist/esm/Token/index.js.map +1 -0
- 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 +4 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/types/Basic/BasicCollection.d.ts +20 -8
- package/dist/types/Basic/BasicCollection.d.ts.map +1 -1
- package/dist/types/Basic/index.d.ts +3 -0
- package/dist/types/Basic/index.d.ts.map +1 -0
- package/dist/types/Basic/types.d.ts +23 -12
- 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 +9 -6
- package/dist/types/OAuth/OAuth.d.ts.map +1 -1
- package/dist/types/OAuth/index.d.ts +3 -0
- package/dist/types/OAuth/index.d.ts.map +1 -0
- package/dist/types/OAuth/types.d.ts +102 -18
- 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 +12 -8
- package/dist/types/Token/TokenCollection.d.ts.map +1 -1
- package/dist/types/Token/index.d.ts +3 -0
- package/dist/types/Token/index.d.ts.map +1 -0
- package/dist/types/Token/types.d.ts +21 -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 +4 -6
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +47 -8
- package/src/Basic/BasicCollection.ts +295 -32
- package/src/Basic/index.ts +3 -0
- package/src/Basic/types.ts +20 -15
- package/src/Basic/utils/createAuthKey.ts +4 -0
- package/src/OAuth/OAuth.ts +148 -174
- package/src/OAuth/index.ts +3 -0
- package/src/OAuth/types.ts +198 -13
- 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 +259 -31
- package/src/Token/index.ts +3 -0
- package/src/Token/types.ts +17 -0
- package/src/Token/utils/createAuthKey.ts +4 -0
- package/src/constants.ts +1 -0
- package/src/index.ts +4 -8
- package/tsconfig.esm.json +1 -1
- package/dist/cjs/Basic/BasicAuth.js +0 -162
- package/dist/cjs/Basic/BasicAuth.js.map +0 -1
- package/dist/cjs/OAuth/OAuthCollection.js +0 -138
- package/dist/cjs/OAuth/OAuthCollection.js.map +0 -1
- package/dist/cjs/Token/TokenAuth.js +0 -140
- package/dist/cjs/Token/TokenAuth.js.map +0 -1
- package/dist/esm/Basic/BasicAuth.js +0 -77
- package/dist/esm/Basic/BasicAuth.js.map +0 -1
- package/dist/esm/OAuth/OAuthCollection.js +0 -60
- package/dist/esm/OAuth/OAuthCollection.js.map +0 -1
- package/dist/esm/Token/TokenAuth.js +0 -59
- package/dist/esm/Token/TokenAuth.js.map +0 -1
- package/dist/types/Basic/BasicAuth.d.ts +0 -20
- package/dist/types/Basic/BasicAuth.d.ts.map +0 -1
- package/dist/types/OAuth/OAuthCollection.d.ts +0 -15
- package/dist/types/OAuth/OAuthCollection.d.ts.map +0 -1
- package/dist/types/Token/TokenAuth.d.ts +0 -12
- package/dist/types/Token/TokenAuth.d.ts.map +0 -1
- package/src/Basic/BasicAuth.ts +0 -129
- package/src/OAuth/OAuthCollection.ts +0 -118
- package/src/Token/TokenAuth.ts +0 -102
package/src/OAuth/OAuth.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
2
|
-
import {
|
|
2
|
+
import { Providers } from '@or-sdk/providers';
|
|
3
3
|
import { timeout } from '@or-sdk/base';
|
|
4
4
|
|
|
5
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
-
|
|
7
5
|
import {
|
|
8
6
|
OAuthConfig,
|
|
9
7
|
OAuthData,
|
|
@@ -11,54 +9,116 @@ import {
|
|
|
11
9
|
CreateOAuthConfig,
|
|
12
10
|
CreateOAuthResult,
|
|
13
11
|
ServiceDefinitionConfig,
|
|
12
|
+
OAuthDynamicCollection,
|
|
13
|
+
OAuthInitData,
|
|
14
|
+
StepContext,
|
|
14
15
|
} from './types';
|
|
15
16
|
|
|
16
17
|
import {
|
|
17
18
|
SERVICE_PROVIDER_PATH,
|
|
18
19
|
OAUTH_REDIRECT_PROVIDER_PATH,
|
|
19
|
-
PREDEFINED_APP,
|
|
20
|
-
TEMPORARY_DATA_EXPIRATION_TIME,
|
|
21
20
|
AuthStatus,
|
|
22
21
|
} from '../constants';
|
|
23
22
|
|
|
24
|
-
import { formatScope } from './utils/formatScope';
|
|
25
23
|
import { isExpired } from './utils/isExpired';
|
|
26
24
|
import { ServiceDefinition } from './utils/ServiceDefinition';
|
|
27
|
-
import
|
|
25
|
+
import OAuthCreator from './utils/createOAuthHelper';
|
|
28
26
|
|
|
29
27
|
export class OAuth {
|
|
30
28
|
private status = AuthStatus.READY;
|
|
31
29
|
|
|
32
|
-
private
|
|
33
|
-
private
|
|
34
|
-
private
|
|
30
|
+
private authKey: string;
|
|
31
|
+
private serviceName: string;
|
|
32
|
+
private keyValueCollection: string;
|
|
33
|
+
private dynamicCollection: string | undefined;
|
|
34
|
+
private authName: string | undefined;
|
|
35
35
|
private readonly keyValueStorage: KeyValueStorage;
|
|
36
|
-
private readonly
|
|
36
|
+
private readonly providers: Providers;
|
|
37
37
|
|
|
38
38
|
constructor(params: OAuthConfig) {
|
|
39
|
-
const {
|
|
40
|
-
|
|
39
|
+
const {
|
|
40
|
+
accountId,
|
|
41
|
+
authKey,
|
|
42
|
+
authName,
|
|
43
|
+
discoveryUrl,
|
|
44
|
+
eventManagerUrl,
|
|
45
|
+
keyValueCollection,
|
|
46
|
+
providersAccountId,
|
|
47
|
+
dynamicCollection,
|
|
48
|
+
serviceName,
|
|
49
|
+
token,
|
|
50
|
+
} = params;
|
|
51
|
+
|
|
52
|
+
if (!(authKey && serviceName)) {
|
|
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
|
+
}
|
|
59
|
+
|
|
60
|
+
if (dynamicCollection && !authName) {
|
|
61
|
+
throw new Error('Dynamic authorization require Authorization name.');
|
|
62
|
+
}
|
|
41
63
|
|
|
42
64
|
this.authKey = authKey;
|
|
65
|
+
this.authName = authName;
|
|
43
66
|
this.serviceName = serviceName;
|
|
67
|
+
this.dynamicCollection = dynamicCollection;
|
|
44
68
|
this.keyValueCollection = keyValueCollection || serviceName;
|
|
45
69
|
|
|
46
70
|
this.keyValueStorage = new KeyValueStorage({
|
|
47
71
|
token,
|
|
48
72
|
discoveryUrl,
|
|
73
|
+
accountId,
|
|
49
74
|
});
|
|
50
75
|
|
|
51
|
-
this.
|
|
76
|
+
this.providers = new Providers({
|
|
52
77
|
token,
|
|
53
78
|
discoveryUrl,
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
eventManagerUrl,
|
|
80
|
+
providersAccountId,
|
|
56
81
|
});
|
|
57
82
|
}
|
|
58
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
|
+
|
|
59
118
|
/**
|
|
60
119
|
* Creates a new auth record in specified collection with the given config.
|
|
61
|
-
* @returns Object with OAuth instance connected to created auth and authorizerUrl
|
|
120
|
+
* @returns Object with OAuth instance connected to created auth and authorizerUrl
|
|
121
|
+
* to redirect the user to for completing authorization
|
|
62
122
|
*
|
|
63
123
|
* ```typescript
|
|
64
124
|
* const {
|
|
@@ -66,14 +126,32 @@ export class OAuth {
|
|
|
66
126
|
* instance // new instance
|
|
67
127
|
* } = await OAuth.create({
|
|
68
128
|
* token: 'my-account-token-string',
|
|
69
|
-
* discoveryUrl: 'discovery.
|
|
129
|
+
* discoveryUrl: 'https://discovery.qa.api.onereach.ai/',
|
|
70
130
|
* serviceName: '__authorization_service_test_service',
|
|
71
131
|
* authName: 'my-auth-name',
|
|
72
132
|
* appId: 'my-app-id',
|
|
73
|
-
*
|
|
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.
|
|
74
151
|
* });
|
|
75
152
|
*
|
|
76
|
-
* // if you want to use returned instance, you must call a method that returns a promise
|
|
153
|
+
* // if you want to use returned instance, you must call a method that returns a promise
|
|
154
|
+
* // that will be resolved when the user completes authorization process
|
|
77
155
|
* // if not, you can omit this step
|
|
78
156
|
* try {
|
|
79
157
|
* await instance.waitForCompletion()
|
|
@@ -85,139 +163,10 @@ export class OAuth {
|
|
|
85
163
|
static async create(
|
|
86
164
|
params: CreateOAuthConfig
|
|
87
165
|
): Promise<CreateOAuthResult> {
|
|
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');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const { serviceName, authName, discoveryUrl, token, appId, scope } = params;
|
|
101
|
-
|
|
102
|
-
const keyValueCollection = params.keyValueCollection || serviceName;
|
|
103
|
-
|
|
104
|
-
const keyValueStorage = new KeyValueStorage({
|
|
105
|
-
token,
|
|
106
|
-
discoveryUrl,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
const eventManager = new EventManager({
|
|
110
|
-
token,
|
|
111
|
-
discoveryUrl,
|
|
112
|
-
requestAccountId: true,
|
|
113
|
-
requestProvidersAccountId: true,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
await eventManager.init();
|
|
117
|
-
|
|
118
|
-
const serviceDefinitionProviderRoute = `http/${eventManager.providersAccountId}${SERVICE_PROVIDER_PATH}`;
|
|
119
|
-
|
|
120
|
-
const services = await eventManager.makeRequest<{
|
|
121
|
-
[key: string]: ServiceDefinitionConfig;
|
|
122
|
-
}>({
|
|
123
|
-
method: 'GET',
|
|
124
|
-
route: serviceDefinitionProviderRoute,
|
|
125
|
-
params: {
|
|
126
|
-
type: 'list',
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const currentServiceData = services[serviceName];
|
|
131
|
-
|
|
132
|
-
const apps = await keyValueStorage.getValueByKey(
|
|
133
|
-
keyValueCollection,
|
|
134
|
-
'__authorizer_apps'
|
|
135
|
-
);
|
|
136
166
|
|
|
137
|
-
const
|
|
138
|
-
(app: { label: string; value: OAuthApp; }) => app.value.appId === appId
|
|
139
|
-
).value;
|
|
167
|
+
const { authorizeUrl, oAuthParams } = await (new OAuthCreator(params)).getOAuthParams();
|
|
140
168
|
|
|
141
|
-
const
|
|
142
|
-
currentServiceData,
|
|
143
|
-
currentApp.authLinkParams,
|
|
144
|
-
currentApp.environment
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
const id = uuidv4();
|
|
148
|
-
const authKey = createAuthKey(id, authName, keyValueCollection, eventManager.currentAccountId);
|
|
149
|
-
|
|
150
|
-
const additionalBodyData = {};
|
|
151
|
-
const additionalHeaders = {};
|
|
152
|
-
|
|
153
|
-
const emUrl = eventManager.serviceUrl;
|
|
154
|
-
const redirectProviderUrl = `${emUrl}/http/${eventManager.providersAccountId}${OAUTH_REDIRECT_PROVIDER_PATH}`;
|
|
155
|
-
|
|
156
|
-
const authConfigs: any = {
|
|
157
|
-
...additionalBodyData,
|
|
158
|
-
grant_type: 'authorization_code',
|
|
159
|
-
redirect_uri: redirectProviderUrl,
|
|
160
|
-
appId,
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
if (scope) {
|
|
164
|
-
const formattedScope = formatScope(scope, serviceDefinition.scopeType);
|
|
165
|
-
authConfigs.scope = formattedScope;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const configs = {
|
|
169
|
-
[serviceDefinition.requestDataType]: authConfigs,
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
const tempAuthData = {
|
|
173
|
-
expiresInDefaultValue: serviceDefinition.expiresInDefaultValue,
|
|
174
|
-
urlToExchangeToken: serviceDefinition.exchangeTokenUri,
|
|
175
|
-
refreshUri: serviceDefinition.refreshUri,
|
|
176
|
-
additionalHeaders,
|
|
177
|
-
configs,
|
|
178
|
-
isCustomApp: appId !== PREDEFINED_APP,
|
|
179
|
-
requestDataType: serviceDefinition.requestDataType,
|
|
180
|
-
service: keyValueCollection,
|
|
181
|
-
serviceConfigName: serviceName,
|
|
182
|
-
name: authName,
|
|
183
|
-
displayServiceName: serviceDefinition.displayServiceName,
|
|
184
|
-
accountId: eventManager.currentAccountId,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const authDataExpire = Date.now() + TEMPORARY_DATA_EXPIRATION_TIME;
|
|
188
|
-
await keyValueStorage.setValueByKey(
|
|
189
|
-
'__authorizer_temp-uuid',
|
|
190
|
-
id,
|
|
191
|
-
tempAuthData,
|
|
192
|
-
authDataExpire
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
const authUrl = new URL(serviceDefinition.authorizeUri);
|
|
196
|
-
const additionalParams: { [key: string]: string; } = JSON.parse(
|
|
197
|
-
serviceDefinition.authRequestAdditionalParams
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
Object.entries(additionalParams.queryParams).forEach(([key, value]) => {
|
|
201
|
-
authUrl.searchParams.append(key, value);
|
|
202
|
-
});
|
|
203
|
-
authUrl.searchParams.append('response_type', 'code');
|
|
204
|
-
authUrl.searchParams.append('client_id', currentApp.clientId);
|
|
205
|
-
authUrl.searchParams.append('redirect_uri', redirectProviderUrl);
|
|
206
|
-
authUrl.searchParams.append('state', authKey);
|
|
207
|
-
|
|
208
|
-
if (scope) {
|
|
209
|
-
const formattedScope = formatScope(scope, serviceDefinition.scopeType);
|
|
210
|
-
authUrl.searchParams.append('scope', formattedScope);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const authorizeUrl = authUrl.href;
|
|
214
|
-
|
|
215
|
-
const newOAuth = new OAuth({
|
|
216
|
-
serviceName,
|
|
217
|
-
authKey,
|
|
218
|
-
discoveryUrl,
|
|
219
|
-
token,
|
|
220
|
-
});
|
|
169
|
+
const newOAuth = new OAuth(oAuthParams);
|
|
221
170
|
|
|
222
171
|
newOAuth.status = AuthStatus.PENDING;
|
|
223
172
|
|
|
@@ -251,15 +200,12 @@ export class OAuth {
|
|
|
251
200
|
}
|
|
252
201
|
|
|
253
202
|
private async getServiceDefinition(currentApp: OAuthApp): Promise<ServiceDefinition> {
|
|
254
|
-
await this.eventManager.init();
|
|
255
|
-
|
|
256
|
-
const serviceDefinitionProviderRoute = `http/${this.eventManager.providersAccountId}${SERVICE_PROVIDER_PATH}`;
|
|
257
203
|
|
|
258
|
-
const services = await this.
|
|
204
|
+
const services = await this.providers.makeRequest<{
|
|
259
205
|
[key: string]: ServiceDefinitionConfig;
|
|
260
206
|
}>({
|
|
261
207
|
method: 'GET',
|
|
262
|
-
route:
|
|
208
|
+
route: SERVICE_PROVIDER_PATH,
|
|
263
209
|
params: {
|
|
264
210
|
type: 'list',
|
|
265
211
|
},
|
|
@@ -299,7 +245,7 @@ export class OAuth {
|
|
|
299
245
|
while (spentTime < pollTimeout) {
|
|
300
246
|
const record = await this.keyValueStorage.getValueByKey(
|
|
301
247
|
this.keyValueCollection,
|
|
302
|
-
this.authKey
|
|
248
|
+
encodeURIComponent(this.authKey)
|
|
303
249
|
);
|
|
304
250
|
|
|
305
251
|
if (record.value) {
|
|
@@ -330,14 +276,27 @@ export class OAuth {
|
|
|
330
276
|
* const authData = await oAuthInstance.getAuthData()
|
|
331
277
|
* ```
|
|
332
278
|
*/
|
|
333
|
-
public async getAuthData(): Promise<OAuthData> {
|
|
334
|
-
if (
|
|
335
|
-
|
|
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;
|
|
336
295
|
}
|
|
337
296
|
|
|
338
297
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
339
|
-
this.
|
|
340
|
-
this.authKey
|
|
298
|
+
this.keyValueCollection,
|
|
299
|
+
encodeURIComponent(this.authKey)
|
|
341
300
|
);
|
|
342
301
|
|
|
343
302
|
if (!value) {
|
|
@@ -351,16 +310,14 @@ export class OAuth {
|
|
|
351
310
|
return authData;
|
|
352
311
|
}
|
|
353
312
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
if (!expired) {
|
|
313
|
+
if (!isExpired(authData.created_at, authData.expires_in)) {
|
|
357
314
|
return authData;
|
|
358
315
|
}
|
|
359
316
|
|
|
360
317
|
await this.refresh();
|
|
361
318
|
|
|
362
319
|
const { value: refreshedAuthData } =
|
|
363
|
-
await this.keyValueStorage.getValueByKey(this.
|
|
320
|
+
await this.keyValueStorage.getValueByKey(this.keyValueCollection, encodeURIComponent(this.authKey));
|
|
364
321
|
|
|
365
322
|
return refreshedAuthData as OAuthData;
|
|
366
323
|
}
|
|
@@ -370,8 +327,8 @@ export class OAuth {
|
|
|
370
327
|
*/
|
|
371
328
|
public async refresh(): Promise<void> {
|
|
372
329
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
373
|
-
this.
|
|
374
|
-
this.authKey
|
|
330
|
+
this.keyValueCollection,
|
|
331
|
+
encodeURIComponent(this.authKey)
|
|
375
332
|
);
|
|
376
333
|
|
|
377
334
|
if (!value) {
|
|
@@ -392,9 +349,9 @@ export class OAuth {
|
|
|
392
349
|
|
|
393
350
|
const serviceDefinition = await this.getServiceDefinition(currentApp);
|
|
394
351
|
|
|
395
|
-
await this.
|
|
352
|
+
await this.providers.makeRequest({
|
|
396
353
|
method: 'POST',
|
|
397
|
-
route:
|
|
354
|
+
route: OAUTH_REDIRECT_PROVIDER_PATH,
|
|
398
355
|
data: {
|
|
399
356
|
refreshTokenUrl: authData.refreshUri,
|
|
400
357
|
sendDataType: serviceDefinition.requestDataType,
|
|
@@ -418,8 +375,8 @@ export class OAuth {
|
|
|
418
375
|
}
|
|
419
376
|
|
|
420
377
|
const { value } = await this.keyValueStorage.getValueByKey(
|
|
421
|
-
this.
|
|
422
|
-
this.authKey
|
|
378
|
+
this.keyValueCollection,
|
|
379
|
+
encodeURIComponent(this.authKey)
|
|
423
380
|
);
|
|
424
381
|
|
|
425
382
|
if (!value) {
|
|
@@ -450,10 +407,27 @@ export class OAuth {
|
|
|
450
407
|
*/
|
|
451
408
|
public async delete(): Promise<void> {
|
|
452
409
|
await this.keyValueStorage.deleteKey(
|
|
453
|
-
this.
|
|
454
|
-
this.authKey
|
|
410
|
+
this.keyValueCollection,
|
|
411
|
+
encodeURIComponent(this.authKey)
|
|
455
412
|
);
|
|
456
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
|
+
|
|
457
431
|
this.status = AuthStatus.DELETED;
|
|
458
432
|
}
|
|
459
433
|
}
|