@or-sdk/authorizer 0.10.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 +157 -0
- package/dist/cjs/Basic/BasicAuth.js.map +1 -0
- package/dist/cjs/Basic/BasicCollection.js +107 -0
- package/dist/cjs/Basic/BasicCollection.js.map +1 -0
- package/dist/cjs/Basic/types.js +3 -0
- package/dist/cjs/Basic/types.js.map +1 -0
- package/dist/cjs/Basic/utils/createAuthKey.js +8 -0
- package/dist/cjs/Basic/utils/createAuthKey.js.map +1 -0
- package/dist/cjs/OAuth/OAuth.js +391 -0
- package/dist/cjs/OAuth/OAuth.js.map +1 -0
- package/dist/cjs/OAuth/OAuthCollection.js +138 -0
- package/dist/cjs/OAuth/OAuthCollection.js.map +1 -0
- package/dist/cjs/OAuth/types.js +11 -0
- package/dist/cjs/OAuth/types.js.map +1 -0
- package/dist/cjs/OAuth/utils/ServiceDefinition.js +175 -0
- package/dist/cjs/OAuth/utils/ServiceDefinition.js.map +1 -0
- package/dist/cjs/OAuth/utils/createAuthKey.js +8 -0
- package/dist/cjs/OAuth/utils/createAuthKey.js.map +1 -0
- package/dist/cjs/OAuth/utils/formatScope.js +20 -0
- package/dist/cjs/OAuth/utils/formatScope.js.map +1 -0
- package/dist/cjs/OAuth/utils/isExpired.js +12 -0
- package/dist/cjs/OAuth/utils/isExpired.js.map +1 -0
- package/dist/cjs/Token/TokenAuth.js +135 -0
- package/dist/cjs/Token/TokenAuth.js.map +1 -0
- package/dist/cjs/Token/TokenCollection.js +106 -0
- package/dist/cjs/Token/TokenCollection.js.map +1 -0
- package/dist/cjs/Token/types.js +3 -0
- package/dist/cjs/Token/types.js.map +1 -0
- package/dist/cjs/Token/utils/createAuthKey.js +8 -0
- package/dist/cjs/Token/utils/createAuthKey.js.map +1 -0
- package/dist/cjs/constants.js +16 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/Basic/BasicAuth.js +86 -0
- package/dist/esm/Basic/BasicAuth.js.map +1 -0
- package/dist/esm/Basic/BasicCollection.js +56 -0
- package/dist/esm/Basic/BasicCollection.js.map +1 -0
- package/dist/esm/Basic/types.js +2 -0
- package/dist/esm/Basic/types.js.map +1 -0
- package/dist/esm/Basic/utils/createAuthKey.js +4 -0
- package/dist/esm/Basic/utils/createAuthKey.js.map +1 -0
- package/dist/esm/OAuth/OAuth.js +258 -0
- package/dist/esm/OAuth/OAuth.js.map +1 -0
- package/dist/esm/OAuth/OAuthCollection.js +69 -0
- package/dist/esm/OAuth/OAuthCollection.js.map +1 -0
- package/dist/esm/OAuth/types.js +8 -0
- package/dist/esm/OAuth/types.js.map +1 -0
- package/dist/esm/OAuth/utils/ServiceDefinition.js +117 -0
- package/dist/esm/OAuth/utils/ServiceDefinition.js.map +1 -0
- package/dist/esm/OAuth/utils/createAuthKey.js +4 -0
- package/dist/esm/OAuth/utils/createAuthKey.js.map +1 -0
- package/dist/esm/OAuth/utils/formatScope.js +16 -0
- package/dist/esm/OAuth/utils/formatScope.js.map +1 -0
- package/dist/esm/OAuth/utils/isExpired.js +8 -0
- package/dist/esm/OAuth/utils/isExpired.js.map +1 -0
- package/dist/esm/Token/TokenAuth.js +64 -0
- package/dist/esm/Token/TokenAuth.js.map +1 -0
- package/dist/esm/Token/TokenCollection.js +55 -0
- package/dist/esm/Token/TokenCollection.js.map +1 -0
- package/dist/esm/Token/types.js +2 -0
- package/dist/esm/Token/types.js.map +1 -0
- package/dist/esm/Token/utils/createAuthKey.js +4 -0
- package/dist/esm/Token/utils/createAuthKey.js.map +1 -0
- package/dist/esm/constants.js +13 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types.js +4 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/types/Basic/BasicAuth.d.ts +19 -0
- package/dist/types/Basic/BasicCollection.d.ts +12 -0
- package/dist/types/Basic/types.d.ts +26 -0
- package/dist/types/Basic/utils/createAuthKey.d.ts +1 -0
- package/dist/types/OAuth/OAuth.d.ts +18 -0
- package/dist/types/OAuth/OAuthCollection.d.ts +14 -0
- package/dist/types/OAuth/types.d.ts +81 -0
- package/dist/types/OAuth/utils/ServiceDefinition.d.ts +36 -0
- package/dist/types/OAuth/utils/createAuthKey.d.ts +1 -0
- package/dist/types/OAuth/utils/formatScope.d.ts +2 -0
- package/dist/types/OAuth/utils/isExpired.d.ts +1 -0
- package/dist/types/Token/TokenAuth.d.ts +11 -0
- package/dist/types/Token/TokenCollection.d.ts +12 -0
- package/dist/types/Token/types.d.ts +25 -0
- package/dist/types/Token/utils/createAuthKey.d.ts +1 -0
- package/dist/types/constants.d.ts +11 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/types.d.ts +3 -0
- package/package.json +33 -0
- package/src/Basic/BasicAuth.ts +147 -0
- package/src/Basic/BasicCollection.ts +97 -0
- package/src/Basic/types.ts +54 -0
- package/src/Basic/utils/createAuthKey.ts +3 -0
- package/src/OAuth/OAuth.ts +480 -0
- package/src/OAuth/OAuthCollection.ts +138 -0
- package/src/OAuth/types.ts +131 -0
- package/src/OAuth/utils/ServiceDefinition.ts +171 -0
- package/src/OAuth/utils/createAuthKey.ts +3 -0
- package/src/OAuth/utils/formatScope.ts +20 -0
- package/src/OAuth/utils/isExpired.ts +7 -0
- package/src/Token/TokenAuth.ts +120 -0
- package/src/Token/TokenCollection.ts +97 -0
- package/src/Token/types.ts +51 -0
- package/src/Token/utils/createAuthKey.ts +3 -0
- package/src/constants.ts +14 -0
- package/src/index.ts +10 -0
- package/src/types.ts +3 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +7 -0
- package/tsconfig.types.json +9 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
2
|
+
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
|
|
5
|
+
import { BasicAuthConfig, BasicAuthData, CreateBasicAuthConfig } from './types';
|
|
6
|
+
|
|
7
|
+
import { createAuthKey } from './utils/createAuthKey';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* OneReach Authorizer service client
|
|
11
|
+
* ## Installation:
|
|
12
|
+
* ```
|
|
13
|
+
* $ npm i @or-sdk/authorizer
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export class BasicAuth {
|
|
17
|
+
private readonly authKey: string;
|
|
18
|
+
private readonly serviceName: string;
|
|
19
|
+
private readonly keyValueStorage: KeyValueStorage;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Connects to existing authorization
|
|
23
|
+
*
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { BasicAuth } from '@or-sdk/authorizer'
|
|
26
|
+
* const basicAuthInstance = new BasicAuth({
|
|
27
|
+
* token: 'my-account-token-string',
|
|
28
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
29
|
+
* serviceName: '__authorization_service_test_service',
|
|
30
|
+
* authKey: '637ac446-1021-475f-9992-3ce7f3ddb637::basic::someAuth::__authorization_service_test_service::02bf4366-f987-49ea-90a6-0d346e13c3c1',
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
constructor(params: BasicAuthConfig) {
|
|
35
|
+
const { token, discoveryUrl, authKey, serviceName } = params;
|
|
36
|
+
|
|
37
|
+
this.authKey = authKey;
|
|
38
|
+
this.serviceName = serviceName;
|
|
39
|
+
this.keyValueStorage = new KeyValueStorage({
|
|
40
|
+
token,
|
|
41
|
+
discoveryUrl,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Encodes username and passoword into Basic authorization token
|
|
47
|
+
*/
|
|
48
|
+
static encodeAccessToken(username: string, password: string): string {
|
|
49
|
+
return btoa(`${username}:${password}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Decodes base64 encoded token and parses it to username and password
|
|
54
|
+
*/
|
|
55
|
+
static decodeAccessToken(token: string): {username: string; password: string;} {
|
|
56
|
+
const [username, password] = atob(token).split(':');
|
|
57
|
+
return {
|
|
58
|
+
username,
|
|
59
|
+
password,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new auth record in specified collection with the given config.
|
|
65
|
+
* @returns BasicAuth instance connected to created auth
|
|
66
|
+
*
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const basicAuthInstance = await BasicAuth.create({
|
|
69
|
+
* token: 'my-account-token-string',
|
|
70
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
71
|
+
* serviceName: '__authorization_service_test_service',
|
|
72
|
+
* authName: 'my-auth-name',
|
|
73
|
+
* username: 'user',
|
|
74
|
+
* password: 'pass'
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
static async create(params: CreateBasicAuthConfig): Promise<BasicAuth> {
|
|
79
|
+
if (!(params.serviceName && params.authName && params.username && params.password && params.discoveryUrl && params.token)) {
|
|
80
|
+
throw new Error('Invalid config passed');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const { serviceName, authName, username, password, discoveryUrl, token } = params;
|
|
84
|
+
|
|
85
|
+
const keyValueStorage = new KeyValueStorage({
|
|
86
|
+
token,
|
|
87
|
+
discoveryUrl,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const id = uuidv4();
|
|
91
|
+
const authKey = createAuthKey(id, authName);
|
|
92
|
+
|
|
93
|
+
const authData = {
|
|
94
|
+
auth_name: authName,
|
|
95
|
+
access_token: BasicAuth.encodeAccessToken(username, password),
|
|
96
|
+
date_created: new Date().toISOString(),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
await keyValueStorage.setValueByKey(serviceName, authKey, authData);
|
|
100
|
+
|
|
101
|
+
return new BasicAuth({ serviceName,
|
|
102
|
+
authKey,
|
|
103
|
+
discoveryUrl,
|
|
104
|
+
token });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Updates username or password
|
|
109
|
+
*/
|
|
110
|
+
public async update(newCredentials: { username?: string; password?: string; }): Promise<BasicAuthData> {
|
|
111
|
+
const data = await this.getAuthData();
|
|
112
|
+
let { username, password } = BasicAuth.decodeAccessToken(data.access_token);
|
|
113
|
+
|
|
114
|
+
if (newCredentials.username) {
|
|
115
|
+
username = newCredentials.username;
|
|
116
|
+
}
|
|
117
|
+
if (newCredentials.password) {
|
|
118
|
+
password = newCredentials.password;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const updatedAccessToken = BasicAuth.encodeAccessToken(username, password);
|
|
122
|
+
const updatedData = {
|
|
123
|
+
...data,
|
|
124
|
+
access_token: updatedAccessToken,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const response = await this.keyValueStorage.setValueByKey(this.serviceName, this.authKey, updatedData);
|
|
128
|
+
|
|
129
|
+
const updatedRecord = response.value;
|
|
130
|
+
return updatedRecord as BasicAuthData;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns authorization data
|
|
135
|
+
*/
|
|
136
|
+
public async getAuthData(): Promise<BasicAuthData> {
|
|
137
|
+
const { value } = await this.keyValueStorage.getValueByKey(this.serviceName, this.authKey);
|
|
138
|
+
return value as BasicAuthData;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Deletes authorization from collection
|
|
143
|
+
*/
|
|
144
|
+
public async delete(): Promise<void> {
|
|
145
|
+
await this.keyValueStorage.deleteKey(this.serviceName, this.authKey);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
3
|
+
import { BasicAuth } from './BasicAuth';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
BasicCollectionConfig,
|
|
7
|
+
CreateBasicAuthInCollectionConfig,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* OneReach Authorizer service client
|
|
12
|
+
* ## Installation:
|
|
13
|
+
* ```
|
|
14
|
+
* $ npm i @or-sdk/authorizer
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class BasicCollection {
|
|
18
|
+
private readonly serviceName: string;
|
|
19
|
+
private readonly keyValueStorage: KeyValueStorage;
|
|
20
|
+
private readonly localToken: Token;
|
|
21
|
+
private readonly localDiscoveryUrl: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Connects to authorization collection
|
|
25
|
+
*
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { BasicCollection } from '@or-sdk/authorizer'
|
|
28
|
+
* const basicCollectionInstance = new BasicCollection({
|
|
29
|
+
* token: 'my-account-token-string',
|
|
30
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
31
|
+
* serviceName: '__authorization_service_test_service',
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(params: BasicCollectionConfig) {
|
|
36
|
+
const { token, discoveryUrl, serviceName } = params;
|
|
37
|
+
|
|
38
|
+
this.localToken = token;
|
|
39
|
+
this.localDiscoveryUrl = discoveryUrl;
|
|
40
|
+
this.serviceName = serviceName;
|
|
41
|
+
this.keyValueStorage = new KeyValueStorage({
|
|
42
|
+
token,
|
|
43
|
+
discoveryUrl,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns array of auth keys from this collection
|
|
49
|
+
*/
|
|
50
|
+
public async listAuthorizations(): Promise<string[]> {
|
|
51
|
+
const records = await this.keyValueStorage.listKeys(this.serviceName);
|
|
52
|
+
|
|
53
|
+
return records.map((record) => record.key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns BasicAuth instance for given key
|
|
58
|
+
*/
|
|
59
|
+
public async getAuthorization(key: string): Promise<BasicAuth> {
|
|
60
|
+
const record = await this.keyValueStorage.getValueByKey(this.serviceName, key);
|
|
61
|
+
if (!record.value) {
|
|
62
|
+
throw new Error('Authorization does not exist');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return new BasicAuth({
|
|
66
|
+
serviceName: this.serviceName,
|
|
67
|
+
authKey: key,
|
|
68
|
+
discoveryUrl: this.localDiscoveryUrl,
|
|
69
|
+
token: this.localToken,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates a new auth record in current collection with the given config.
|
|
75
|
+
* @returns BasicAuth instance connected to created auth
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const basicAuthInstance = await basicCollectionInstance.createAuthorization({
|
|
79
|
+
* authName: 'my-auth-name',
|
|
80
|
+
* username: 'user',
|
|
81
|
+
* password: 'pass'
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
public async createAuthorization(
|
|
86
|
+
params: CreateBasicAuthInCollectionConfig
|
|
87
|
+
): Promise<BasicAuth> {
|
|
88
|
+
return await BasicAuth.create({
|
|
89
|
+
discoveryUrl: this.localDiscoveryUrl,
|
|
90
|
+
token: this.localToken,
|
|
91
|
+
serviceName: this.serviceName,
|
|
92
|
+
authName: params.authName,
|
|
93
|
+
password: params.password,
|
|
94
|
+
username: params.username,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
|
|
3
|
+
export type BasicAuthConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* token
|
|
6
|
+
*/
|
|
7
|
+
token: Token;
|
|
8
|
+
/**
|
|
9
|
+
* function which return token
|
|
10
|
+
*/
|
|
11
|
+
discoveryUrl: string;
|
|
12
|
+
serviceName: string;
|
|
13
|
+
authKey: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type BasicAuthData = {
|
|
17
|
+
auth_name: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HTTP Basic authentication Base64 encoded username and password joined by a single colon
|
|
21
|
+
*/
|
|
22
|
+
access_token: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Datetime of auth creation in YYYY-MM-DDTHH:MM:SSZ format
|
|
26
|
+
*/
|
|
27
|
+
date_created: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type CreateBasicAuthConfig = {
|
|
31
|
+
serviceName: string;
|
|
32
|
+
authName: string;
|
|
33
|
+
username: string;
|
|
34
|
+
password: string;
|
|
35
|
+
discoveryUrl: string;
|
|
36
|
+
/**
|
|
37
|
+
* token
|
|
38
|
+
*/
|
|
39
|
+
token: Token;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type CreateBasicAuthInCollectionConfig = Omit<CreateBasicAuthConfig, 'serviceName' | 'discoveryUrl' | 'token'>;
|
|
43
|
+
|
|
44
|
+
export type BasicCollectionConfig = {
|
|
45
|
+
/**
|
|
46
|
+
* token
|
|
47
|
+
*/
|
|
48
|
+
token: Token;
|
|
49
|
+
/**
|
|
50
|
+
* function which return token
|
|
51
|
+
*/
|
|
52
|
+
discoveryUrl: string;
|
|
53
|
+
serviceName: string;
|
|
54
|
+
};
|