@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,131 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
import { OAuth } from './OAuth';
|
|
3
|
+
|
|
4
|
+
export type OAuthConfig = {
|
|
5
|
+
/**
|
|
6
|
+
* token or function which return token
|
|
7
|
+
*/
|
|
8
|
+
token: Token;
|
|
9
|
+
discoveryUrl: string;
|
|
10
|
+
serviceName: string;
|
|
11
|
+
keyValueCollection?: string;
|
|
12
|
+
authKey: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type OAuthData = {
|
|
16
|
+
auth_name: string;
|
|
17
|
+
displayServiceName: string;
|
|
18
|
+
appId: string;
|
|
19
|
+
token_type: string;
|
|
20
|
+
scope: string;
|
|
21
|
+
redirect_uri: string;
|
|
22
|
+
expires_in?: number;
|
|
23
|
+
refreshUri?: string;
|
|
24
|
+
access_token: string;
|
|
25
|
+
created_at: string;
|
|
26
|
+
authLinkParams?: Record<string, string>;
|
|
27
|
+
accountId: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type CreateOAuthConfig = {
|
|
31
|
+
/**
|
|
32
|
+
* Name of OAuth service definition. If service definition is not found in global library, it will be taken from local library
|
|
33
|
+
*/
|
|
34
|
+
serviceName: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Pass this if your using custom name for key-value collection that differs from serviceName
|
|
38
|
+
*/
|
|
39
|
+
keyValueCollection?: string;
|
|
40
|
+
|
|
41
|
+
authName: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* ID of OAuth application. Use the value "predefined" to use environment-shared application
|
|
45
|
+
*/
|
|
46
|
+
appId: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Array of OAuth scopes
|
|
50
|
+
*/
|
|
51
|
+
scope?: Array<string>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Token or function which return token
|
|
55
|
+
*/
|
|
56
|
+
token: Token;
|
|
57
|
+
|
|
58
|
+
discoveryUrl: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type CreateOAuthInCollectionConfig = Omit<CreateOAuthConfig, 'discoveryUrl' | 'serviceName' | 'token' | 'keyValueCollection'>;
|
|
62
|
+
|
|
63
|
+
export type CreateOAuthResult = {
|
|
64
|
+
authorizeUrl: string;
|
|
65
|
+
instance: OAuth;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type OAuthApp = {
|
|
69
|
+
name: string;
|
|
70
|
+
appId: string;
|
|
71
|
+
clientId: string;
|
|
72
|
+
clientSecret: string;
|
|
73
|
+
environment?: string;
|
|
74
|
+
authLinkParams?: Record<string, string>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type CreateOAuthAppConfig = {
|
|
78
|
+
name: string;
|
|
79
|
+
clientId: string;
|
|
80
|
+
clientSecret: string;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Name of environment. You can omit this parameter if you want to use the default environment.
|
|
84
|
+
*/
|
|
85
|
+
environment?: string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Object with values for additional params.
|
|
89
|
+
*/
|
|
90
|
+
authLinkParams?: Record<string, string>;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export type OAuthCollectionConfig = {
|
|
94
|
+
/**
|
|
95
|
+
* token or function which return token
|
|
96
|
+
*/
|
|
97
|
+
token: Token;
|
|
98
|
+
discoveryUrl: string;
|
|
99
|
+
serviceName: string;
|
|
100
|
+
/**
|
|
101
|
+
* Pass this if your using custom name for key-value collection that differs from serviceName
|
|
102
|
+
*/
|
|
103
|
+
keyValueCollection?: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export type ServiceDefinitionConfig = {
|
|
107
|
+
serviceName: string;
|
|
108
|
+
requestDataType: string;
|
|
109
|
+
grantType?: string;
|
|
110
|
+
authorizeUri: string;
|
|
111
|
+
exchangeTokenUri: string;
|
|
112
|
+
useRefresh: boolean;
|
|
113
|
+
expiresInPropertyName: string;
|
|
114
|
+
expiresInDefaultValue?: number;
|
|
115
|
+
refreshUri?: string;
|
|
116
|
+
scopeType: ScopeType;
|
|
117
|
+
authRequestAdditionalParams: string;
|
|
118
|
+
codeExchangeRequestAdditionalParams: string;
|
|
119
|
+
environments?: Record<string, any>;
|
|
120
|
+
authLinkAdditionalParams?: Record<string, string>[];
|
|
121
|
+
displayServiceName?: string;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export enum ScopeType {
|
|
125
|
+
SPACE_DELIMITED = 'SPACE_DELIMITED',
|
|
126
|
+
COMMA_DELIMITED = 'COMMA_DELIMITED',
|
|
127
|
+
COMMA_DELIMITED_WITHOUT_WHITESPACE = 'COMMA_DELIMITED_WITHOUT_WHITESPACE',
|
|
128
|
+
JSON_ARRAY = 'JSON_ARRAY'
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { ServiceDefinitionConfig, ScopeType } from '../types';
|
|
2
|
+
|
|
3
|
+
export class ServiceDefinition {
|
|
4
|
+
public currentEnvironment: string;
|
|
5
|
+
public authLinkAdditionalParamsValues: Record<string, string>;
|
|
6
|
+
|
|
7
|
+
private readonly _serviceName: string;
|
|
8
|
+
private readonly _requestDataType: string;
|
|
9
|
+
private readonly _grantType?: string;
|
|
10
|
+
private readonly _authorizeUri: string;
|
|
11
|
+
private readonly _exchangeTokenUri: string;
|
|
12
|
+
private readonly _useRefresh: boolean;
|
|
13
|
+
private readonly _expiresInPropertyName: string;
|
|
14
|
+
private readonly _expiresInDefaultValue?: number;
|
|
15
|
+
private readonly _refreshUri?: string;
|
|
16
|
+
private readonly _scopeType: ScopeType;
|
|
17
|
+
private readonly _authRequestAdditionalParams: string;
|
|
18
|
+
private readonly _codeExchangeRequestAdditionalParams: string;
|
|
19
|
+
private readonly _environments?: any;
|
|
20
|
+
private readonly _authLinkAdditionalParams?: Record<string, string>[];
|
|
21
|
+
private readonly _displayServiceName?: string;
|
|
22
|
+
|
|
23
|
+
constructor(params: ServiceDefinitionConfig, authLinkAdditionalParamsValues?: Record<string, string>, currentEnvironment?: string) {
|
|
24
|
+
this.currentEnvironment = currentEnvironment || 'default';
|
|
25
|
+
this.authLinkAdditionalParamsValues = authLinkAdditionalParamsValues || {};
|
|
26
|
+
|
|
27
|
+
this._serviceName = params.serviceName;
|
|
28
|
+
this._requestDataType = params.requestDataType;
|
|
29
|
+
this._grantType = params.grantType;
|
|
30
|
+
this._authorizeUri = params.authorizeUri;
|
|
31
|
+
this._exchangeTokenUri = params.exchangeTokenUri;
|
|
32
|
+
this._useRefresh = params.useRefresh;
|
|
33
|
+
this._expiresInPropertyName = params.expiresInPropertyName;
|
|
34
|
+
this._expiresInDefaultValue = params.expiresInDefaultValue;
|
|
35
|
+
this._refreshUri = params.refreshUri;
|
|
36
|
+
this._scopeType = params.scopeType;
|
|
37
|
+
this._authRequestAdditionalParams = params.authRequestAdditionalParams;
|
|
38
|
+
this._codeExchangeRequestAdditionalParams =
|
|
39
|
+
params.codeExchangeRequestAdditionalParams;
|
|
40
|
+
this._environments = params.environments;
|
|
41
|
+
this._authLinkAdditionalParams = params.authLinkAdditionalParams;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Checks if property exists in environment or not.
|
|
46
|
+
* @param environment
|
|
47
|
+
* @param prop
|
|
48
|
+
*/
|
|
49
|
+
private isPropertyInEnvironment(environment: string, prop: string) {
|
|
50
|
+
if (this._environments === undefined) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const isEnvironmentExist = Object.keys(this._environments).includes(
|
|
55
|
+
environment
|
|
56
|
+
);
|
|
57
|
+
if (!isEnvironmentExist) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const currentEnv = this._environments[environment];
|
|
62
|
+
|
|
63
|
+
if (!currentEnv) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isPropExist = Object.keys(currentEnv).includes(prop);
|
|
68
|
+
return isPropExist;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private replaceAuthLinkParamsByValues(url: string) {
|
|
72
|
+
let template = url;
|
|
73
|
+
const params = this.authLinkAdditionalParamsValues;
|
|
74
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
75
|
+
template = template.replace(new RegExp(`{${key}}`), value);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return template;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public get requestDataType(): string {
|
|
82
|
+
return this._requestDataType;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public get grantType(): string {
|
|
86
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'grantType')) {
|
|
87
|
+
return this._environments[this.currentEnvironment].grantType;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return this._grantType || 'authorization_code';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public get authorizeUri(): string {
|
|
94
|
+
let url;
|
|
95
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'authorizeUri')) {
|
|
96
|
+
url = this._environments[this.currentEnvironment].authorizeUri;
|
|
97
|
+
} else {
|
|
98
|
+
url = this._authorizeUri;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return this.replaceAuthLinkParamsByValues(url);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public get exchangeTokenUri(): string {
|
|
105
|
+
let url;
|
|
106
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'exchangeTokenUri')) {
|
|
107
|
+
url = this._environments[this.currentEnvironment].exchangeTokenUri;
|
|
108
|
+
} else {
|
|
109
|
+
url = this._exchangeTokenUri;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return this.replaceAuthLinkParamsByValues(url);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public get refreshUri(): string {
|
|
116
|
+
let url;
|
|
117
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'refreshUri')) {
|
|
118
|
+
url = this._environments[this.currentEnvironment].refreshUri;
|
|
119
|
+
} else {
|
|
120
|
+
url = this._refreshUri;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return this.replaceAuthLinkParamsByValues(url);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public get useRefresh(): boolean {
|
|
127
|
+
return this._useRefresh;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public get expiresInPropertyName(): string {
|
|
131
|
+
return this._expiresInPropertyName;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public get expiresInDefaultValue(): number {
|
|
135
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'expiresInDefaultValue')) {
|
|
136
|
+
return this._environments[this.currentEnvironment].expiresInDefaultValue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return this._expiresInDefaultValue || 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public get scopeType(): ScopeType {
|
|
143
|
+
return this._scopeType;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public get authRequestAdditionalParams(): string {
|
|
147
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'authRequestAdditionalParams')) {
|
|
148
|
+
return this._environments[this.currentEnvironment].authRequestAdditionalParams;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this._authRequestAdditionalParams;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public get codeExchangeRequestAdditionalParams(): string {
|
|
155
|
+
if (this.isPropertyInEnvironment(this.currentEnvironment, 'codeExchangeRequestAdditionalParams')) {
|
|
156
|
+
return this._environments[this.currentEnvironment].codeExchangeRequestAdditionalParams;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return this._codeExchangeRequestAdditionalParams;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public get authLinkAdditionalParams(): Record<string, string>[] | undefined {
|
|
163
|
+
return this._authLinkAdditionalParams;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public get displayServiceName(): string {
|
|
167
|
+
return this._displayServiceName || this._serviceName.split(
|
|
168
|
+
'__authorization_service_'
|
|
169
|
+
)[1];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ScopeType } from '../types';
|
|
2
|
+
|
|
3
|
+
export const formatScope = (scopeArray: string[], scopeType: ScopeType): string => {
|
|
4
|
+
switch (scopeType) {
|
|
5
|
+
case ScopeType.SPACE_DELIMITED:
|
|
6
|
+
return scopeArray.join(' ').trim();
|
|
7
|
+
|
|
8
|
+
case ScopeType.COMMA_DELIMITED:
|
|
9
|
+
return scopeArray.join(', ').trim();
|
|
10
|
+
|
|
11
|
+
case ScopeType.COMMA_DELIMITED_WITHOUT_WHITESPACE:
|
|
12
|
+
return scopeArray.join(',').trim();
|
|
13
|
+
|
|
14
|
+
case ScopeType.JSON_ARRAY:
|
|
15
|
+
return JSON.stringify(scopeArray);
|
|
16
|
+
|
|
17
|
+
default:
|
|
18
|
+
return scopeArray.join(' ').trim();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const isExpired = (createdAt: string, expireInSeconds: number): boolean => {
|
|
2
|
+
const userDate = new Date(createdAt);
|
|
3
|
+
const userTimezoneOffset = userDate.getTimezoneOffset() * 60000;
|
|
4
|
+
const correctDate = new Date(userDate.getTime() - userTimezoneOffset);
|
|
5
|
+
const diff = (Date.now() - correctDate.getTime()) / 1000;
|
|
6
|
+
return diff > expireInSeconds;
|
|
7
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
|
|
4
|
+
import { createAuthKey } from './utils/createAuthKey';
|
|
5
|
+
|
|
6
|
+
import { TokenAuthConfig, TokenAuthData, CreateTokenAuthConfig } from './types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* OneReach Authorizer service client
|
|
10
|
+
* ## Installation:
|
|
11
|
+
* ```
|
|
12
|
+
* $ npm i @or-sdk/authorizer
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export class TokenAuth {
|
|
16
|
+
private readonly authKey: string;
|
|
17
|
+
private readonly serviceName: string;
|
|
18
|
+
private readonly keyValueStorage: KeyValueStorage;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Connects to existing authorization
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { TokenAuth } from '@or-sdk/authorizer'
|
|
25
|
+
* const instance = new TokenAuth({
|
|
26
|
+
* token: 'my-account-token-string',
|
|
27
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
28
|
+
* serviceName: '__authorization_service_test_service',
|
|
29
|
+
* authKey: '637ac446-1021-475f-9992-3ce7f3ddb637::token::someAuth::__authorization_service_test_service::02bf4366-f987-49ea-90a6-0d346e13c3c1',
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
constructor(params: TokenAuthConfig) {
|
|
34
|
+
const { token, discoveryUrl, authKey, serviceName } = params;
|
|
35
|
+
|
|
36
|
+
this.authKey = authKey;
|
|
37
|
+
this.serviceName = serviceName;
|
|
38
|
+
this.keyValueStorage = new KeyValueStorage({
|
|
39
|
+
token,
|
|
40
|
+
discoveryUrl,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new auth record in specified collection with the given config.
|
|
46
|
+
* @returns TokenAuth instance connected to created auth
|
|
47
|
+
*
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const instance = await TokenAuth.create({
|
|
50
|
+
* token: 'my-account-token-string',
|
|
51
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
52
|
+
* serviceName: '__authorization_service_test_service',
|
|
53
|
+
* authName: 'my-auth-name',
|
|
54
|
+
* credentials: {
|
|
55
|
+
* token: 'secrettoken'
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
static async create(params: CreateTokenAuthConfig): Promise<TokenAuth> {
|
|
61
|
+
if (!(params.serviceName && params.authName && params.credentials && params.discoveryUrl && params.token)) {
|
|
62
|
+
throw new Error('Invalid config passed');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { serviceName, authName, credentials, discoveryUrl, token } = params;
|
|
66
|
+
|
|
67
|
+
const keyValueStorage = new KeyValueStorage({
|
|
68
|
+
token,
|
|
69
|
+
discoveryUrl,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const id = uuidv4();
|
|
73
|
+
const authKey = createAuthKey(id, authName);
|
|
74
|
+
|
|
75
|
+
const authData = {
|
|
76
|
+
...credentials,
|
|
77
|
+
auth_name: authName,
|
|
78
|
+
date_created: new Date().toISOString(),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
await keyValueStorage.setValueByKey(serviceName, authKey, authData);
|
|
82
|
+
|
|
83
|
+
return new TokenAuth({ serviceName,
|
|
84
|
+
authKey,
|
|
85
|
+
discoveryUrl,
|
|
86
|
+
token });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Updates credentials
|
|
91
|
+
*/
|
|
92
|
+
public async update(newCredentials: Record<string, unknown>): Promise<TokenAuthData> {
|
|
93
|
+
const data = await this.getAuthData();
|
|
94
|
+
|
|
95
|
+
const updatedData = {
|
|
96
|
+
...data,
|
|
97
|
+
...newCredentials,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const response = await this.keyValueStorage.setValueByKey(this.serviceName, this.authKey, updatedData);
|
|
101
|
+
|
|
102
|
+
const updatedRecord = response.value;
|
|
103
|
+
return updatedRecord as TokenAuthData;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns authorization data
|
|
108
|
+
*/
|
|
109
|
+
public async getAuthData(): Promise<TokenAuthData> {
|
|
110
|
+
const { value } = await this.keyValueStorage.getValueByKey(this.serviceName, this.authKey);
|
|
111
|
+
return value as TokenAuthData;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Deletes authorization from collection
|
|
116
|
+
*/
|
|
117
|
+
public async delete(): Promise<void> {
|
|
118
|
+
await this.keyValueStorage.deleteKey(this.serviceName, this.authKey);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
import { KeyValueStorage } from '@or-sdk/key-value-storage';
|
|
3
|
+
import { TokenAuth } from './TokenAuth';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
TokenCollectionConfig,
|
|
7
|
+
CreateTokenAuthInCollectionConfig,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* OneReach Authorizer service client
|
|
12
|
+
* ## Installation:
|
|
13
|
+
* ```
|
|
14
|
+
* $ npm i @or-sdk/authorizer
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class TokenCollection {
|
|
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 { TokenCollection } from '@or-sdk/authorizer'
|
|
28
|
+
* const tokenCollectionInstance = new TokenCollection({
|
|
29
|
+
* token: 'my-account-token-string',
|
|
30
|
+
* discoveryUrl: 'discovery.example.onereach.ai',
|
|
31
|
+
* serviceName: '__authorization_service_test_service',
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(params: TokenCollectionConfig) {
|
|
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 TokenAuth instance for given key
|
|
58
|
+
*/
|
|
59
|
+
public async getAuthorization(key: string): Promise<TokenAuth> {
|
|
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 TokenAuth({
|
|
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 TokenCollection instance connected to created auth
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const tokenCollectionInstance = await tokenCollectionInstance.createAuthorization({
|
|
79
|
+
* authName: 'my-auth-name',
|
|
80
|
+
* credentials: {
|
|
81
|
+
* token: 'secrettoken'
|
|
82
|
+
* }
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
public async createAuthorization(
|
|
87
|
+
params: CreateTokenAuthInCollectionConfig
|
|
88
|
+
): Promise<TokenAuth> {
|
|
89
|
+
return await TokenAuth.create({
|
|
90
|
+
discoveryUrl: this.localDiscoveryUrl,
|
|
91
|
+
token: this.localToken,
|
|
92
|
+
serviceName: this.serviceName,
|
|
93
|
+
authName: params.authName,
|
|
94
|
+
credentials: params.credentials,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Token } from '@or-sdk/base';
|
|
2
|
+
|
|
3
|
+
export type TokenAuthConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* token or function which return token
|
|
6
|
+
*/
|
|
7
|
+
token: Token;
|
|
8
|
+
discoveryUrl: string;
|
|
9
|
+
serviceName: string;
|
|
10
|
+
authKey: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type TokenAuthData = {
|
|
14
|
+
auth_name: string;
|
|
15
|
+
/**
|
|
16
|
+
* Datetime of auth creation in YYYY-MM-DDTHH:MM:SSZ format
|
|
17
|
+
*/
|
|
18
|
+
date_created: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Saved credentials by given keys
|
|
22
|
+
*/
|
|
23
|
+
[credentialKey: string]: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type CreateTokenAuthConfig = {
|
|
27
|
+
serviceName: string;
|
|
28
|
+
authName: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Object with credentials
|
|
32
|
+
*/
|
|
33
|
+
credentials: Record<string, unknown>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* token or function which return token
|
|
37
|
+
*/
|
|
38
|
+
token: Token;
|
|
39
|
+
discoveryUrl: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type CreateTokenAuthInCollectionConfig = Omit<CreateTokenAuthConfig, 'serviceName' | 'discoveryUrl' | 'token'>;
|
|
43
|
+
|
|
44
|
+
export type TokenCollectionConfig = {
|
|
45
|
+
/**
|
|
46
|
+
* token or function which return token
|
|
47
|
+
*/
|
|
48
|
+
token: Token;
|
|
49
|
+
discoveryUrl: string;
|
|
50
|
+
serviceName: string;
|
|
51
|
+
};
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const SERVICE_PROVIDER_PATH = '/authorizer/services/list';
|
|
2
|
+
export const OAUTH_REDIRECT_PROVIDER_PATH = '/authorizer/redirect';
|
|
3
|
+
|
|
4
|
+
export enum AuthStatus {
|
|
5
|
+
NEW = 'NEW',
|
|
6
|
+
PENDING = 'PENDING',
|
|
7
|
+
READY = 'READY',
|
|
8
|
+
DELETED = 'DELETED',
|
|
9
|
+
ERROR = 'ERROR'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const PREDEFINED_APP = 'predefined';
|
|
13
|
+
|
|
14
|
+
export const TEMPORARY_DATA_EXPIRATION_TIME = 300000; // 5 minutes
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { BasicAuth } from './Basic/BasicAuth';
|
|
2
|
+
export { BasicCollection } from './Basic/BasicCollection';
|
|
3
|
+
|
|
4
|
+
export { TokenAuth } from './Token/TokenAuth';
|
|
5
|
+
export { TokenCollection } from './Token/TokenCollection';
|
|
6
|
+
|
|
7
|
+
export { OAuth } from './OAuth/OAuth';
|
|
8
|
+
export { OAuthCollection } from './OAuth/OAuthCollection';
|
|
9
|
+
|
|
10
|
+
export * from './types';
|
package/src/types.ts
ADDED
package/tsconfig.json
ADDED