Support more WebSocket endpoints
This commit is contained in:
parent
0fb3951078
commit
d5810d6bac
2 changed files with 48 additions and 6 deletions
|
@ -268,10 +268,8 @@ export class SocketManager extends EventListener {
|
|||
public async fetch(url: string, init: RequestInit): Promise<Response> {
|
||||
const headers = new Headers(init.headers);
|
||||
|
||||
const isAuthenticated = headers.has('Authorization');
|
||||
|
||||
let resource: WebSocketResource;
|
||||
if (isAuthenticated) {
|
||||
if (this.isAuthenticated(headers)) {
|
||||
resource = await this.getAuthenticatedResource();
|
||||
} else {
|
||||
resource = await this.getUnauthenticatedResource();
|
||||
|
@ -618,6 +616,33 @@ export class SocketManager extends EventListener {
|
|||
}
|
||||
}
|
||||
|
||||
private isAuthenticated(headers: Headers): boolean {
|
||||
if (!this.credentials) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const authorization = headers.get('Authorization');
|
||||
if (!authorization) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const [basic, base64] = authorization.split(/\s+/, 2);
|
||||
|
||||
if (basic.toLowerCase() !== 'basic' || !base64) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const [username, password] = Bytes.toString(Bytes.fromBase64(base64)).split(
|
||||
':',
|
||||
2
|
||||
);
|
||||
|
||||
return (
|
||||
username === this.credentials.username &&
|
||||
password === this.credentials.password
|
||||
);
|
||||
}
|
||||
|
||||
// EventEmitter types
|
||||
|
||||
public on(type: 'authError', callback: (error: HTTPError) => void): this;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue