feat: use websocket header to pass the auth token
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user