2021-06-03 03:36:25 +00:00
|
|
|
import {HTTPKernelModule} from '../HTTPKernelModule'
|
|
|
|
import {Injectable, Inject} from '../../../di'
|
|
|
|
import {uuid4} from '../../../util'
|
|
|
|
import {HTTPKernel} from '../HTTPKernel'
|
|
|
|
import {Request} from '../../lifecycle/Request'
|
|
|
|
import {Logging} from '../../../service/Logging'
|
2021-03-07 18:51:04 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* HTTP kernel middleware that tries to look up the session ID from the request.
|
|
|
|
* If none exists, generates a new one and sets the cookie.
|
|
|
|
*/
|
2021-03-07 18:51:04 +00:00
|
|
|
@Injectable()
|
|
|
|
export class SetSessionCookieHTTPModule extends HTTPKernelModule {
|
2021-03-09 15:42:19 +00:00
|
|
|
public readonly executeWithBlockingWriteback = true
|
|
|
|
|
2021-03-07 18:51:04 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public static register(kernel: HTTPKernel): void {
|
2021-03-07 19:26:14 +00:00
|
|
|
kernel.register(this).first()
|
2021-03-07 18:51:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public async apply(request: Request): Promise<Request> {
|
2021-03-07 18:51:04 +00:00
|
|
|
if ( !request.cookies.has('extollo.session') ) {
|
2021-06-03 03:36:25 +00:00
|
|
|
const session = `${uuid4()}-${uuid4()}`
|
2021-03-07 18:51:04 +00:00
|
|
|
|
|
|
|
this.logging.verbose(`Starting session: ${session}`)
|
2021-03-08 15:00:43 +00:00
|
|
|
request.cookies.set('extollo.session', session)
|
2021-03-07 18:51:04 +00:00
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
}
|