45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import {SecurityContext} from './SecurityContext'
|
|
import {AuthenticatableRepository} from '../types'
|
|
import {Awaitable} from '../../util'
|
|
import {Inject} from '../../di'
|
|
import {Request} from '../../http/lifecycle/Request'
|
|
import {OAuth2Token, TokenRepository} from '../server/types'
|
|
import {UserAuthenticationResumedEvent} from '../event/UserAuthenticationResumedEvent'
|
|
|
|
export class TokenSecurityContext extends SecurityContext {
|
|
@Inject()
|
|
protected readonly request!: Request
|
|
|
|
@Inject()
|
|
protected readonly tokens!: TokenRepository
|
|
|
|
constructor(
|
|
public readonly repository: AuthenticatableRepository,
|
|
) {
|
|
super(repository, 'token')
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
persist(): Awaitable<void> {}
|
|
|
|
async resume(): Promise<void> {
|
|
if ( !this.request.hasInstance(OAuth2Token) ) {
|
|
return
|
|
}
|
|
|
|
const token: OAuth2Token = this.request.getExistingInstance(OAuth2Token)
|
|
if ( !token.userId ) {
|
|
return
|
|
}
|
|
|
|
const user = await this.repository.getByIdentifier(token.userId)
|
|
if ( user ) {
|
|
this.authenticatedUser = user
|
|
await this.bus.push(new UserAuthenticationResumedEvent(user, this))
|
|
return
|
|
}
|
|
|
|
this.authenticatedUser = undefined
|
|
}
|
|
}
|