@dishantlangayan/sc-cli-core 0.6.0 → 0.7.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.
@@ -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.accessToken;
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],
@@ -246,12 +280,16 @@ export class BrokerAuthManager {
246
280
  }
247
281
  // Ensure directory exists
248
282
  await mkdir(this.configDir, { mode: 0o700, recursive: true });
249
- // Encrypt data
250
- const encrypted = await AuthEncryption.encrypt(this.storage, this.encryptionKey);
251
- // Re-derive key with new salt for next save
283
+ // Generate new salt and derive key for THIS save
252
284
  const combinedKey = `${this.masterKey}:${this.machineId}`;
253
- const newSalt = Buffer.from(encrypted.salt, 'base64');
254
- this.encryptionKey = await AuthEncryption.deriveKey(combinedKey, newSalt);
285
+ const newSalt = AuthEncryption.generateSalt();
286
+ const newKey = await AuthEncryption.deriveKey(combinedKey, newSalt);
287
+ // Encrypt data with the new key
288
+ const encrypted = await AuthEncryption.encrypt(this.storage, newKey);
289
+ // Update the salt in encrypted data to match the salt we used for key derivation
290
+ encrypted.salt = newSalt.toString('base64');
291
+ // Store the new key for next operation
292
+ this.encryptionKey = newKey;
255
293
  // Write to temp file first (atomic write)
256
294
  const jsonData = JSON.stringify(encrypted, null, 2);
257
295
  const tempFile = `${this.configFile}.tmp`;
@@ -277,6 +315,14 @@ export class BrokerAuthManager {
277
315
  throw new BrokerAuthError('Failed to save broker storage', BrokerAuthErrorCode.FILE_WRITE_ERROR, error);
278
316
  }
279
317
  }
318
+ /**
319
+ * Unset the default flag on all brokers
320
+ */
321
+ unsetAllDefaults() {
322
+ for (const broker of this.storage.brokers) {
323
+ broker.isDefault = false;
324
+ }
325
+ }
280
326
  /**
281
327
  * Validate broker configuration
282
328
  * @param broker - Broker to validate
@@ -8,15 +8,16 @@ export declare enum AuthType {
8
8
  /**
9
9
  * Broker authentication configuration
10
10
  *
11
- * @property authType - Type of authentication (BASIC or OAUTH)
12
- * @property name - Human-readable name/alias for the broker
13
- * @property sempEndpoint - SEMP endpoint URL (must start with http:// or https://)
14
- * @property sempPort - SEMP port number (1-65535)
15
- * @property accessToken - For OAuth: access token string. For Basic: base64-encoded credentials
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;
@@ -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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dishantlangayan/sc-cli-core",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Base library for the Solace Cloud CLI and plugins",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Dishant Langayan",