feat: use websocket header to pass the auth token

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-12-05 09:19:50 +02:00
parent d05a6f566f
commit 7af9750ea8
3 changed files with 28 additions and 41 deletions
+3 -13
View File
@@ -123,9 +123,11 @@ export class SignalsClient implements SignalsApi {
}
const apiUrl = `${await this.discoveryApi.getBaseUrl('signals')}`;
const { token } = await this.identity.getCredentials();
const url = new URL(apiUrl);
url.protocol = url.protocol === 'http:' ? 'ws' : 'wss';
this.ws = new WebSocket(url.toString());
this.ws = new WebSocket(url.toString(), token);
this.ws.onmessage = (data: MessageEvent) => {
this.handleMessage(data);
@@ -155,9 +157,6 @@ export class SignalsClient implements SignalsApi {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Connect timeout');
}
// Authenticate
await this.authenticate();
}
private handleMessage(data: MessageEvent) {
@@ -175,15 +174,6 @@ export class SignalsClient implements SignalsApi {
}
}
private async authenticate() {
const { token } = await this.identity.getCredentials();
if (token) {
// Authentication is done with websocket message to server as the plain
// websocket does not allow sending headers during connection upgrade
this.send({ action: 'authenticate', token: token });
}
}
private reconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);