@punks/backend-entity-manager 0.0.192 → 0.0.194

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1073,7 +1073,7 @@ class ServiceLocator {
1073
1073
  if (!services && !options?.optional) {
1074
1074
  throw new Error(`Service type "${serviceName}" not found in ServiceLocator`);
1075
1075
  }
1076
- return Object.entries(services).map(([, value]) => value);
1076
+ return Object.entries(services ?? {}).map(([, value]) => value);
1077
1077
  }
1078
1078
  resolveMultipleNamed(serviceName, instanceName, options) {
1079
1079
  const services = this.multipleServices[serviceName];
@@ -1082,7 +1082,7 @@ class ServiceLocator {
1082
1082
  }
1083
1083
  const service = services[instanceName];
1084
1084
  if (!service && !options?.optional) {
1085
- throw new Error(`Service instance "${serviceName}" not found in ServiceLocator`);
1085
+ throw new Error(`Service "${serviceName}" instance "${instanceName}" not found in ServiceLocator -> Available: ${services ? Object.keys(services) : "none"}`);
1086
1086
  }
1087
1087
  return service;
1088
1088
  }
@@ -1181,6 +1181,7 @@ const GlobalServices = {
1181
1181
  },
1182
1182
  Authentication: {
1183
1183
  IAuthenticationContextProvider: "IAuthenticationContextProvider",
1184
+ IAuthenticationMiddleware: "IAuthenticationMiddleware",
1184
1185
  },
1185
1186
  Pipelines: {
1186
1187
  IPipelineController: "IPipelineController",
@@ -1413,6 +1414,15 @@ class EntitiesServiceLocator {
1413
1414
  this.provider.registerMultiple(GlobalServices.Plugins.IBucketProvider, name, instance);
1414
1415
  return this;
1415
1416
  }
1417
+ resolveAuthenticationMiddlewares() {
1418
+ return (this.provider.resolveMultiple(GlobalServices.Authentication.IAuthenticationMiddleware, {
1419
+ optional: true,
1420
+ }) ?? []);
1421
+ }
1422
+ registerAuthenticationMiddleware(name, instance) {
1423
+ this.provider.registerMultiple(GlobalServices.Authentication.IAuthenticationMiddleware, name, instance);
1424
+ return this;
1425
+ }
1416
1426
  resolveFileProvider(providerId) {
1417
1427
  return this.provider.resolveMultipleNamed(GlobalServices.Plugins.IFileProvider, providerId);
1418
1428
  }
@@ -3225,6 +3235,7 @@ class NestEntitySnapshotService extends EntitySnapshotService {
3225
3235
  }
3226
3236
 
3227
3237
  const EntityManagerSymbols = {
3238
+ GlobalAuthenticationMiddleware: Symbol.for("WP:GLOBAL_AUTH_MIDDLEWARE"),
3228
3239
  AppInitializer: Symbol.for("WP:APP_INITIALIZER"),
3229
3240
  EventsTracker: Symbol.for("WP:EVENTS_TRACKER"),
3230
3241
  Entity: Symbol.for("WP:ENTITY"),
@@ -3255,6 +3266,11 @@ const WpEntityAuthMiddleware = (entityName, props = {}) => common.applyDecorator
3255
3266
  ...props,
3256
3267
  }));
3257
3268
 
3269
+ const WpGlobalAuthenticationMiddleware = (name, props = {}) => common.applyDecorators(common.Injectable(), common.SetMetadata(EntityManagerSymbols.GlobalAuthenticationMiddleware, {
3270
+ name,
3271
+ ...props,
3272
+ }));
3273
+
3258
3274
  const WpBucketProvider = (providerId) => common.applyDecorators(common.Injectable(), common.SetMetadata(EntityManagerSymbols.BucketProvider, {
3259
3275
  providerId,
3260
3276
  }));
@@ -22148,6 +22164,7 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
22148
22164
  await this.registerEmailLogger();
22149
22165
  await this.registerFilesReferenceRepositoryProviders();
22150
22166
  await this.registerFileProviders();
22167
+ await this.registerGlobalAuthenticationMiddlewares();
22151
22168
  await this.registerBucketProviders();
22152
22169
  await this.registerCacheInstances();
22153
22170
  await this.executeInitializers(app);
