Error response enhancements, CoreID auth client backend

This commit is contained in:
2022-03-29 01:14:46 -05:00
parent a039b1ff25
commit 8f08b94f74
31 changed files with 736 additions and 52 deletions

View File

@@ -1,10 +1,11 @@
import {Inject, Injectable} from '../../di'
import {Awaitable, Maybe} from '../../util'
import {Awaitable, HTTPStatus, Maybe} from '../../util'
import {Authenticatable, AuthenticatableRepository} from '../types'
import {Logging} from '../../service/Logging'
import {UserAuthenticatedEvent} from '../event/UserAuthenticatedEvent'
import {UserFlushedEvent} from '../event/UserFlushedEvent'
import {Bus} from '../../support/bus'
import {HTTPError} from '../../http/HTTPError'
/**
* Base-class for a context that authenticates users and manages security.
@@ -95,6 +96,20 @@ export abstract class SecurityContext {
return this.authenticatedUser
}
/** Get the current user or throw an authorization error. */
user(): Authenticatable {
if ( !this.hasUser() ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED)
}
const user = this.getUser()
if ( !user ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED)
}
return user
}
/**
* Returns true if there is a currently authenticated user.
*/