parent
efb9726470
commit
e339ec718d
@ -0,0 +1,27 @@
|
|||||||
|
import {BaseEvent, BaseSerializer, ObjectSerializer} from '../../support/bus'
|
||||||
|
import {Awaitable} from '../../util'
|
||||||
|
|
||||||
|
/** An event raised when a required auth check has failed. */
|
||||||
|
export class AuthCheckFailed extends BaseEvent {
|
||||||
|
eventName = '@extollo/lib:AuthCheckFailed'
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Serializes AuthCheckFailed events. */
|
||||||
|
@ObjectSerializer()
|
||||||
|
export class AuthCheckFailedSerializer extends BaseSerializer<AuthCheckFailed, { authCheckFailed: true }> {
|
||||||
|
protected decodeSerial(): Awaitable<AuthCheckFailed> {
|
||||||
|
return new AuthCheckFailed()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected encodeActual(): Awaitable<{ authCheckFailed: true }> {
|
||||||
|
return { authCheckFailed: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
protected getName(): string {
|
||||||
|
return '@extollo/lib:AuthCheckFailedSerializer'
|
||||||
|
}
|
||||||
|
|
||||||
|
matchActual(some: AuthCheckFailed): boolean {
|
||||||
|
return some instanceof AuthCheckFailed
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
import {Container} from '../di'
|
||||||
|
import {RequestLocalStorage} from '../http/RequestLocalStorage'
|
||||||
|
import {Session} from '../http/session/Session'
|
||||||
|
import {Logging} from '../service/Logging'
|
||||||
|
import {SecurityContext} from './context/SecurityContext'
|
||||||
|
import {Bus} from '../support/bus'
|
||||||
|
import {AuthCheckFailed} from './event/AuthCheckFailed'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the security context for the current request's web socket is still valid.
|
||||||
|
* If not, raise an `AuthCheckFailed` event. This is meant to be used as a subscriber
|
||||||
|
* to `WebSocketHealthCheckEvent` on the request.
|
||||||
|
*
|
||||||
|
* @see AuthCheckFailed
|
||||||
|
*/
|
||||||
|
export async function webSocketAuthCheck(): Promise<void> {
|
||||||
|
const request = Container.getContainer()
|
||||||
|
.make<RequestLocalStorage>(RequestLocalStorage)
|
||||||
|
.get()
|
||||||
|
|
||||||
|
const logging = request.make<Logging>(Logging)
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Try to re-load the session in case we're using the SessionSecurityContext
|
||||||
|
await request.make<Session>(Session).load()
|
||||||
|
} catch (e: unknown) {
|
||||||
|
logging.error(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
const security = request.make<SecurityContext>(SecurityContext)
|
||||||
|
await security.resume()
|
||||||
|
|
||||||
|
if ( !security.hasUser() ) {
|
||||||
|
await request.make<Bus>(Bus).push(new AuthCheckFailed())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
import {Event} from '../../support/bus'
|
||||||
|
import {uuid4} from '../../util'
|
||||||
|
|
||||||
|
/** Event used to tell the server to close the websocket connection. */
|
||||||
|
export class WebSocketHealthCheckEvent implements Event {
|
||||||
|
eventName = '@extollo/lib:WebSocketHealthCheckEvent'
|
||||||
|
|
||||||
|
eventUuid = uuid4()
|
||||||
|
|
||||||
|
shouldBroadcast = false
|
||||||
|
}
|
Loading…
Reference in new issue