- protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
- if (this.isBasicAuthEnabled() === true) {
- if (this.isValidBasicAuth(req) === false) {
- next(new Error('Unauthorized'));
- }
- next();
- }
- next();
+ private isValidBasicAuth (req: IncomingMessage, next: (err?: Error) => void): boolean {
+ const [username, password] = getUsernameAndPasswordFromAuthorizationToken(
+ req.headers.authorization?.split(/\s+/).pop() ?? '',
+ next
+ )
+ return this.isValidUsernameAndPassword(username, password)
+ }
+
+ private isValidProtocolBasicAuth (req: IncomingMessage, next: (err?: Error) => void): boolean {
+ const authorizationProtocol = req.headers['sec-websocket-protocol']?.split(/,\s+/).pop()
+ const [username, password] = getUsernameAndPasswordFromAuthorizationToken(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ `${authorizationProtocol}${Array(((4 - (authorizationProtocol!.length % 4)) % 4) + 1).join(
+ '='
+ )}`
+ .split('.')
+ .pop() ?? '',
+ next
+ )
+ return this.isValidUsernameAndPassword(username, password)
+ }
+
+ private isValidUsernameAndPassword (username: string, password: string): boolean {
+ return (
+ this.uiServerConfiguration.authentication?.username === username &&
+ this.uiServerConfiguration.authentication.password === password
+ )