@punks/backend-entity-manager 0.0.241 → 0.0.243

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.
Files changed (78) hide show
  1. package/dist/cjs/index.js +82 -6
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/types/abstractions/authentication.d.ts +1 -0
  4. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.actions.d.ts +8 -0
  5. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.controller.d.ts +18 -0
  6. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.converter.d.ts +9 -0
  7. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.dto.d.ts +25 -0
  8. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.module.d.ts +2 -0
  9. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.serializer.d.ts +12 -0
  10. package/dist/cjs/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.types.d.ts +31 -0
  11. package/dist/cjs/types/platforms/nest/__test__/server/app/appAuth/appAuth.test-controller.d.ts +3 -0
  12. package/dist/cjs/types/platforms/nest/__test__/server/database/core/entities/appApiKey.entity.d.ts +12 -0
  13. package/dist/cjs/types/platforms/nest/__test__/server/database/core/entities.d.ts +2 -1
  14. package/dist/cjs/types/platforms/nest/__test__/server/database/core/index.d.ts +4 -0
  15. package/dist/cjs/types/platforms/nest/__test__/server/database/core/repositories/appApiKey.repository.d.ts +6 -0
  16. package/dist/cjs/types/platforms/nest/__test__/server/database/core/repositories/appOrganization.repository.d.ts +2 -0
  17. package/dist/cjs/types/platforms/nest/__test__/server/database/core/repositories.d.ts +2 -1
  18. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.adapter.d.ts +7 -0
  19. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.authentication.d.ts +11 -0
  20. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.manager.d.ts +7 -0
  21. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.models.d.ts +16 -0
  22. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.module.d.ts +2 -0
  23. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.query.d.ts +13 -0
  24. package/dist/cjs/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.types.d.ts +70 -0
  25. package/dist/cjs/types/platforms/nest/__test__/server/entities/modules.d.ts +2 -2
  26. package/dist/cjs/types/platforms/nest/__test__/server/infrastructure/authentication/providers/apiKey/index.d.ts +16 -0
  27. package/dist/cjs/types/platforms/nest/__test__/server/infrastructure/authentication/providers/context/index.d.ts +3 -1
  28. package/dist/cjs/types/platforms/nest/__test__/server/infrastructure/authentication/providers/index.d.ts +2 -1
  29. package/dist/cjs/types/platforms/nest/extensions/authentication/abstractions/index.d.ts +22 -4
  30. package/dist/cjs/types/platforms/nest/extensions/authentication/decorators/apiKeysService.d.ts +1 -0
  31. package/dist/cjs/types/platforms/nest/extensions/authentication/decorators/guards.d.ts +1 -0
  32. package/dist/cjs/types/platforms/nest/extensions/authentication/decorators/index.d.ts +2 -1
  33. package/dist/cjs/types/platforms/nest/extensions/authentication/decorators/symbols.d.ts +4 -2
  34. package/dist/cjs/types/platforms/nest/extensions/authentication/guards/auth.d.ts +1 -0
  35. package/dist/cjs/types/platforms/nest/extensions/authentication/initializer/index.d.ts +2 -0
  36. package/dist/cjs/types/platforms/nest/extensions/authentication/middlewares/authentication/index.d.ts +1 -0
  37. package/dist/cjs/types/platforms/nest/extensions/authentication/resolver/index.d.ts +4 -1
  38. package/dist/cjs/types/platforms/nest/extensions/authentication/services/authentication/index.d.ts +1 -0
  39. package/dist/esm/index.js +81 -7
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/types/abstractions/authentication.d.ts +1 -0
  42. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.actions.d.ts +8 -0
  43. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.controller.d.ts +18 -0
  44. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.converter.d.ts +9 -0
  45. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.dto.d.ts +25 -0
  46. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.module.d.ts +2 -0
  47. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.serializer.d.ts +12 -0
  48. package/dist/esm/types/platforms/nest/__test__/server/app/appApiKeys/appApiKey.types.d.ts +31 -0
  49. package/dist/esm/types/platforms/nest/__test__/server/app/appAuth/appAuth.test-controller.d.ts +3 -0
  50. package/dist/esm/types/platforms/nest/__test__/server/database/core/entities/appApiKey.entity.d.ts +12 -0
  51. package/dist/esm/types/platforms/nest/__test__/server/database/core/entities.d.ts +2 -1
  52. package/dist/esm/types/platforms/nest/__test__/server/database/core/index.d.ts +4 -0
  53. package/dist/esm/types/platforms/nest/__test__/server/database/core/repositories/appApiKey.repository.d.ts +6 -0
  54. package/dist/esm/types/platforms/nest/__test__/server/database/core/repositories/appOrganization.repository.d.ts +2 -0
  55. package/dist/esm/types/platforms/nest/__test__/server/database/core/repositories.d.ts +2 -1
  56. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.adapter.d.ts +7 -0
  57. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.authentication.d.ts +11 -0
  58. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.manager.d.ts +7 -0
  59. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.models.d.ts +16 -0
  60. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.module.d.ts +2 -0
  61. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.query.d.ts +13 -0
  62. package/dist/esm/types/platforms/nest/__test__/server/entities/appApiKeys/appApiKey.types.d.ts +70 -0
  63. package/dist/esm/types/platforms/nest/__test__/server/entities/modules.d.ts +2 -2
  64. package/dist/esm/types/platforms/nest/__test__/server/infrastructure/authentication/providers/apiKey/index.d.ts +16 -0
  65. package/dist/esm/types/platforms/nest/__test__/server/infrastructure/authentication/providers/context/index.d.ts +3 -1
  66. package/dist/esm/types/platforms/nest/__test__/server/infrastructure/authentication/providers/index.d.ts +2 -1
  67. package/dist/esm/types/platforms/nest/extensions/authentication/abstractions/index.d.ts +22 -4
  68. package/dist/esm/types/platforms/nest/extensions/authentication/decorators/apiKeysService.d.ts +1 -0
  69. package/dist/esm/types/platforms/nest/extensions/authentication/decorators/guards.d.ts +1 -0
  70. package/dist/esm/types/platforms/nest/extensions/authentication/decorators/index.d.ts +2 -1
  71. package/dist/esm/types/platforms/nest/extensions/authentication/decorators/symbols.d.ts +4 -2
  72. package/dist/esm/types/platforms/nest/extensions/authentication/guards/auth.d.ts +1 -0
  73. package/dist/esm/types/platforms/nest/extensions/authentication/initializer/index.d.ts +2 -0
  74. package/dist/esm/types/platforms/nest/extensions/authentication/middlewares/authentication/index.d.ts +1 -0
  75. package/dist/esm/types/platforms/nest/extensions/authentication/resolver/index.d.ts +4 -1
  76. package/dist/esm/types/platforms/nest/extensions/authentication/services/authentication/index.d.ts +1 -0
  77. package/dist/index.d.ts +35 -7
  78. package/package.json +1 -1
