@@ -4,8 +4,6 @@ import { RegistrationStore } from '../../util/RegistrationStore';
44import { Permission } from '../../views/Permission' ;
55import { Authorizer } from './Authorizer' ;
66
7- const namespace = ( resource : string ) => new URL ( resource ) . pathname . split ( '/' ) ?. [ 2 ] ?? '' ;
8-
97/**
108 * An authorizer delegating to different authorizers based on the namespaces in the request.
119 */
@@ -19,11 +17,15 @@ export class NamespacedAuthorizer implements Authorizer {
1917 * and the value being the corresponding authorizer to use for that namespace.
2018 * @param fallback - Authorizer to use if there is no namespace match.
2119 * @param registrationStore - The key/value store containing the resource registrations.
20+ * @param namespacePosition - URL segment position to find the namespace, after removing the domain.
21+ * E.g., if URL is http://localhost:3000/alice/profile/card, `profile` has position 2.
22+ * Defaults to 2.
2223 */
2324 constructor (
24- protected authorizers : Record < string , Authorizer > ,
25- protected fallback : Authorizer ,
26- protected registrationStore : RegistrationStore ,
25+ protected readonly authorizers : Record < string , Authorizer > ,
26+ protected readonly fallback : Authorizer ,
27+ protected readonly registrationStore : RegistrationStore ,
28+ protected readonly namespacePosition = 2 ,
2729 ) { }
2830
2931 /** @inheritdoc */
@@ -68,9 +70,9 @@ export class NamespacedAuthorizer implements Authorizer {
6870 const resourceIdentifier = registration . description . name ;
6971 if ( ! resourceIdentifier ) {
7072 this . logger . warn ( `Resource ${ resourceId } has no registered name.` ) ;
71- return
73+ return ;
7274 }
7375
74- return namespace ( resourceIdentifier ) ;
76+ return new URL ( resourceIdentifier ) . pathname . split ( '/' ) ?. [ this . namespacePosition ] ?? '' ;
7577 }
7678}
0 commit comments