@@ -4,8 +4,10 @@ import http, {OutgoingHttpHeader} from "node:http";
44import stream from "node:stream" ;
55import { Authenticator } from "./auth/Authenticator.js" ;
66import { Authorisation } from "./auth/Authorisation.js" ;
7- import { AuthenticatedRequest } from "./auth/AuthenticatedRequest.js" ;
7+ import { Permission } from "./auth/index.js" ;
8+ import { ThrowableResponse } from "./response/index.js" ;
89import { Server } from "./Server.js" ;
10+ import { ServerErrorRegistry } from "./ServerErrorRegistry.js" ;
911
1012/**
1113 * An incoming HTTP request from a connected client.
@@ -282,26 +284,6 @@ export class Request<A> {
282284 return await authenticator . authenticate ( this ) ;
283285 }
284286
285- /**
286- * Attempt to authenticate this request with one of the {@link Server}’s {@link Authenticator}s.
287- * @returns `null` if the request lacks authorisation information.
288- */
289- public async authenticate ( ) : Promise < AuthenticatedRequest < A > | null > {
290- const authorisation = await this . getAuthorisation ( ) ;
291- if ( authorisation === null ) return null ;
292- return new AuthenticatedRequest < A > (
293- authorisation ,
294- this . method ,
295- this . originalUrl ,
296- this . url ,
297- this . headers ,
298- this . bodyStream ,
299- this . originalIp ,
300- this . ip ,
301- this . server ,
302- ) ;
303- }
304-
305287 /**
306288 * Returns a boolean value that declares whether the body has been read yet.
307289 */
@@ -375,6 +357,33 @@ export class Request<A> {
375357 return ( await this . blob ( ) ) . text ( ) ;
376358 }
377359
360+ /**
361+ * Require that authorisation can be obtained from this request.
362+ * @throws {@link ThrowableResponse } of {@link ServerErrorRegistry.ErrorCodes.UNAUTHORISED} if authorisation cannot
363+ * be obtained.
364+ */
365+ public async auth ( ) : Promise < Authorisation < A > > ;
366+
367+ /**
368+ * Require that authorisation can be obtained from this request and that the given (requested) permission(s) are
369+ * ALL within the scope of the authorisation.
370+ * @param permissions The requested permissions.
371+ * @throws {@link ThrowableResponse } of {@link ServerErrorRegistry.ErrorCodes.UNAUTHORISED} if authorisation cannot
372+ * be obtained.
373+ * @throws {@link ThrowableResponse } of {@link ServerErrorRegistry.ErrorCodes.NO_PERMISSION} if the authorisation
374+ * lacks any of the requested permissions.
375+ */
376+ public async auth ( ...permissions : [ Permission , ...Permission [ ] ] ) : Promise < Authorisation < A > > ;
377+
378+ public async auth ( ...permissions : Permission [ ] ) : Promise < Authorisation < A > > {
379+ const authorisation = await this . getAuthorisation ( ) ;
380+ if ( authorisation === null )
381+ throw new ThrowableResponse ( this . server . errors . _get ( ServerErrorRegistry . ErrorCodes . UNAUTHORISED , null ) ) ;
382+ if ( permissions . length > 0 && ! authorisation . hasAll ( permissions ) )
383+ throw new ThrowableResponse ( this . server . errors . _get ( ServerErrorRegistry . ErrorCodes . NO_PERMISSION , null ) ) ;
384+ return authorisation ;
385+ }
386+
378387 /**
379388 * Response headers that the Response to this request should include.
380389 * @internal
0 commit comments