@principal-ai/control-tower-core 0.1.20 → 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);
@@ -5566,7 +5646,7 @@ class BaseServer extends TypedEventEmitter {
5566
5646
  metadata: tokenPayload.metadata
5567
5647
  });
5568
5648
  }
5569
- await this.sendToClient(clientId, { type: "auth_result", success: true });
5649
+ await this.sendToClient(clientId, { type: "auth_result", success: true, userId: tokenPayload.userId });
5570
5650
  } catch (error) {
5571
5651
  await this.sendToClient(clientId, { type: "auth_result", success: false, error: error.message });
5572
5652
  }
@@ -5941,4 +6021,4 @@ export {
5941
6021
  BaseClient
5942
6022
  };
5943
6023
 
5944
- //# debugId=08BDA837DE69F3D464756E2164756E21
6024
+ //# debugId=8D71D2794ACED82564756E2164756E21