-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryCredentialStore.ts
110 lines (101 loc) · 3.8 KB
/
MemoryCredentialStore.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
/** @module auth */
/** @hidden */
let async = require('async');
import { ConfigParams } from 'pip-services-commons-node';
import { IReconfigurable } from 'pip-services-commons-node';
import { StringValueMap } from 'pip-services-commons-node';
import { CredentialParams } from './CredentialParams';
import { ICredentialStore } from './ICredentialStore';
/**
* Credential store that keeps credentials in memory.
*
* ### Configuration parameters ###
*
* - [credential key 1]:
* - ... credential parameters for key 1
* - [credential key 2]:
* - ... credential parameters for key N
* - ...
*
* @see [[ICredentialStore]]
* @see [[CredentialParams]]
*
* ### Example ###
*
* let config = ConfigParams.fromTuples(
* "key1.user", "jdoe",
* "key1.pass", "pass123",
* "key2.user", "bsmith",
* "key2.pass", "mypass"
* );
*
* let credentialStore = new MemoryCredentialStore();
* credentialStore.readCredentials(config);
*
* credentialStore.lookup("123", "key1", (err, credential) => {
* // Result: user=jdoe;pass=pass123
* });
*/
export class MemoryCredentialStore implements ICredentialStore, IReconfigurable {
private readonly _items: StringValueMap = new StringValueMap();
/**
* Creates a new instance of the credential store.
*
* @param config (optional) configuration with credential parameters.
*/
public constructor(config: ConfigParams = null) {
if (config != null)
this.configure(config);
}
/**
* Configures component by passing configuration parameters.
*
* @param config configuration parameters to be set.
*/
public configure(config: ConfigParams): void {
this.readCredentials(config);
}
/**
* Reads credentials from configuration parameters.
* Each section represents an individual CredentialParams
*
* @param config configuration parameters to be read
*/
public readCredentials(config: ConfigParams) {
this._items.clear();
let keys = config.getKeys();
for (let index = 0; index < keys.length; index++) {
let key = keys[index];
let value = config.getAsNullableString(key);
this._items.put(key, CredentialParams.fromTuplesArray([key, value]));
}
}
/**
* Stores credential parameters into the store.
*
* @param correlationId (optional) transaction id to trace execution through call chain.
* @param key a key to uniquely identify the credential parameters.
* @param credential a credential parameters to be stored.
* @param callback callback function that receives an error or null for success.
*/
public store(correlationId: string, key: string, credential: CredentialParams,
callback: (err: any) => void): void {
if (credential != null)
this._items.put(key, credential);
else
this._items.remove(key);
if (callback) callback(null);
}
/**
* Lookups credential parameters by its key.
*
* @param correlationId (optional) transaction id to trace execution through call chain.
* @param key a key to uniquely identify the credential parameters.
* @param callback callback function that receives found credential parameters or error.
*/
public lookup(correlationId: string, key: string,
callback: (err: any, result: CredentialParams) => void): void {
let credential: any = this._items.getAsObject(key);
callback(null, <CredentialParams>credential);
}
}