@principal-ai/control-tower-core 0.1.19 → 0.1.21

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/index.mjs CHANGED
@@ -4264,7 +4264,52 @@ class WebSocketServerTransportAdapter {
4264
4264
  return;
4265
4265
  }
4266
4266
  try {
4267
- const credentials = message.payload;
4267
+ const payload = message.payload;
4268
+ if (payload.token && typeof payload.token === "string") {
4269
+ try {
4270
+ const tokenPayload = await this.authAdapter.validateToken(payload.token);
4271
+ if (client.authTimeout) {
4272
+ clearTimeout(client.authTimeout);
4273
+ client.authTimeout = undefined;
4274
+ }
4275
+ client.authenticated = true;
4276
+ client.userId = tokenPayload.userId;
4277
+ client.metadata = tokenPayload.metadata;
4278
+ this.sendToClient(client, {
4279
+ id: this.generateId(),
4280
+ type: "auth_result",
4281
+ payload: {
4282
+ success: true,
4283
+ userId: client.userId
4284
+ },
4285
+ timestamp: Date.now()
4286
+ });
4287
+ const authMessage = {
4288
+ id: this.generateId(),
4289
+ type: "client_authenticated",
4290
+ payload: {
4291
+ clientId: client.id,
4292
+ userId: client.userId,
4293
+ metadata: client.metadata
4294
+ },
4295
+ timestamp: Date.now()
4296
+ };
4297
+ this.messageHandlers.forEach((handler) => handler(authMessage));
4298
+ return;
4299
+ } catch (error) {
4300
+ this.sendToClient(client, {
4301
+ id: this.generateId(),
4302
+ type: "auth_result",
4303
+ payload: {
4304
+ success: false,
4305
+ error: error.message
4306
+ },
4307
+ timestamp: Date.now()
4308
+ });
4309
+ return;
4310
+ }
4311
+ }
4312
+ const credentials = payload;
4268
4313
  const result = await this.authAdapter.authenticate(credentials);
4269
4314
  if (result.success) {
4270
4315
  if (client.authTimeout) {
@@ -4696,6 +4741,28 @@ class BaseClient extends TypedEventEmitter {
4696
4741
  }
4697
4742
  await this.connect(this.lastConnectionUrl, this.lastCredentials || undefined);
4698
4743
  }
4744
+ async authenticate(options) {
4745
+ if (this.connectionState !== "connected") {
4746
+ throw new Error("Client must be connected to authenticate");
4747
+ }
4748
+ if (!this.authToken) {
4749
+ throw new Error("No auth token available. Connect with credentials first.");
4750
+ }
4751
+ const payload = {
4752
+ token: this.authToken,
4753
+ ...options
4754
+ };
4755
+ const message = {
4756
+ id: this.generateId(),
4757
+ type: "authenticate",
4758
+ payload,
4759
+ timestamp: Date.now()
4760
+ };
4761
+ await this.transport.send(message);
4762
+ }
4763
+ hasAuthToken() {
4764
+ return this.authToken !== null;
4765
+ }
4699
4766
  async joinRoom(roomId) {
4700
4767
  if (this.connectionState !== "connected") {
4701
4768
  throw new Error("Client must be connected to join a room");
@@ -4779,6 +4846,9 @@ class BaseClient extends TypedEventEmitter {
4779
4846
  async handleMessage(message) {
4780
4847
  try {
4781
4848
  switch (message.type) {
4849
+ case "auth_result":
4850
+ await this.handleAuthResult(message.payload);
4851
+ break;
4782
4852
  case "room_joined":
4783
4853
  await this.handleRoomJoined(message.payload);
4784
4854
  break;
@@ -4821,6 +4891,16 @@ class BaseClient extends TypedEventEmitter {
4821
4891
  }
4822
4892
  }
4823
4893
  }
4894
+ async handleAuthResult(payload) {
4895
+ if (payload.success) {
4896
+ if (payload.userId) {
4897
+ this.userId = payload.userId;
4898
+ }
4899
+ await this.emit("authenticated", { userId: this.userId || "" });
4900
+ } else {
4901
+ await this.emit("authentication_failed", { error: payload.error || "Authentication failed" });
4902
+ }
4903
+ }
4824
4904
  async handleRoomJoined(payload) {
4825
4905
  this.currentRoomId = payload.roomId;
4826
4906
  const roomState = deserializeRoomState(payload.state);
@@ -5404,7 +5484,8 @@ class BaseServer extends TypedEventEmitter {
5404
5484
  if (payload.authenticated) {
5405
5485
  await this.emit("client_authenticated", {
5406
5486
  clientId: payload.clientId,
5407
- userId: payload.userId || ""
5487
+ userId: payload.userId || "",
5488
+ metadata: payload.metadata
5408
5489
  });
5409
5490
  }
5410
5491
  }
@@ -5421,7 +5502,8 @@ class BaseServer extends TypedEventEmitter {
5421
5502
  this.userClientMap.get(payload.userId).add(payload.clientId);
5422
5503
  await this.emit("client_authenticated", {
5423
5504
  clientId: payload.clientId,
5424
- userId: payload.userId
5505
+ userId: payload.userId,
5506
+ metadata: payload.metadata
5425
5507
  });
5426
5508
  }
5427
5509
  }
@@ -5558,9 +5640,13 @@ class BaseServer extends TypedEventEmitter {
5558
5640
  if (client) {
5559
5641
  client.userId = tokenPayload.userId;
5560
5642
  client.authenticated = true;
5561
- await this.emit("client_authenticated", { clientId, userId: tokenPayload.userId });
5643
+ await this.emit("client_authenticated", {
5644
+ clientId,
5645
+ userId: tokenPayload.userId,
5646
+ metadata: tokenPayload.metadata
5647
+ });
5562
5648
  }
5563
- await this.sendToClient(clientId, { type: "auth_result", success: true });
5649
+ await this.sendToClient(clientId, { type: "auth_result", success: true, userId: tokenPayload.userId });
5564
5650
  } catch (error) {
5565
5651
  await this.sendToClient(clientId, { type: "auth_result", success: false, error: error.message });
5566
5652
  }
@@ -5935,4 +6021,4 @@ export {
5935
6021
  BaseClient
5936
6022
  };
5937
6023
 
5938
- //# debugId=16358B5F472DB04F64756E2164756E21
6024
+ //# debugId=8D71D2794ACED82564756E2164756E21