package/dist/cjs/index.js CHANGED
@@ -3626,12 +3626,14 @@ const CurrentUser = common.createParamDecorator((data, context) => {
3626
3626
  });
3627
3627
 
3628
3628
  const AuthenticationExtensionSymbols = {
3629
+ ApiKeysService: Symbol.for("WP.EXT:AUTHENTICATION.API_KEYS_SERVICE"),
3630
+ UserRolesService: Symbol.for("WP.EXT:AUTHENTICATION.USER_ROLES_SERVICE"),
3629
3631
  UserService: Symbol.for("WP.EXT:AUTHENTICATION.USER_SERVICE"),
3630
- RolesService: Symbol.for("WP.EXT:AUTHENTICATION.ROLES_SERVICE"),
3631
3632
  PermissionsService: Symbol.for("WP.EXT:AUTHENTICATION.PERMISSIONS_SERVICE"),
3632
- UserRolesService: Symbol.for("WP.EXT:AUTHENTICATION.USER_ROLES_SERVICE"),
3633
+ RolesService: Symbol.for("WP.EXT:AUTHENTICATION.ROLES_SERVICE"),
3633
3634
  };
3634
3635
  const AuthenticationGuardsSymbols = {
3636
+ ApiKey: "guard:apiKey",
3635
3637
  Authenticated: "guard:authenticated",
3636
3638
  Public: "guard:public",
3637
3639
  Roles: "guard:roles",
@@ -3644,6 +3646,7 @@ const Authenticated = () => common.SetMetadata(AuthenticationGuardsSymbols.Authe
3644
3646
  const Permissions = (...permissions) => common.SetMetadata(AuthenticationGuardsSymbols.Permissions, permissions);
3645
3647
  const Roles = (...roles) => common.SetMetadata(AuthenticationGuardsSymbols.Roles, roles);
3646
3648
  const MemberOf = (...groups) => common.SetMetadata(AuthenticationGuardsSymbols.MemberOf, groups);
3649
+ const ApiKeyAccess = () => common.SetMetadata(AuthenticationGuardsSymbols.ApiKey, true);
3647
3650
  const buildRolesGuard = ({ mainRole, secondaryRoles, }, options) => Roles(mainRole.uid, ...(!options?.exact && secondaryRoles
3648
3651
  ? secondaryRoles.map((role) => role.uid)
3649
3652
  : []));
@@ -3657,6 +3660,8 @@ const WpUserService = () => common.applyDecorators(common.Injectable(), common.S
3657
3660
 
3658
3661
  const WpUserRolesService = () => common.applyDecorators(common.Injectable(), common.SetMetadata(AuthenticationExtensionSymbols.UserRolesService, true));
3659
3662
 
3663
+ const WpApiKeysService = () => common.applyDecorators(common.Injectable(), common.SetMetadata(AuthenticationExtensionSymbols.ApiKeysService, true));
3664
+
3660
3665
  const AuthenticationEmailTemplates = {
3661
3666
  Registration: "registration",
3662
3667
  PasswordReset: "passwordReset",
@@ -3737,6 +3742,19 @@ exports.AuthGuard = AuthGuard_1 = class AuthGuard {
3737
3742
  return true;
3738
3743
  }
3739
3744
  const auth = this.getCurrentAuth(context);
3745
+ const isApiRoute = this.getIsApiRoute(context);
3746
+ if (isApiRoute) {
3747
+ if (auth?.apiKey && !auth.apiKey.disabled) {
3748
+ this.logger.debug(`Authorized:true -> api route with valid api key`, this.getContextInfo({
3749
+ context,
3750
+ }));
3751
+ return true;
3752
+ }
3753
+ this.logger.debug(`Authorized:false -> api route without api key`, this.getContextInfo({
3754
+ context,
3755
+ }));
3756
+ return false;
3757
+ }
3740
3758
  const allowedRoles = this.getAllowedRoles(context);
3741
3759
  if (allowedRoles) {
3742
3760
  const isAllowed = this.isRoleMatching(allowedRoles, auth?.roles ?? []);
@@ -3805,6 +3823,9 @@ exports.AuthGuard = AuthGuard_1 = class AuthGuard {
3805
3823
  getIsPublic(context) {
3806
3824
  return this.getMetadata(AuthenticationGuardsSymbols.Public, context);
3807
3825
  }
3826
+ getIsApiRoute(context) {
3827
+ return this.getMetadata(AuthenticationGuardsSymbols.ApiKey, context);
3828
+ }
3808
3829
  getAllowedRoles(context) {
3809
3830
  return this.getMetadata(AuthenticationGuardsSymbols.Roles, context);
3810
3831
  }
@@ -3813,8 +3834,9 @@ exports.AuthGuard = AuthGuard_1 = class AuthGuard {
3813
3834
  }
3814
3835
  getCurrentAuth(context) {
3815
3836
  const request = context.switchToHttp()?.getRequest();
3816
- return request?.auth?.user
3837
+ return request?.auth?.user || request.auth?.apiKey
3817
3838
  ? {
3839
+ apiKey: request.auth.apiKey,
3818
3840
  user: request.auth.user,
3819
3841
  roles: request.auth.roles,
3820
3842
  permissions: request.auth.permissions,
@@ -3827,12 +3849,21 @@ exports.AuthGuard = AuthGuard_1 = class AuthGuard {
3827
3849
  context.getClass(),
3828
3850
  ]);
3829
3851
  }
3830
- getContextInfo({ context, user, roles, permissions, }) {
3852
+ getContextInfo({ context, apiKey, user, roles, permissions, }) {
3831
3853
  return {
3832
3854
  request: {
3833
3855
  path: context.switchToHttp()?.getRequest()?.path,
3834
3856
  method: context.switchToHttp()?.getRequest()?.method,
3835
3857
  },
3858
+ ...(apiKey
3859
+ ? {
3860
+ apiKey: {
3861
+ key: `${apiKey.key.substring(0, 7)}*****${apiKey.key.slice(-2)}`,
3862
+ name: apiKey.name,
3863
+ organizationId: apiKey.organization?.id,
3864
+ },
3865
+ }
3866
+ : {}),
3836
3867
  ...(user
3837
3868
  ? {
3838
3869
  user: {
@@ -21759,6 +21790,9 @@ exports.SecretsService = __decorate([
21759
21790
  ], exports.SecretsService);
21760
21791
 
21761
21792
  let AuthenticationServicesResolver = class AuthenticationServicesResolver {
21793
+ registerApiKeysService(service) {
21794
+ this.apiKeysService = service;
21795
+ }
21762
21796
  registerUsersService(usersService) {
21763
21797
  this.usersService = usersService;
21764
21798
  }
@@ -21771,6 +21805,12 @@ let AuthenticationServicesResolver = class AuthenticationServicesResolver {
21771
21805
  registerUserRolesService(userRolesService) {
21772
21806
  this.usersRolesService = userRolesService;
21773
21807
  }
21808
+ getApiKeysService() {
21809
+ if (!this.apiKeysService) {
21810
+ throw new Error("Api keys service is not registered");
21811
+ }
21812
+ return this.apiKeysService;
21813
+ }
21774
21814
  getUsersService() {
21775
21815
  if (!this.usersService) {
21776
21816
  throw new Error("Users service is not registered");
@@ -22451,6 +22491,9 @@ exports.AuthenticationService = class AuthenticationService {
22451
22491
  async userTokenVerify(input) {
22452
22492
  return await this.userTokenVerifyHandler.execute(input);
22453
22493
  }
22494
+ get apiKeysService() {
22495
+ return this.resolver.getApiKeysService();
22496
+ }
22454
22497
  get usersService() {
22455
22498
  return this.resolver.getUsersService();
22456
22499
  }
@@ -23102,6 +23145,7 @@ let AuthenticationInitializer = AuthenticationInitializer_1 = class Authenticati
23102
23145
  await this.registerPermissionsService();
23103
23146
  await this.registerUsersService();
23104
23147
  await this.registerUserRolesService();
23148
+ await this.registerApiKeysService();
23105
23149
  this.logger.log("Authentication initialized 🔑");
23106
23150
  }
23107
23151
  async registerRolesService() {
@@ -23136,6 +23180,15 @@ let AuthenticationInitializer = AuthenticationInitializer_1 = class Authenticati
23136
23180
  this.registry.registerUserRolesService(service.discoveredClass.instance);
23137
23181
  this.logger.log(`Authentication user roles service ${service.discoveredClass.name} registered`);
23138
23182
  }
23183
+ async registerApiKeysService() {
23184
+ const service = await this.discoverApiKeysService();
23185
+ if (!service) {
23186
+ console.warn("No api keys service found");
23187
+ return;
23188
+ }
23189
+ this.registry.registerApiKeysService(service.discoveredClass.instance);
23190
+ this.logger.log(`Authentication api key service ${service.discoveredClass.name} registered`);
23191
+ }
23139
23192
  async discoverUserService() {
23140
23193
  return await this.discover.providerWithMetaAtKey(AuthenticationExtensionSymbols.UserService);
23141
23194
  }
@@ -23148,6 +23201,9 @@ let AuthenticationInitializer = AuthenticationInitializer_1 = class Authenticati
23148
23201
  async discoverUserRolesService() {
23149
23202
  return await this.discover.providerWithMetaAtKey(AuthenticationExtensionSymbols.UserRolesService);
23150
23203
  }
23204
+ async discoverApiKeysService() {
23205
+ return await this.discover.providerWithMetaAtKey(AuthenticationExtensionSymbols.ApiKeysService);
23206
+ }
23151
23207
  };
23152
23208
  AuthenticationInitializer = AuthenticationInitializer_1 = __decorate([
23153
23209
  WpAppInitializer(),
@@ -23203,6 +23259,16 @@ let AuthenticationMiddleware = class AuthenticationMiddleware {
23203
23259
  this.authService = authService;
23204
23260
  }
23205
23261
  async use(req, res, next) {
23262
+ let authData = null;
23263
+ const apiKey = this.extractApiKeyFromHeader(req);
23264
+ if (apiKey) {
23265
+ const entry = await this.authService.apiKeysService.find(apiKey);
23266
+ if (entry && !entry.disabled) {
23267
+ authData = {
23268
+ apiKey: entry,
23269
+ };
23270
+ }
23271
+ }
23206
23272
  const token = this.extractTokenFromHeader(req);
23207
23273
  if (token) {
23208
23274
  const parsedToken = await this.authService.userTokenVerify({ token });
@@ -23213,15 +23279,18 @@ let AuthenticationMiddleware = class AuthenticationMiddleware {
23213
23279
  const organizationalUnits = user
23214
23280
  ? await this.getUserOrganizationalUnits(user)
23215
23281
  : [];
23216
- const authData = {
23282
+ authData = {
23283
+ ...(authData ?? {}),
23217
23284
  user,
23218
23285
  roles,
23219
23286
  permissions,
23220
23287
  organizationalUnits,
23221
23288
  };
23222
- req.auth = await this.postProcessContext(authData);
23223
23289
  }
23224
23290
  }
23291
+ if (authData) {
23292
+ req.auth = await this.postProcessContext(authData);
23293
+ }
23225
23294
  next();
23226
23295
  }
23227
23296
  async postProcessContext(authData) {
@@ -23259,6 +23328,10 @@ let AuthenticationMiddleware = class AuthenticationMiddleware {
23259
23328
  const [type, token] = request.headers.authorization?.split(" ") ?? [];
23260
23329
  return type === "Bearer" ? token : undefined;
23261
23330
  }
23331
+ extractApiKeyFromHeader(request) {
23332
+ const [type, token] = request.headers.authorization?.split(" ") ?? [];
23333
+ return type === "ApiKey" ? token : undefined;
23334
+ }
23262
23335
  };
23263
23336
  AuthenticationMiddleware = __decorate([
23264
23337
  common.Injectable(),
@@ -23756,6 +23829,7 @@ AwsS3FileProvider = __decorate([
23756
23829
  var AwsBucketModule_1;
23757
23830
  const ModuleData$5 = {
23758
23831
  providers: [exports.AwsS3BucketProvider, AwsS3FileProvider],
23832
+ exports: [exports.AwsS3BucketProvider, AwsS3FileProvider],
23759
23833
  };
23760
23834
  exports.AwsBucketModule = AwsBucketModule_1 = class AwsBucketModule {
23761
23835
  static forRoot(input) {
@@ -32443,6 +32517,7 @@ exports.AwsSecretsModule = AwsSecretsModule_1 = __decorate([
32443
32517
  ], exports.AwsSecretsModule);
32444
32518
 
32445
32519
  exports.AUTHENTICATION_EVENTS_NAMESPACE = AUTHENTICATION_EVENTS_NAMESPACE;
32520
+ exports.ApiKeyAccess = ApiKeyAccess;
32446
32521
  exports.AppExceptionsFilterBase = AppExceptionsFilterBase;
32447
32522
  exports.AppInMemorySettings = AppInMemorySettings;
32448
32523
  exports.Authenticated = Authenticated;
@@ -32491,6 +32566,7 @@ exports.SendgridEmailTemplate = SendgridEmailTemplate;
32491
32566
  exports.TypeOrmQueryBuilder = TypeOrmQueryBuilder;
32492
32567
  exports.TypeOrmRepository = TypeOrmRepository;
32493
32568
  exports.TypeormCacheInstance = TypeormCacheInstance;
32569
+ exports.WpApiKeysService = WpApiKeysService;
32494
32570
  exports.WpAppInitializer = WpAppInitializer;
32495
32571
  exports.WpAwsSesEmailTemplate = WpAwsSesEmailTemplate;
32496
32572
  exports.WpBucketProvider = WpBucketProvider;