-
Notifications
You must be signed in to change notification settings - Fork 0
/
CredentialParams.ts
230 lines (210 loc) · 7.71 KB
/
CredentialParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** @module auth */
import { ConfigParams } from 'pip-services-commons-node';
import { StringValueMap } from 'pip-services-commons-node';
/**
* Contains credentials to authenticate against external services.
* They are used together with connection parameters, but usually stored
* in a separate store, protected from unauthorized access.
*
* ### Configuration parameters ###
*
* - store_key: key to retrieve parameters from credential store
* - username: user name
* - user: alternative to username
* - password: user password
* - pass: alternative to password
* - access_id: application access id
* - client_id: alternative to access_id
* - access_key: application secret key
* - client_key: alternative to access_key
* - secret_key: alternative to access_key
*
* In addition to standard parameters CredentialParams may contain any number of custom parameters
*
* @see [[https://rawgit.com/pip-services-node/pip-services-commons-node/master/doc/api/classes/config.configparams.html ConfigParams]]
* @see [[ConnectionParams]]
* @see [[CredentialResolver]]
* @see [[ICredentialStore]]
*
* ### Example ###
*
* let credential = CredentialParams.fromTuples(
* "user", "jdoe",
* "pass", "pass123",
* "pin", "321"
* );
*
* let username = credential.getUsername(); // Result: "jdoe"
* let password = credential.getPassword(); // Result: "pass123"
* let pin = credential.getAsNullableString("pin"); // Result: 321
*/
export class CredentialParams extends ConfigParams {
/**
* Creates a new credential parameters and fills it with values.
*
* @param values (optional) an object to be converted into key-value pairs to initialize these credentials.
*/
public constructor(values: any = null) {
super(values);
}
/**
* Checks if these credential parameters shall be retrieved from [[CredentialStore]].
* The credential parameters are redirected to [[CredentialStore]] when store_key parameter is set.
*
* @returns true if credentials shall be retrieved from [[CredentialStore]]
*
* @see [[getStoreKey]]
*/
public useCredentialStore(): boolean {
return super.getAsNullableString("store_key") != null;
}
/**
* Gets the key to retrieve these credentials from [[CredentialStore]].
* If this key is null, than all parameters are already present.
*
* @returns the store key to retrieve credentials.
*
* @see [[useCredentialStore]]
*/
public getStoreKey(): string {
return super.getAsNullableString("store_key");
}
/**
* Sets the key to retrieve these parameters from [[CredentialStore]].
*
* @param value a new key to retrieve credentials.
*/
public setStoreKey(value: string) {
super.put("store_key", value);
}
/**
* Gets the user name.
* The value can be stored in parameters "username" or "user".
*
* @returns the user name.
*/
public getUsername(): string {
return super.getAsNullableString("username") || super.getAsNullableString("user");
}
/**
* Sets the user name.
*
* @param value a new user name.
*/
public setUsername(value: string) {
super.put("username", value);
}
/**
* Get the user password.
* The value can be stored in parameters "password" or "pass".
*
* @returns the user password.
*/
public getPassword(): string {
return super.getAsNullableString("password") || super.getAsNullableString("pass");
}
/**
* Sets the user password.
*
* @param value a new user password.
*/
public setPassword(value: string) {
super.put("password", value);
}
/**
* Gets the application access id.
* The value can be stored in parameters "access_id" pr "client_id"
*
* @returns the application access id.
*/
public getAccessId(): string {
return super.getAsNullableString("access_id")
|| super.getAsNullableString("client_id");
}
/**
* Sets the application access id.
*
* @param value a new application access id.
*/
public setAccessId(value: string) {
super.put("access_id", value);
}
/**
* Gets the application secret key.
* The value can be stored in parameters "access_key", "client_key" or "secret_key".
*
* @returns the application secret key.
*/
public getAccessKey(): string {
return super.getAsNullableString("access_key")
|| super.getAsNullableString("client_key")
|| super.getAsNullableString("secret_key");
}
/**
* Sets the application secret key.
*
* @param value a new application secret key.
*/
public setAccessKey(value: string) {
super.put("access_key", value);
}
/**
* Creates a new CredentialParams object filled with key-value pairs serialized as a string.
*
* @param line a string with serialized key-value pairs as "key1=value1;key2=value2;..."
* Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"
* @returns a new CredentialParams object.
*/
public static fromString(line: string): CredentialParams {
let map = StringValueMap.fromString(line);
return new CredentialParams(map);
}
/**
* Creates a new CredentialParams object filled with provided key-value pairs called tuples.
* Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs.
*
* @param tuples the tuples to fill a new CredentialParams object.
* @returns a new CredentialParams object.
*/
public static fromTuples(...tuples: any[]): CredentialParams {
let map = StringValueMap.fromTuplesArray(tuples);
return new CredentialParams(map);
}
/**
* Retrieves all CredentialParams from configuration parameters
* from "credentials" section. If "credential" section is present instead,
* than it returns a list with only one CredentialParams.
*
* @param config a configuration parameters to retrieve credentials
* @returns a list of retrieved CredentialParams
*/
public static manyFromConfig(config: ConfigParams): CredentialParams[] {
let result: CredentialParams[] = [];
let credentials: ConfigParams = config.getSection("credentials");
if (credentials.length() > 0) {
for (let section in credentials.getSectionNames()) {
let credential: ConfigParams = credentials.getSection(section);
result.push(new CredentialParams(credential));
}
} else {
let credential: ConfigParams = config.getSection("credential");
if (credential.length() > 0)
result.push(new CredentialParams(credential));
}
return result;
}
/**
* Retrieves a single CredentialParams from configuration parameters
* from "credential" section. If "credentials" section is present instead,
* then is returns only the first credential element.
*
* @param config ConfigParams, containing a section named "credential(s)".
* @returns the generated CredentialParams object.
*
* @see [[manyFromConfig]]
*/
public static fromConfig(config: ConfigParams): CredentialParams {
let credentials: CredentialParams[] = this.manyFromConfig(config);
return credentials.length > 0 ? credentials[0] : null;
}
}