type ResponsePayload,
type UIServerConfiguration,
} from '../../types/index.js'
-import { logger } from '../../utils/index.js'
+import { isEmpty, logger } from '../../utils/index.js'
import { UIServiceFactory } from './ui-services/UIServiceFactory.js'
import { getUsernameAndPasswordFromAuthorizationToken } from './UIServerUtils.js'
for (const uiService of this.uiServices.values()) {
uiService.stop()
}
+ this.uiServices.clear()
+ this.responseHandlers.clear()
this.clearCaches()
}
protected authenticate (req: IncomingMessage, next: (err?: Error) => void): void {
- const authorizationError = new BaseError('Unauthorized')
- if (this.isBasicAuthEnabled()) {
- if (!this.isValidBasicAuth(req, next)) {
- next(authorizationError)
- }
+ if (this.uiServerConfiguration.authentication?.enabled !== true) {
next()
+ return
+ }
+ let ok = false
+ if (this.isBasicAuthEnabled()) {
+ ok = this.isValidBasicAuth(req, next)
} else if (this.isProtocolBasicAuthEnabled()) {
- if (!this.isValidProtocolBasicAuth(req, next)) {
- next(authorizationError)
- }
- next()
- } else if (this.uiServerConfiguration.authentication?.enabled === true) {
- next(authorizationError)
+ ok = this.isValidProtocolBasicAuth(req, next)
}
- next()
+ next(ok ? undefined : new BaseError('Unauthorized'))
}
protected registerProtocolVersionUIService (version: ProtocolVersion): void {
private isValidProtocolBasicAuth (req: IncomingMessage, next: (err?: Error) => void): boolean {
const authorizationProtocol = req.headers['sec-websocket-protocol']?.split(/,\s+/).pop()
+ if (authorizationProtocol == null || isEmpty(authorizationProtocol)) {
+ return false
+ }
const usernameAndPassword = getUsernameAndPasswordFromAuthorizationToken(
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/restrict-template-expressions
- `${authorizationProtocol}${Array(((4 - (authorizationProtocol!.length % 4)) % 4) + 1).join(
+ `${authorizationProtocol}${Array(((4 - (authorizationProtocol.length % 4)) % 4) + 1).join(
'='
)}`
.split('.')
import { getRandomValues } from 'node:crypto'
export const sleep = async (milliSeconds: number): Promise<NodeJS.Timeout> => {
- return await new Promise<NodeJS.Timeout>(resolve =>
- setTimeout(resolve as () => void, milliSeconds)
- )
+ return await new Promise<NodeJS.Timeout>(resolve => {
+ const timeout = setTimeout(() => {
+ resolve(timeout)
+ }, milliSeconds)
+ })
}
export const defaultExitHandler = (code: number): void => {