@dishantlangayan/sc-cli-core 0.6.0 → 0.7.1
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.
|
@@ -53,6 +53,11 @@ export declare class BrokerAuthManager {
|
|
|
53
53
|
* @returns Broker configuration or null if not found
|
|
54
54
|
*/
|
|
55
55
|
getBroker(name: string): Promise<BrokerAuth | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the default broker
|
|
58
|
+
* @returns Default broker or null if no default is set
|
|
59
|
+
*/
|
|
60
|
+
getDefaultBroker(): Promise<BrokerAuth | null>;
|
|
56
61
|
/**
|
|
57
62
|
* Initialize the auth manager with encryption key derived from OS keychain and machine ID
|
|
58
63
|
*/
|
|
@@ -67,6 +72,11 @@ export declare class BrokerAuthManager {
|
|
|
67
72
|
* @param name - Broker name to remove
|
|
68
73
|
*/
|
|
69
74
|
removeBroker(name: string): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Set a broker as the default
|
|
77
|
+
* @param name - Broker name to set as default
|
|
78
|
+
*/
|
|
79
|
+
setDefaultBroker(name: string): Promise<void>;
|
|
70
80
|
/**
|
|
71
81
|
* Update existing broker configuration
|
|
72
82
|
* @param name - Broker name to update
|
|
@@ -90,6 +100,10 @@ export declare class BrokerAuthManager {
|
|
|
90
100
|
* Save storage to encrypted file
|
|
91
101
|
*/
|
|
92
102
|
private saveStorage;
|
|
103
|
+
/**
|
|
104
|
+
* Unset the default flag on all brokers
|
|
105
|
+
*/
|
|
106
|
+
private unsetAllDefaults;
|
|
93
107
|
/**
|
|
94
108
|
* Validate broker configuration
|
|
95
109
|
* @param broker - Broker to validate
|
|
@@ -49,6 +49,10 @@ export class BrokerAuthManager {
|
|
|
49
49
|
if (existing) {
|
|
50
50
|
throw new BrokerAuthError(`Broker '${broker.name}' already exists`, BrokerAuthErrorCode.BROKER_ALREADY_EXISTS);
|
|
51
51
|
}
|
|
52
|
+
// If this broker is being set as default, unset any existing default
|
|
53
|
+
if (broker.isDefault) {
|
|
54
|
+
this.unsetAllDefaults();
|
|
55
|
+
}
|
|
52
56
|
// Add broker
|
|
53
57
|
this.storage.brokers.push(broker);
|
|
54
58
|
// Save to file
|
|
@@ -84,7 +88,7 @@ export class BrokerAuthManager {
|
|
|
84
88
|
throw new BrokerAuthError(`Broker '${brokerName}' not found`, BrokerAuthErrorCode.BROKER_NOT_FOUND);
|
|
85
89
|
}
|
|
86
90
|
const baseURL = `${broker.sempEndpoint}:${broker.sempPort}`;
|
|
87
|
-
const accessToken = broker
|
|
91
|
+
const { accessToken } = broker;
|
|
88
92
|
return new ScConnection(baseURL, accessToken, {
|
|
89
93
|
apiType: 'semp',
|
|
90
94
|
authType: broker.authType === AuthType.BASIC ? 'basic' : 'bearer',
|
|
@@ -109,6 +113,15 @@ export class BrokerAuthManager {
|
|
|
109
113
|
const broker = this.storage.brokers.find((b) => b.name === name);
|
|
110
114
|
return broker ?? null;
|
|
111
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the default broker
|
|
118
|
+
* @returns Default broker or null if no default is set
|
|
119
|
+
*/
|
|
120
|
+
async getDefaultBroker() {
|
|
121
|
+
this.ensureInitialized();
|
|
122
|
+
const broker = this.storage.brokers.find((b) => b.isDefault === true);
|
|
123
|
+
return broker ?? null;
|
|
124
|
+
}
|
|
112
125
|
/**
|
|
113
126
|
* Initialize the auth manager with encryption key derived from OS keychain and machine ID
|
|
114
127
|
*/
|
|
@@ -171,6 +184,23 @@ export class BrokerAuthManager {
|
|
|
171
184
|
// Save to file
|
|
172
185
|
await this.saveStorage();
|
|
173
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Set a broker as the default
|
|
189
|
+
* @param name - Broker name to set as default
|
|
190
|
+
*/
|
|
191
|
+
async setDefaultBroker(name) {
|
|
192
|
+
this.ensureInitialized();
|
|
193
|
+
const index = this.storage.brokers.findIndex((b) => b.name === name);
|
|
194
|
+
if (index === -1) {
|
|
195
|
+
throw new BrokerAuthError(`Broker '${name}' not found`, BrokerAuthErrorCode.BROKER_NOT_FOUND);
|
|
196
|
+
}
|
|
197
|
+
// Unset all existing defaults
|
|
198
|
+
this.unsetAllDefaults();
|
|
199
|
+
// Set this broker as default
|
|
200
|
+
this.storage.brokers[index].isDefault = true;
|
|
201
|
+
// Save to file
|
|
202
|
+
await this.saveStorage();
|
|
203
|
+
}
|
|
174
204
|
/**
|
|
175
205
|
* Update existing broker configuration
|
|
176
206
|
* @param name - Broker name to update
|
|
@@ -182,6 +212,10 @@ export class BrokerAuthManager {
|
|
|
182
212
|
if (index === -1) {
|
|
183
213
|
throw new BrokerAuthError(`Broker '${name}' not found`, BrokerAuthErrorCode.BROKER_NOT_FOUND);
|
|
184
214
|
}
|
|
215
|
+
// If setting this broker as default, unset any existing default
|
|
216
|
+
if (updates.isDefault === true) {
|
|
217
|
+
this.unsetAllDefaults();
|
|
218
|
+
}
|
|
185
219
|
// Merge updates
|
|
186
220
|
const updated = {
|
|
187
221
|
...this.storage.brokers[index],
|
|
@@ -244,14 +278,31 @@ export class BrokerAuthManager {
|
|
|
244
278
|
if (!this.masterKey || !this.machineId) {
|
|
245
279
|
throw new BrokerAuthError('Auth manager not initialized', BrokerAuthErrorCode.NOT_INITIALIZED);
|
|
246
280
|
}
|
|
281
|
+
// If storage is empty, delete the file instead of saving
|
|
282
|
+
if (this.storage.brokers.length === 0) {
|
|
283
|
+
try {
|
|
284
|
+
await unlink(this.configFile);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
// Ignore if file doesn't exist (ENOENT)
|
|
288
|
+
if (error.code !== 'ENOENT') {
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
247
294
|
// Ensure directory exists
|
|
248
295
|
await mkdir(this.configDir, { mode: 0o700, recursive: true });
|
|
249
|
-
//
|
|
250
|
-
const encrypted = await AuthEncryption.encrypt(this.storage, this.encryptionKey);
|
|
251
|
-
// Re-derive key with new salt for next save
|
|
296
|
+
// Generate new salt and derive key for THIS save
|
|
252
297
|
const combinedKey = `${this.masterKey}:${this.machineId}`;
|
|
253
|
-
const newSalt =
|
|
254
|
-
|
|
298
|
+
const newSalt = AuthEncryption.generateSalt();
|
|
299
|
+
const newKey = await AuthEncryption.deriveKey(combinedKey, newSalt);
|
|
300
|
+
// Encrypt data with the new key
|
|
301
|
+
const encrypted = await AuthEncryption.encrypt(this.storage, newKey);
|
|
302
|
+
// Update the salt in encrypted data to match the salt we used for key derivation
|
|
303
|
+
encrypted.salt = newSalt.toString('base64');
|
|
304
|
+
// Store the new key for next operation
|
|
305
|
+
this.encryptionKey = newKey;
|
|
255
306
|
// Write to temp file first (atomic write)
|
|
256
307
|
const jsonData = JSON.stringify(encrypted, null, 2);
|
|
257
308
|
const tempFile = `${this.configFile}.tmp`;
|
|
@@ -277,6 +328,14 @@ export class BrokerAuthManager {
|
|
|
277
328
|
throw new BrokerAuthError('Failed to save broker storage', BrokerAuthErrorCode.FILE_WRITE_ERROR, error);
|
|
278
329
|
}
|
|
279
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Unset the default flag on all brokers
|
|
333
|
+
*/
|
|
334
|
+
unsetAllDefaults() {
|
|
335
|
+
for (const broker of this.storage.brokers) {
|
|
336
|
+
broker.isDefault = false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
280
339
|
/**
|
|
281
340
|
* Validate broker configuration
|
|
282
341
|
* @param broker - Broker to validate
|
|
@@ -8,15 +8,16 @@ export declare enum AuthType {
|
|
|
8
8
|
/**
|
|
9
9
|
* Broker authentication configuration
|
|
10
10
|
*
|
|
11
|
-
* @property
|
|
12
|
-
* @property
|
|
13
|
-
* @property
|
|
14
|
-
* @property
|
|
15
|
-
* @property
|
|
11
|
+
* @property {string} accessToken - For OAuth: access token string. For Basic: base64-encoded credentials
|
|
12
|
+
* @property {AuthType} authType - Type of authentication (BASIC or OAUTH)
|
|
13
|
+
* @property {string} name - Human-readable name/alias for the broker
|
|
14
|
+
* @property {string} sempEndpoint - SEMP endpoint URL (must start with http:// or https://)
|
|
15
|
+
* @property {number} sempPort - SEMP port number (1-65535)
|
|
16
16
|
*/
|
|
17
17
|
export interface BrokerAuth {
|
|
18
18
|
accessToken: string;
|
|
19
19
|
authType: AuthType;
|
|
20
|
+
isDefault?: boolean;
|
|
20
21
|
name: string;
|
|
21
22
|
sempEndpoint: string;
|
|
22
23
|
sempPort: number;
|
package/lib/auth/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { AuthEncryption } from './auth-encryption.js';
|
|
6
6
|
export { type EncryptedData, type EncryptionMetadata } from './auth-types.js';
|
|
7
|
-
export { AuthType, type BrokerAuth, BrokerAuthError, BrokerAuthErrorCode, type BrokerAuthStorage, } from './broker-auth-types.js';
|
|
8
7
|
export { BrokerAuthManager } from './broker-auth-manager.js';
|
|
8
|
+
export { AuthType, type BrokerAuth, BrokerAuthError, BrokerAuthErrorCode, type BrokerAuthStorage, } from './broker-auth-types.js';
|
|
9
9
|
export { OrgManager } from './org-manager.js';
|
|
10
10
|
export { type OrgConfig, OrgError, OrgErrorCode, type OrgStorage } from './org-types.js';
|
package/lib/auth/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Provides encrypted storage for broker and organization credentials
|
|
4
4
|
*/
|
|
5
5
|
export { AuthEncryption } from './auth-encryption.js';
|
|
6
|
-
export { AuthType, BrokerAuthError, BrokerAuthErrorCode, } from './broker-auth-types.js';
|
|
7
6
|
export { BrokerAuthManager } from './broker-auth-manager.js';
|
|
7
|
+
export { AuthType, BrokerAuthError, BrokerAuthErrorCode, } from './broker-auth-types.js';
|
|
8
8
|
export { OrgManager } from './org-manager.js';
|
|
9
9
|
export { OrgError, OrgErrorCode } from './org-types.js';
|