@@ -22253,6 +22270,16 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
22253
22270
  this.logger.log(`File provider ${provider.discoveredClass.name} registered 🚜`);
22254
22271
  }
22255
22272
  }
22273
+ async registerGlobalAuthenticationMiddlewares() {
22274
+ const providers = await this.discoverGlobalAuthenticationMiddlewares();
22275
+ for (const provider of providers) {
22276
+ this.registry
22277
+ .getContainer()
22278
+ .getEntitiesServicesLocator()
22279
+ .registerAuthenticationMiddleware(provider.meta.name, provider.discoveredClass.instance);
22280
+ this.logger.log(`Authentication middleware ${provider.discoveredClass.name} registered 🚜`);
22281
+ }
22282
+ }
22256
22283
  async registerBucketProviders() {
22257
22284
  const providers = await this.discoverBucketProviders();
22258
22285
  if (!providers.length) {
@@ -22390,6 +22417,9 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
22390
22417
  async discoverAuthMiddlewares() {
22391
22418
  return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.EntityAuthMiddleware);
22392
22419
  }
22420
+ async discoverGlobalAuthenticationMiddlewares() {
22421
+ return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.GlobalAuthenticationMiddleware);
22422
+ }
22393
22423
  async discoverAppInitializers() {
22394
22424
  return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.AppInitializer);
22395
22425
  }
@@ -22704,7 +22734,8 @@ function appSessionMiddleware(req, res, next) {
22704
22734
  }
22705
22735
 
22706
22736
  let AuthenticationMiddleware = class AuthenticationMiddleware {
22707
- constructor(authService) {
22737
+ constructor(registry, authService) {
22738
+ this.registry = registry;
22708
22739
  this.authService = authService;
22709
22740
  }
22710
22741
  async use(req, res, next) {
@@ -22724,10 +22755,28 @@ let AuthenticationMiddleware = class AuthenticationMiddleware {
22724
22755
  permissions,
22725
22756
  organizationalUnits,
22726
22757
  };
22758
+ await this.postProcessContext(req.auth);
22727
22759
  }
22728
22760
  }
22729
22761
  next();
22730
22762
  }
22763
+ async postProcessContext(auth) {
22764
+ const authenticationMiddlewares = this.getAuthenticationMiddlewares();
22765
+ for (const middleware of authenticationMiddlewares) {
22766
+ const additionalProps = await middleware.processContext(auth);
22767
+ auth = {
22768
+ ...auth,
22769
+ ...additionalProps,
22770
+ };
22771
+ }
22772
+ }
22773
+ getAuthenticationMiddlewares() {
22774
+ return this.registry
22775
+ .getContainer()
22776
+ .getEntitiesServicesLocator()
22777
+ .resolveAuthenticationMiddlewares()
22778
+ .sort((a, b) => a.getPriority() - b.getPriority());
22779
+ }
22731
22780
  async getUserFromToken(parsedToken) {
22732
22781
  return await this.authService.usersService.getById(parsedToken.userId);
22733
22782
  }
@@ -22747,7 +22796,8 @@ let AuthenticationMiddleware = class AuthenticationMiddleware {
22747
22796
  };
22748
22797
  AuthenticationMiddleware = __decorate([
22749
22798
  common.Injectable(),
22750
- __metadata("design:paramtypes", [exports.AuthenticationService])
22799
+ __metadata("design:paramtypes", [exports.EntityManagerRegistry,
22800
+ exports.AuthenticationService])
22751
22801
  ], AuthenticationMiddleware);
22752
22802
 
22753
22803
  var AuthenticationModule_1;
@@ -28677,6 +28727,7 @@ exports.WpEntityVersioningProvider = WpEntityVersioningProvider;
28677
28727
  exports.WpEventsTracker = WpEventsTracker;
28678
28728
  exports.WpFileProvider = WpFileProvider;
28679
28729
  exports.WpFileReferenceRepository = WpFileReferenceRepository;
28730
+ exports.WpGlobalAuthenticationMiddleware = WpGlobalAuthenticationMiddleware;
28680
28731
  exports.WpPermissionsService = WpPermissionsService;
28681
28732
  exports.WpPipeline = WpPipeline;
28682
28733
  exports.WpRolesService = WpRolesService;