@or-sdk/authorizer 0.24.14-beta.971.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.
- 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 +4 -37
- 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
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
2
|
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
3
|
-
import {
|
|
4
|
-
import { createAuthKey } from './utils/createAuthKey';
|
|
5
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
import { BasicAuth } from './BasicAuth';
|
|
6
4
|
|
|
7
5
|
import {
|
|
8
6
|
BasicCollectionConfig,
|
|
@@ -10,95 +8,55 @@ import {
|
|
|
10
8
|
} from './types';
|
|
11
9
|
|
|
12
10
|
export class BasicCollection {
|
|
13
|
-
private serviceName: string;
|
|
11
|
+
private readonly serviceName: string;
|
|
14
12
|
private readonly keyValueStorage: KeyValueStorage;
|
|
13
|
+
private readonly localToken: Token;
|
|
14
|
+
private readonly localDiscoveryUrl: string;
|
|
15
15
|
|
|
16
16
|
constructor(params: BasicCollectionConfig) {
|
|
17
|
-
const { token, discoveryUrl, serviceName
|
|
17
|
+
const { token, discoveryUrl, serviceName } = params;
|
|
18
18
|
|
|
19
|
-
this.
|
|
19
|
+
this.localToken = token;
|
|
20
|
+
this.localDiscoveryUrl = discoveryUrl;
|
|
21
|
+
this.serviceName = serviceName;
|
|
20
22
|
this.keyValueStorage = new KeyValueStorage({
|
|
21
23
|
token,
|
|
22
24
|
discoveryUrl,
|
|
23
|
-
accountId,
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Allows to explicitly (re-)set authorization service name
|
|
30
|
-
*/
|
|
31
|
-
public init(serviceName: string) {
|
|
32
|
-
if (!serviceName) throw new Error('Service name is required.');
|
|
33
|
-
|
|
34
|
-
this.serviceName = serviceName;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Encodes username and password into Basic authorization token
|
|
40
|
-
*/
|
|
41
|
-
static encodeAccessToken(username: string, password: string): string {
|
|
42
|
-
return btoa(`${username}:${password}`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Decodes base64 encoded token and parses it to username and password
|
|
47
|
-
*/
|
|
48
|
-
static decodeAccessToken(token: string): {username: string; password: string;} {
|
|
49
|
-
const [username, password] = atob(token).split(':');
|
|
50
|
-
return {
|
|
51
|
-
username,
|
|
52
|
-
password,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
28
|
/**
|
|
58
29
|
* Returns array of auth keys from this collection
|
|
59
30
|
*/
|
|
60
|
-
public async listAuthorizations(): Promise<
|
|
31
|
+
public async listAuthorizations(): Promise<string[]> {
|
|
61
32
|
const { items: records } = await this.keyValueStorage.listKeys(this.serviceName);
|
|
62
33
|
|
|
63
|
-
return
|
|
34
|
+
return records.map((record) => record.key);
|
|
64
35
|
}
|
|
65
36
|
|
|
66
|
-
|
|
67
37
|
/**
|
|
68
38
|
* Returns BasicAuth instance for given key
|
|
69
39
|
*/
|
|
70
|
-
public async getAuthorization(key: string): Promise<
|
|
71
|
-
|
|
72
|
-
if (!key.includes('::basic')) throw new Error('The basic authorization key should contain "basic" type specifier.');
|
|
73
|
-
|
|
74
|
-
const record = await this.keyValueStorage.getValueByKey(this.serviceName, encodeURIComponent(key));
|
|
75
|
-
|
|
40
|
+
public async getAuthorization(key: string): Promise<BasicAuth> {
|
|
41
|
+
const record = await this.keyValueStorage.getValueByKey(this.serviceName, key);
|
|
76
42
|
if (!record.value) {
|
|
77
|
-
throw new Error('Authorization does not exist
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const authRecord = record.value as Omit<BasicAuthData, 'key'>;
|
|
82
|
-
|
|
83
|
-
if (!authRecord.auth_name || !authRecord.access_token || !authRecord.date_created) {
|
|
84
|
-
throw new Error('Authorization does not exist or invalid.');
|
|
43
|
+
throw new Error('Authorization does not exist');
|
|
85
44
|
}
|
|
86
45
|
|
|
87
|
-
return {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
46
|
+
return new BasicAuth({
|
|
47
|
+
serviceName: this.serviceName,
|
|
48
|
+
authKey: key,
|
|
49
|
+
discoveryUrl: this.localDiscoveryUrl,
|
|
50
|
+
token: this.localToken,
|
|
51
|
+
});
|
|
93
52
|
}
|
|
94
53
|
|
|
95
|
-
|
|
96
54
|
/**
|
|
97
55
|
* Creates a new auth record in current collection with the given config.
|
|
98
56
|
* @returns BasicAuth instance connected to created auth
|
|
99
57
|
*
|
|
100
58
|
* ```typescript
|
|
101
|
-
* const
|
|
59
|
+
* const basicAuthInstance = await basicCollectionInstance.createAuthorization({
|
|
102
60
|
* authName: 'my-auth-name',
|
|
103
61
|
* username: 'user',
|
|
104
62
|
* password: 'pass'
|
|
@@ -107,83 +65,14 @@ export class BasicCollection {
|
|
|
107
65
|
*/
|
|
108
66
|
public async createAuthorization(
|
|
109
67
|
params: CreateBasicAuthInCollectionConfig
|
|
110
|
-
): Promise<
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const id = uuidv4();
|
|
122
|
-
const authKey = createAuthKey(id, authName);
|
|
123
|
-
|
|
124
|
-
const authData = {
|
|
125
|
-
auth_name: authName,
|
|
126
|
-
access_token: BasicCollection.encodeAccessToken(username, password),
|
|
127
|
-
date_created: new Date().toISOString(),
|
|
128
|
-
} as Omit<BasicAuthData, 'key'>;
|
|
129
|
-
|
|
130
|
-
await this.keyValueStorage.setValueByKey(this.serviceName, encodeURIComponent(authKey), authData);
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
...authData,
|
|
134
|
-
key: authKey,
|
|
135
|
-
} as BasicAuthData;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Returns array of auth keys from this collection
|
|
141
|
-
*/
|
|
142
|
-
public async updateAuthorization(
|
|
143
|
-
basicAuth: BasicAuthData,
|
|
144
|
-
newCredentials: { username?: string; password?: string; }
|
|
145
|
-
): Promise<BasicAuthData> {
|
|
146
|
-
if (!basicAuth || !basicAuth.key || !basicAuth.access_token) {
|
|
147
|
-
throw new Error('Authorization is invalid.');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (!this.serviceName) throw new Error('Service name is not defined.');
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
let { username, password } = BasicCollection.decodeAccessToken(basicAuth.access_token);
|
|
154
|
-
|
|
155
|
-
if (newCredentials.username) {
|
|
156
|
-
username = newCredentials.username;
|
|
157
|
-
}
|
|
158
|
-
if (newCredentials.password) {
|
|
159
|
-
password = newCredentials.password;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const access_token = BasicCollection.encodeAccessToken(username, password);
|
|
163
|
-
const response = await this.keyValueStorage.setValueByKey(
|
|
164
|
-
this.serviceName,
|
|
165
|
-
encodeURIComponent(basicAuth.key),
|
|
166
|
-
{
|
|
167
|
-
auth_name: basicAuth.auth_name,
|
|
168
|
-
date_created: basicAuth.date_created,
|
|
169
|
-
access_token,
|
|
170
|
-
}
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
const updatedData = response.value as Omit<BasicAuthData, 'key'>;
|
|
174
|
-
return {
|
|
175
|
-
...updatedData,
|
|
176
|
-
key: basicAuth.key,
|
|
177
|
-
} as BasicAuthData;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Deletes authorization from collection
|
|
183
|
-
*/
|
|
184
|
-
public async deleteAuthorization(authKey: string): Promise<void> {
|
|
185
|
-
if (!this.serviceName) throw new Error('Service name is not defined.');
|
|
186
|
-
await this.keyValueStorage.deleteKey(this.serviceName, encodeURIComponent(authKey));
|
|
68
|
+
): Promise<BasicAuth> {
|
|
69
|
+
return await BasicAuth.create({
|
|
70
|
+
discoveryUrl: this.localDiscoveryUrl,
|
|
71
|
+
token: this.localToken,
|
|
72
|
+
serviceName: this.serviceName,
|
|
73
|
+
authName: params.authName,
|
|
74
|
+
password: params.password,
|
|
75
|
+
username: params.username,
|
|
76
|
+
});
|
|
187
77
|
}
|
|
188
78
|
}
|
|
189
|
-
|
package/src/Basic/types.ts
CHANGED
|
@@ -14,7 +14,6 @@ export type BasicAuthConfig = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export type BasicAuthData = {
|
|
17
|
-
key: string;
|
|
18
17
|
auth_name: string;
|
|
19
18
|
|
|
20
19
|
/**
|
|
@@ -51,6 +50,5 @@ export type BasicCollectionConfig = {
|
|
|
51
50
|
* function which return token
|
|
52
51
|
*/
|
|
53
52
|
discoveryUrl: string;
|
|
54
|
-
|
|
55
|
-
serviceName?: string;
|
|
53
|
+
serviceName: string;
|
|
56
54
|
};
|
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,
|