@zola_do/authorization 0.3.6 → 0.3.7

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 (34) hide show
  1. package/README.md +11 -11
  2. package/dist/decorators/current-user.decorator.js +6 -2
  3. package/dist/decorators/current-user.decorator.js.map +1 -1
  4. package/dist/guards/api-key.guard.js +7 -6
  5. package/dist/guards/api-key.guard.js.map +1 -1
  6. package/dist/guards/jwt.guard.d.ts +0 -1
  7. package/dist/guards/jwt.guard.js +8 -9
  8. package/dist/guards/jwt.guard.js.map +1 -1
  9. package/dist/guards/optional-jwt.guard.d.ts +0 -1
  10. package/dist/guards/optional-jwt.guard.js +8 -9
  11. package/dist/guards/optional-jwt.guard.js.map +1 -1
  12. package/dist/guards/permissions.guard.d.ts +1 -1
  13. package/dist/guards/permissions.guard.js +46 -9
  14. package/dist/guards/permissions.guard.js.map +1 -1
  15. package/dist/guards/vendor.guard.js +14 -2
  16. package/dist/guards/vendor.guard.js.map +1 -1
  17. package/dist/guards/webhook-signature.guard.d.ts +1 -0
  18. package/dist/guards/webhook-signature.guard.js +13 -8
  19. package/dist/guards/webhook-signature.guard.js.map +1 -1
  20. package/dist/helper/auth.helper.js +9 -4
  21. package/dist/helper/auth.helper.js.map +1 -1
  22. package/dist/helper/escape-html.helper.d.ts +1 -0
  23. package/dist/helper/escape-html.helper.js +12 -0
  24. package/dist/helper/escape-html.helper.js.map +1 -0
  25. package/dist/helper/jwt-request.helper.d.ts +7 -0
  26. package/dist/helper/jwt-request.helper.js +29 -0
  27. package/dist/helper/jwt-request.helper.js.map +1 -0
  28. package/dist/helper/validate-jwt-env.helper.d.ts +1 -0
  29. package/dist/helper/validate-jwt-env.helper.js +21 -6
  30. package/dist/helper/validate-jwt-env.helper.js.map +1 -1
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.js +1 -0
  33. package/dist/index.js.map +1 -1
  34. package/package.json +1 -1
package/README.md CHANGED
@@ -169,9 +169,10 @@ export class ProtectedController {}
169
169
 
170
170
  ### OptionalJwtGuard
171
171
 
172
- Allows anonymous access but extracts user if token is present:
172
+ Allows anonymous access but extracts user if token is present. **`AuthorizationModule` registers global `JwtGuard`**, so also mark the route `@AllowAnonymous()` or the global guard returns 401 before this runs.
173
173
 
174
174
  ```typescript
175
+ @AllowAnonymous()
175
176
  @UseGuards(OptionalJwtGuard)
176
177
  @Controller('optional')
177
178
  export class OptionalController {
@@ -187,7 +188,7 @@ export class OptionalController {
187
188
 
188
189
  ### PermissionsGuard
189
190
 
190
- Requires specific permissions. Supports pipe-separated OR logic:
191
+ Requires specific permissions. Supports pipe-separated OR logic. Routes marked `@AllowAnonymous()` skip permission checks entirely. On protected routes, you must pass a non-empty permission string — `PermissionsGuard('')` returns 403. Omit the guard when you only need JWT (`JwtGuard` global). CRUD factories skip `PermissionsGuard` when `*Permission` is unset.
191
192
 
192
193
  ```typescript
193
194
  @UseGuards(JwtGuard, PermissionsGuard('product:create'))
@@ -359,25 +360,22 @@ export class AuthController {
359
360
 
360
361
  ## Custom JwtGuard Override
361
362
 
362
- Override the default JWT validation logic:
363
+ Extend `JwtGuard` to add checks after library verification (for example suspended accounts):
363
364
 
364
365
  ```typescript
365
- import { Injectable, ExecutionContext } from '@nestjs/common';
366
+ import { ForbiddenException, Injectable, ExecutionContext } from '@nestjs/common';
366
367
  import { JwtGuard } from '@zola_do/authorization';
367
368
 
368
369
  @Injectable()
369
370
  export class AppJwtGuard extends JwtGuard {
370
371
  async canActivate(context: ExecutionContext): Promise<boolean> {
371
- // Run default validation
372
372
  const canActivate = await super.canActivate(context);
373
373
  if (!canActivate) return false;
374
374
 
375
- // Add custom logic
376
375
  const request = context.switchToHttp().getRequest();
377
376
  const user = request.user;
378
377
 
379
- // Check additional conditions
380
- if (user.status === 'suspended') {
378
+ if (user?.status === 'suspended') {
381
379
  throw new ForbiddenException('Account suspended');
382
380
  }
383
381
 
@@ -386,7 +384,7 @@ export class AppJwtGuard extends JwtGuard {
386
384
  }
387
385
  ```
388
386
 
389
- Register in module:
387
+ Register in module (stacks a second global guard on top of `AuthorizationModule`'s default):
390
388
 
391
389
  ```typescript
392
390
  import { APP_GUARD } from '@nestjs/core';
@@ -403,6 +401,8 @@ import { AuthorizationModule, JwtGuard } from '@zola_do/authorization';
403
401
  export class AppModule {}
404
402
  ```
405
403
 
404
+ After `super.canActivate()`, `request.user` is the verified JWT payload. The library sets `Symbol.for('zola.auth.verified')` so a stacked guard does not re-parse the Bearer token. **Do not** set `req.user` in middleware to skip JWT unless you verified the token yourself and call `markJwtVerified(req)` from `@zola_do/authorization`.
405
+
406
406
  ## Strategies
407
407
 
408
408
  ### JwtStrategy
@@ -438,7 +438,7 @@ import { JwtRefreshTokenStrategy } from '@zola_do/authorization';
438
438
  | `JWT_ALGORITHM` | HMAC signing algorithm (`HS256`, `HS384`, `HS512`) | `HS256` |
439
439
  | `API_KEY` | API key for ApiKeyGuard | Optional |
440
440
 
441
- `AuthorizationModule` calls `validateJwtEnv()` on startup. Import `validateJwtEnv` for explicit checks before bootstrap, or pass `{ required: false }` to skip when JWT is intentionally disabled.
441
+ `AuthorizationModule` calls `validateJwtEnv()` on startup (requires distinct access/refresh secrets ≥32 chars and `JWT_*_EXPIRES`). Import `validateJwtEnv` for explicit checks before bootstrap, or pass `{ required: false }` to skip when JWT is intentionally disabled.
442
442
 
443
443
  ### Client IP behind a reverse proxy
444
444
 
@@ -531,7 +531,7 @@ Requires `@nestjs/throttler` peer dependency.
531
531
  | Guard | Description | Auth Required |
532
532
  | ------------------------------- | ----------------------- | -------------------- |
533
533
  | `JwtGuard` | Bearer token validation | Yes (global default) |
534
- | `OptionalJwtGuard` | Bearer token if present | No |
534
+ | `OptionalJwtGuard` | Bearer token if present | No (pair with `@AllowAnonymous()` when `JwtGuard` is global) |
535
535
  | `PermissionsGuard(permissions)` | Permission check | Yes |
536
536
  | `ApiKeyGuard` | API key validation | Yes |
537
537
  | `VendorGuard()` | Vendor status check | Yes |
@@ -2,8 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CurrentUser = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
- exports.CurrentUser = (0, common_1.createParamDecorator)((data, ctx) => {
5
+ exports.CurrentUser = (0, common_1.createParamDecorator)((property, ctx) => {
6
6
  const request = ctx.switchToHttp().getRequest();
7
- return request.user;
7
+ const user = request.user;
8
+ if (property && user != null) {
9
+ return user[property];
10
+ }
11
+ return user;
8
12
  });
9
13
  //# sourceMappingURL=current-user.decorator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"current-user.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-user.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwE;AAE3D,QAAA,WAAW,GAAG,IAAA,6BAAoB,EAC7C,CAAC,IAAa,EAAE,GAAqB,EAAE,EAAE;IACvC,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAEhD,OAAO,OAAO,CAAC,IAAI,CAAC;AACtB,CAAC,CACF,CAAC"}
1
+ {"version":3,"file":"current-user.decorator.js","sourceRoot":"","sources":["../../src/decorators/current-user.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwE;AAE3D,QAAA,WAAW,GAAG,IAAA,6BAAoB,EAC7C,CAAC,QAA4B,EAAE,GAAqB,EAAE,EAAE;IACtD,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAChD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE1B,IAAI,QAAQ,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CACF,CAAC"}
@@ -32,12 +32,13 @@ let ApiKeyGuard = class ApiKeyGuard {
32
32
  }
33
33
  }
34
34
  timingSafeCompare(provided, expected) {
35
- const providedBuffer = Buffer.from(provided);
36
- const expectedBuffer = Buffer.from(expected);
37
- if (providedBuffer.length !== expectedBuffer.length) {
38
- return false;
39
- }
40
- return (0, crypto_1.timingSafeEqual)(providedBuffer, expectedBuffer);
35
+ const providedDigest = (0, crypto_1.createHmac)('sha256', 'zola.api-key-compare')
36
+ .update(provided)
37
+ .digest();
38
+ const expectedDigest = (0, crypto_1.createHmac)('sha256', 'zola.api-key-compare')
39
+ .update(expected)
40
+ .digest();
41
+ return (0, crypto_1.timingSafeEqual)(providedDigest, expectedDigest);
41
42
  }
42
43
  };
43
44
  exports.ApiKeyGuard = ApiKeyGuard;
@@ -1 +1 @@
1
- {"version":3,"file":"api-key.guard.js","sourceRoot":"","sources":["../../src/guards/api-key.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAKwB;AACxB,mCAAyC;AAGlC,IAAM,WAAW,GAAjB,MAAM,WAAW;IACtB,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;YACpD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;YAChC,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;YACpC,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;gBAC1C,MAAM,IAAI,8BAAqB,CAAC,sBAAsB,CAAC,CAAC;YAC1D,CAAC;YAED,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;YAC1C,IAAI,CAAC,aAAa,EAAE,CAAC;gBACnB,MAAM,IAAI,8BAAqB,CAAC,wBAAwB,CAAC,CAAC;YAC5D,CAAC;YAED,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,CAAC;gBACnD,MAAM,IAAI,8BAAqB,CAAC,iBAAiB,CAAC,CAAC;YACrD,CAAC;YAED,O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I,CAAC,iBAAiB,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,CAAC;gBACnD,MAAM,IAAI,8BAAqB,CAAC,iBAAiB,CAAC,CAAC;YACrD,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAEO,iBAAiB,CAAC,QAAgB,EAAE,QAAgB;QAC1D,MAAM,cAAc,GAAG,IAAA,mBAAU,EAAC,QAAQ,EAAE,sBAAsB,CAAC;aAChE,MAAM,CAAC,QAAQ,CAAC;aAChB,MAAM,EAAE,CAAC;QACZ,MAAM,cAAc,GAAG,IAAA,mBAAU,EAAC,QAAQ,EAAE,sBAAsB,CAAC;aAChE,MAAM,CAAC,QAAQ,CAAC;aAChB,MAAM,EAAE,CAAC;QAEZ,OAAO,IAAA,wBAAe,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACzD,CAAC;CACF,CAAA;;sBAnCY,WAAW;IADvB,IAAA,mBAAU,GAAE;GACA,WAAW,CAmCvB"}
@@ -6,5 +6,4 @@ export declare class JwtGuard implements CanActivate {
6
6
  private readonly authHelper;
7
7
  constructor(reflector: Reflector, authHelper: AuthHelper);
8
8
  canActivate(context: ExecutionContext): Promise<boolean>;
9
- private extractTokenFromHeader;
10
9
  }
@@ -14,6 +14,7 @@ const common_1 = require("@nestjs/common");
14
14
  const core_1 = require("@nestjs/core");
15
15
  const allow_anonymous_decorator_1 = require("../decorators/allow-anonymous.decorator");
16
16
  const auth_helper_1 = require("../helper/auth.helper");
17
+ const jwt_request_helper_1 = require("../helper/jwt-request.helper");
17
18
  let JwtGuard = class JwtGuard {
18
19
  constructor(reflector, authHelper) {
19
20
  this.reflector = reflector;
@@ -22,8 +23,12 @@ let JwtGuard = class JwtGuard {
22
23
  async canActivate(context) {
23
24
  var _a;
24
25
  try {
25
- const request = context.switchToHttp().getRequest();
26
- if (request === null || request === void 0 ? void 0 : request.user) {
26
+ const request = (0, jwt_request_helper_1.getHttpRequest)(context);
27
+ if (!request) {
28
+ return true;
29
+ }
30
+ const token = (0, jwt_request_helper_1.extractBearerToken)(request);
31
+ if ((0, jwt_request_helper_1.isJwtVerified)(request) && !token) {
27
32
  return true;
28
33
  }
29
34
  const isAnonymousAllowed = (_a = this.reflector) === null || _a === void 0 ? void 0 : _a.getAllAndOverride(allow_anonymous_decorator_1.ALLOW_ANONYMOUS_META_KEY, [context.getHandler(), context.getClass()]);
@@ -31,7 +36,6 @@ let JwtGuard = class JwtGuard {
31
36
  request.isAnonymous = true;
32
37
  return true;
33
38
  }
34
- const token = this.extractTokenFromHeader(request);
35
39
  if (!token) {
36
40
  throw new common_1.UnauthorizedException('JsonWebTokenError', 'jwt must be provided');
37
41
  }
@@ -41,18 +45,13 @@ let JwtGuard = class JwtGuard {
41
45
  }
42
46
  const user = await this.authHelper.verify(token, secret);
43
47
  request.user = user;
48
+ (0, jwt_request_helper_1.markJwtVerified)(request);
44
49
  return true;
45
50
  }
46
51
  catch (error) {
47
52
  throw error;
48
53
  }
49
54
  }
50
- extractTokenFromHeader(request) {
51
- var _a;
52
- var _b;
53
- const [type, token] = (_b = (_a = request.headers.authorization) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : [];
54
- return type === 'Bearer' ? token : undefined;
55
- }
56
55
  };
57
56
  exports.JwtGuard = JwtGuard;
58
57
  exports.JwtGuard = JwtGuard = __decorate([
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.guard.js","sourceRoot":"","sources":["../../src/guards/jwt.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AAExB,uCAAyC;AACzC,uFAAmF;AACnF,uDAAmD;AAG5C,IAAM,QAAQ,GAAd,MAAM,QAAQ;IACnB,YACmB,SAAoB,EACpB,UAAsB;yBADtB,SAAS;0BACT,UAAU;IAC1B,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;;QACzC,IAAI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J,CAAC;YAED,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;YACnD,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,IAAI,8BAAqB,CAC7B,oBAAoB,EACpB,2CAA2C,CAC5C,CAAC;YACJ,CAAC;YAED,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAEzD,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;YACpB,IAAA,oCAAe,EAAC,OAAO,CAAC,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF,CAAA;;mBArDY,QAAQ;IADpB,IAAA,mBAAU,GAAE;qCAGmB,gBAAS,EACR,wBAAU;GAH9B,QAAQ,CAqDpB"}
@@ -4,5 +4,4 @@ export declare class OptionalJwtGuard implements CanActivate {
4
4
  private readonly authHelper;
5
5
  constructor(authHelper: AuthHelper);
6
6
  canActivate(context: ExecutionContext): Promise<boolean>;
7
- private extractTokenFromHeader;
8
7
  }
@@ -12,17 +12,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.OptionalJwtGuard = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const auth_helper_1 = require("../helper/auth.helper");
15
+ const jwt_request_helper_1 = require("../helper/jwt-request.helper");
15
16
  let OptionalJwtGuard = class OptionalJwtGuard {
16
17
  constructor(authHelper) {
17
18
  this.authHelper = authHelper;
18
19
  }
19
20
  async canActivate(context) {
20
21
  try {
21
- const request = context.switchToHttp().getRequest();
22
- if (request === null || request === void 0 ? void 0 : request.user) {
22
+ const request = (0, jwt_request_helper_1.getHttpRequest)(context);
23
+ if (!request) {
24
+ return true;
25
+ }
26
+ const token = (0, jwt_request_helper_1.extractBearerToken)(request);
27
+ if ((0, jwt_request_helper_1.isJwtVerified)(request) && !token) {
23
28
  return true;
24
29
  }
25
- const token = this.extractTokenFromHeader(request);
26
30
  if (!token) {
27
31
  return true;
28
32
  }
@@ -32,18 +36,13 @@ let OptionalJwtGuard = class OptionalJwtGuard {
32
36
  }
33
37
  const user = await this.authHelper.verify(token, secret);
34
38
  request.user = user;
39
+ (0, jwt_request_helper_1.markJwtVerified)(request);
35
40
  return true;
36
41
  }
37
42
  catch (error) {
38
43
  throw error;
39
44
  }
40
45
  }
41
- extractTokenFromHeader(request) {
42
- var _a;
43
- var _b;
44
- const [type, token] = (_b = (_a = request.headers.authorization) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : [];
45
- return type === 'Bearer' ? token : undefined;
46
- }
47
46
  };
48
47
  exports.OptionalJwtGuard = OptionalJwtGuard;
49
48
  exports.OptionalJwtGuard = OptionalJwtGuard = __decorate([
@@ -1 +1 @@
1
- {"version":3,"file":"optional-jwt.guard.js","sourceRoot":"","sources":["../../src/guards/optional-jwt.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AAExB,uDAAmD;AAG5C,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAC3B,YAA6B,UAAsB;0BAAtB,UAAU;IAAe,CAAC;IAEvD,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;YACpD,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAE,CAAC;gBAClB,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,KAAK,GAAG,IAAI,CAAC,sBAAsB,CAAC,OAAO,CAAC,CAAC;YACnD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;YACnD,IAAI,CAAC,MAAM,EA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,CAAC,KAAK,EAAE,CAAC;gBACrC,OAAO,IAAI,CAAC;YACd,CAAC;YAED,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC;YACnD,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,IAAI,8BAAqB,CAC7B,oBAAoB,EACpB,2CAA2C,CAC5C,CAAC;YACJ,CAAC;YAED,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAEzD,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;YACpB,IAAA,oCAAe,EAAC,OAAO,CAAC,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF,CAAA;;2BArCY,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;qCAE8B,wBAAU;GADxC,gBAAgB,CAqC5B"}
@@ -1,2 +1,2 @@
1
1
  import { CanActivate, Type } from '@nestjs/common';
2
- export declare function PermissionsGuard(permissions: string): Type<CanActivate>;
2
+ export declare function PermissionsGuard(permissions?: string): Type<CanActivate>;
@@ -1,23 +1,60 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.PermissionsGuard = PermissionsGuard;
13
+ const common_1 = require("@nestjs/common");
14
+ const core_1 = require("@nestjs/core");
15
+ const allow_anonymous_decorator_1 = require("../decorators/allow-anonymous.decorator");
16
+ const jwt_request_helper_1 = require("../helper/jwt-request.helper");
17
+ function parseRequiredPermissions(permissions) {
18
+ const trimmed = (permissions !== null && permissions !== void 0 ? permissions : '').trim();
19
+ if (!trimmed) {
20
+ return [];
21
+ }
22
+ return trimmed
23
+ .split('|')
24
+ .map((part) => part.trim())
25
+ .filter((part) => part.length > 0);
26
+ }
4
27
  function PermissionsGuard(permissions) {
5
- class PermissionsGuardMixin {
28
+ let PermissionsGuardMixin = class PermissionsGuardMixin {
29
+ constructor(reflector) {
30
+ this.reflector = reflector;
31
+ }
6
32
  canActivate(context) {
7
- const request = context.switchToHttp().getRequest();
8
- if (!permissions || request.isAnonymous) {
33
+ var _a;
34
+ const request = (0, jwt_request_helper_1.getHttpRequest)(context);
35
+ if (!request) {
9
36
  return true;
10
37
  }
11
- const requiredPermissions = permissions.split('|');
12
- if (requiredPermissions.length < 1) {
38
+ const isAnonymousAllowed = (_a = this.reflector) === null || _a === void 0 ? void 0 : _a.getAllAndOverride(allow_anonymous_decorator_1.ALLOW_ANONYMOUS_META_KEY, [context.getHandler(), context.getClass()]);
39
+ if (isAnonymousAllowed) {
13
40
  return true;
14
41
  }
42
+ const requiredPermissions = parseRequiredPermissions(permissions);
43
+ if (requiredPermissions.length === 0) {
44
+ return false;
45
+ }
15
46
  const user = request.user;
16
- const userPermissions = user === null || user === void 0 ? void 0 : user.permissions;
17
- const isAllowed = requiredPermissions.some((requiredPermission) => userPermissions === null || userPermissions === void 0 ? void 0 : userPermissions.find((x) => x === requiredPermission.trim()));
18
- return isAllowed;
47
+ if (!user || !(0, jwt_request_helper_1.isJwtVerified)(request)) {
48
+ return false;
49
+ }
50
+ const userPermissions = user.permissions;
51
+ return requiredPermissions.some((requiredPermission) => userPermissions === null || userPermissions === void 0 ? void 0 : userPermissions.find((x) => x === requiredPermission));
19
52
  }
20
- }
53
+ };
54
+ PermissionsGuardMixin = __decorate([
55
+ (0, common_1.Injectable)(),
56
+ __metadata("design:paramtypes", [core_1.Reflector])
57
+ ], PermissionsGuardMixin);
21
58
  return PermissionsGuardMixin;
22
59
  }
23
60
  //# sourceMappingURL=permissions.guard.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"permissions.guard.js","sourceRoot":"","sources":["../../src/guards/permissions.guard.ts"],"names":[],"mappings":";;;AAEA,0BAAiC,WAAmB;IAClD,MAAM,qBAAqB;QACzB,WAAW,CAAC,OAAyB;YACnC,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;YAEpD,IAAI,CAAC,WAAW,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;gBACxC,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,mBAAmB,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnD,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACnC,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,IAAI,GAAQ,OAAO,CAAC,IAAI,CAAC;YAC/B,MAAM,eAAe,GAAG,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,WAAW,CAAC;YAE1C,MAAM,SAAS,GAAG,mBAAmB,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,EAAE,CAChE,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,IAAI,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,KAAK,kBAAkB,CAAC,IAAI,EAAE,CAAC,CACtE,CAAC;YAEF,OAAO,SAAS,CAAC;QACnB,CAAC;KACF;IAED,OAAO,qBAAqB,CAAC;AAC/B,CAAC"}
1
+ {"version":3,"file":"permissions.guard.js","sourceRoot":"","sources":["../../src/guards/permissions.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AACxB,uCAAyC;AACzC,uFAAmF;AACnF,qEAA6E;AAE7E,SAAS,wBAAwB,CAAC,WAAoB;IACpD,MAAM,OAAO,GAAG,CAAC,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,OAAO;SACX,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;SAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACvC,CAAC;AAED,0BAAiC,WAAoB;IACnD,IACM,qBAAqB,GAD3B,MACM,qBAAqB;QACzB,YAA6B,SAAoB;6BAApB,SAAS;QAAc,CAAC;QAErD,WAAW,CAAC,OAAyB;;YACnC,MAAM,OAAO,GAAG,IAAA,mCAAc,EAAC,OAAO,CAAC,CAAC;YACxC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,kBAAkB,GAAG,MAAA,IAAI,CAAC,SAAS,0CAAE,iBAAiB,CAC1D,oDAAwB,EACxB,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAC3C,CAAC;YACF,IAAI,kBAAkB,EAAE,CAAC;gBACvB,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,mBAAmB,GAAG,wBAAwB,CAAC,WAAW,CAAC,CAAC;YAClE,IAAI,mBAAmB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACrC,OAAO,KAAK,CAAC;YACf,CAAC;YAED,MAAM,IAAI,GAAQ,OAAO,CAAC,IAAI,CAAC;YAC/B,IAAI,CAAC,IAAI,IAAI,CAAC,IAAA,kCAAa,EAAC,OAAO,CAAC,EAAE,CAAC;gBACrC,OAAO,KAAK,CAAC;YACf,CAAC;YAED,MAAM,eAAe,GAAG,IAAI,CAAC,WAAW,CAAC;YAEzC,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,EAAE,CACrD,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,IAAI,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,KAAK,kBAAkB,CAAC,CAC/D,CAAC;QACJ,CAAC;KACF,CAAA;IAjCK,qBAAqB;QAD1B,IAAA,mBAAU,GAAE;yCAE6B,gBAAS;OAD7C,qBAAqB,CAiC1B;IAED,OAAO,qBAAqB,CAAC;AAC/B,CAAC"}
@@ -2,13 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VendorGuard = VendorGuard;
4
4
  const common_1 = require("@nestjs/common");
5
+ const hasOrganizationId = (organization) => {
6
+ if ((organization === null || organization === void 0 ? void 0 : organization.id) == null) {
7
+ return false;
8
+ }
9
+ return String(organization.id).trim() !== '';
10
+ };
11
+ const isVendorRegistered = (user) => {
12
+ const organization = user === null || user === void 0 ? void 0 : user.organization;
13
+ if (!hasOrganizationId(organization)) {
14
+ return false;
15
+ }
16
+ return false;
17
+ };
5
18
  function VendorGuard() {
6
19
  class VendorGuardMixin {
7
20
  canActivate(context) {
8
21
  const request = context.switchToHttp().getRequest();
9
22
  const user = request.user;
10
- const organization = user === null || user === void 0 ? void 0 : user.organization;
11
- if ((organization === null || organization === void 0 ? void 0 : organization.id) != null) {
23
+ if (isVendorRegistered(user)) {
12
24
  return true;
13
25
  }
14
26
  throw new common_1.ForbiddenException('vendor_registration_not_completed');
@@ -1 +1 @@
1
- {"version":3,"file":"vendor.guard.js","sourceRoot":"","sources":["../../src/guards/vendor.guard.ts"],"names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YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;YACpD,MAAM,IAAI,GAAQ,OAAO,CAAC,IAAI,CAAC;YAE/B,IAAI,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC7B,OAAO,IAAI,CAAC;YACd,CAAC;YAED,MAAM,IAAI,2BAAkB,CAAC,mCAAmC,CAAC,CAAC;QACpE,CAAC;KACF;IAED,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
@@ -4,4 +4,5 @@ export type WebhookSignatureGuardOptions = {
4
4
  headerName?: string;
5
5
  algorithm?: 'sha256' | 'sha512';
6
6
  };
7
+ export declare function resolveWebhookRawBody(rawBody: unknown): string | Buffer;
7
8
  export declare function WebhookSignatureGuard(options?: WebhookSignatureGuardOptions): Type<CanActivate>;
@@ -6,9 +6,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.resolveWebhookRawBody = resolveWebhookRawBody;
9
10
  exports.WebhookSignatureGuard = WebhookSignatureGuard;
10
11
  const common_1 = require("@nestjs/common");
11
12
  const crypto_1 = require("crypto");
13
+ const jwt_request_helper_1 = require("../helper/jwt-request.helper");
14
+ function resolveWebhookRawBody(rawBody) {
15
+ if (typeof rawBody === 'string' || Buffer.isBuffer(rawBody)) {
16
+ return rawBody;
17
+ }
18
+ throw new common_1.UnauthorizedException('webhook_raw_body_required');
19
+ }
12
20
  function WebhookSignatureGuard(options = {}) {
13
21
  var _a, _b, _c;
14
22
  const secretEnvVar = (_a = options.secretEnvVar) !== null && _a !== void 0 ? _a : 'WEBHOOK_SIGNING_SECRET';
@@ -17,8 +25,10 @@ function WebhookSignatureGuard(options = {}) {
17
25
  let WebhookSignatureGuardMixin = class WebhookSignatureGuardMixin {
18
26
  canActivate(context) {
19
27
  var _a;
20
- var _b;
21
- const req = context.switchToHttp().getRequest();
28
+ const req = (0, jwt_request_helper_1.getHttpRequest)(context);
29
+ if (!req) {
30
+ return true;
31
+ }
22
32
  const secret = process.env[secretEnvVar];
23
33
  if (!secret) {
24
34
  throw new common_1.UnauthorizedException('webhook_secret_not_configured');
@@ -27,12 +37,7 @@ function WebhookSignatureGuard(options = {}) {
27
37
  if (!provided || typeof provided !== 'string') {
28
38
  throw new common_1.UnauthorizedException('webhook_signature_missing');
29
39
  }
30
- const rawBody = (_b = req.rawBody) !== null && _b !== void 0 ? _b : req.body;
31
- const payload = typeof rawBody === 'string'
32
- ? rawBody
33
- : Buffer.isBuffer(rawBody)
34
- ? rawBody
35
- : JSON.stringify(rawBody !== null && rawBody !== void 0 ? rawBody : {});
40
+ const payload = resolveWebhookRawBody(req.rawBody);
36
41
  const expected = (0, crypto_1.createHmac)(algorithm, secret)
37
42
  .update(payload)
38
43
  .digest('hex');
@@ -1 +1 @@
1
- {"version":3,"file":"webhook-signature.guard.js","sourceRoot":"","sources":["../../src/guards/webhook-signature.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAMwB;AACxB,mCAAqD;AAQrD,+BACE,OAAO,GAAiC,EAAE;;IAE1C,MAAM,YAAY,SAAG,OAAO,CAAC,YAAY,mCAAI,wBAAwB,CAAC;IACtE,MAAM,UAAU,GAAG,OAAC,OAAO,CAAC,UAAU,mCAAI,qBAAqB,CAAC,CAAC,WAAW,EAAE,CAAC;IAC/E,MAAM,SAAS,SAAG,OAAO,CAAC,SAAS,mCAAI,QAAQ,CAAC;IAGhD,IAAM,0BAA0B,GAAhC,MAAM,0BAA0B;QAC9B,WAAW,CAAC,OAAyB;;;YACnC,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;YAChD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;YACzC,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,MAAM,IAAI,8BAAqB,CAAC,+BAA+B,CAAC,CAAC;YACnE,CAAC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E,GAAW,CAAC,OAAO,CAAC,CAAC;YAE5D,MAAM,QAAQ,GAAG,IAAA,mBAAU,EAAC,SAAS,EAAE,MAAM,CAAC;iBAC3C,MAAM,CAAC,OAAO,CAAC;iBACf,MAAM,CAAC,KAAK,CAAC,CAAC;YAEjB,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC7C,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC7C,IAAI,cAAc,CAAC,MAAM,KAAK,cAAc,CAAC,MAAM,EAAE,CAAC;gBACpD,MAAM,IAAI,8BAAqB,CAAC,2BAA2B,CAAC,CAAC;YAC/D,CAAC;YACD,IAAI,CAAC,IAAA,wBAAe,EAAC,cAAc,EAAE,cAAc,CAAC,EAAE,CAAC;gBACrD,MAAM,IAAI,8BAAqB,CAAC,2BAA2B,CAAC,CAAC;YAC/D,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAA;IAlCK,0BAA0B;QAD/B,IAAA,mBAAU,GAAE;OACP,0BAA0B,CAkC/B;IAED,OAAO,0BAA0B,CAAC;AACpC,CAAC"}
@@ -48,6 +48,7 @@ const jwt_1 = require("@nestjs/jwt");
48
48
  const bcrypt = __importStar(require("bcryptjs"));
49
49
  const crypto_1 = require("crypto");
50
50
  const jwt_options_helper_1 = require("./jwt-options.helper");
51
+ const escape_html_helper_1 = require("./escape-html.helper");
51
52
  let AuthHelper = class AuthHelper {
52
53
  constructor(jwt) {
53
54
  this.jwt = jwt;
@@ -99,6 +100,10 @@ let AuthHelper = class AuthHelper {
99
100
  return randomNumber.toString();
100
101
  }
101
102
  verifyEmailTemplateForOtp(fullName, username, otp, duration) {
103
+ const safeName = (0, escape_html_helper_1.escapeHtml)(fullName);
104
+ const safeUsername = (0, escape_html_helper_1.escapeHtml)(username);
105
+ const safeOtp = (0, escape_html_helper_1.escapeHtml)(otp);
106
+ const safeDuration = (0, escape_html_helper_1.escapeHtml)(duration);
102
107
  return `<!DOCTYPE html>
103
108
  <html lang="en">
104
109
  <head>
@@ -155,7 +160,7 @@ let AuthHelper = class AuthHelper {
155
160
  font-weight: 500;
156
161
  "
157
162
  >
158
- Hey ${fullName},
163
+ Hey ${safeName},
159
164
  </p>
160
165
  <p
161
166
  style="
@@ -168,7 +173,7 @@ let AuthHelper = class AuthHelper {
168
173
  Use the following OTP
169
174
  to complete the procedure to verify your email address. OTP is
170
175
  valid for
171
- <span style="font-weight: 600; color: #1f1f1f;">${duration} minutes</span>.
176
+ <span style="font-weight: 600; color: #1f1f1f;">${safeDuration} minutes</span>.
172
177
  Do not share this code with others.
173
178
  </p>
174
179
  <p
@@ -189,7 +194,7 @@ let AuthHelper = class AuthHelper {
189
194
  font-weight: 600;
190
195
  "
191
196
  >
192
- ${username}
197
+ ${safeUsername}
193
198
  </p>
194
199
  <p
195
200
  style="
@@ -211,7 +216,7 @@ let AuthHelper = class AuthHelper {
211
216
  background-color: #0d7801;
212
217
  "
213
218
  >
214
- ${otp}
219
+ ${safeOtp}
215
220
  </p>
216
221
  </div>
217
222
  </div>
@@ -1 +1 @@
1
- {"version":3,"file":"auth.helper.js","sourceRoot":"","sources":["../../src/helper/auth.helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAmE;AACnE,qCAAyC;AACzC,MAAY,MAAM,qCAAiB;AACnC,mCAAmC;AAEnC,6DAG8B;AAGvB,IAAM,UAAU,GAAhB,MAAM,UAAU;IACrB,YAA6B,GAAe;mBAAf,GAAG;IAAe,CAAC;IAEzC,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,MAAc;QAC/C,MAAM,OAAO,GAAQ,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAA,0CAAqB,EAAC,MAAM,CAAC,CAAC,CAAC;QAE3E,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,8BAAq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AG,CAAC,CAAC;QAChC,MAAM,YAAY,GAAG,IAAA,+BAAU,EAAC,QAAQ,CAAC,CAAC;QAE1C,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oBAwDS,QAAQ;;;;;;;;;;;;;gEAaoC,YAAY;;;;;;;;;;;;;;;;;;;;;gBAqB5D,YAAY;;;;;;;;;;;;;;;;;;;;;;gBAsBZ,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqGtB,CAAC;IACA,CAAC;CACF,CAAA;;qBArTY,UAAU;IADtB,IAAA,mBAAU,GAAE;qCAEuB,gBAAU;GADjC,UAAU,CAqTtB"}
@@ -0,0 +1 @@
1
+ export declare function escapeHtml(value: unknown): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.escapeHtml = escapeHtml;
4
+ function escapeHtml(value) {
5
+ return String(value !== null && value !== void 0 ? value : '')
6
+ .replace(/&/g, '&amp;')
7
+ .replace(/</g, '&lt;')
8
+ .replace(/>/g, '&gt;')
9
+ .replace(/"/g, '&quot;')
10
+ .replace(/'/g, '&#39;');
11
+ }
12
+ //# sourceMappingURL=escape-html.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"escape-html.helper.js","sourceRoot":"","sources":["../../src/helper/escape-html.helper.ts"],"names":[],"mappings":";;;AACA,oBAA2B,KAAc;IACvC,OAAO,MAAM,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import { Request } from 'express';
3
+ export declare const JWT_VERIFIED_SYMBOL: unique symbol;
4
+ export declare function markJwtVerified(request: any): void;
5
+ export declare function isJwtVerified(request: any): boolean;
6
+ export declare function getHttpRequest(context: ExecutionContext): Request | undefined;
7
+ export declare function extractBearerToken(request: Request): string | undefined;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JWT_VERIFIED_SYMBOL = void 0;
4
+ exports.markJwtVerified = markJwtVerified;
5
+ exports.isJwtVerified = isJwtVerified;
6
+ exports.getHttpRequest = getHttpRequest;
7
+ exports.extractBearerToken = extractBearerToken;
8
+ exports.JWT_VERIFIED_SYMBOL = Symbol.for('zola.auth.verified');
9
+ function markJwtVerified(request) {
10
+ if (request) {
11
+ request[exports.JWT_VERIFIED_SYMBOL] = true;
12
+ }
13
+ }
14
+ function isJwtVerified(request) {
15
+ return (request === null || request === void 0 ? void 0 : request[exports.JWT_VERIFIED_SYMBOL]) === true;
16
+ }
17
+ function getHttpRequest(context) {
18
+ if (context.getType() !== 'http') {
19
+ return undefined;
20
+ }
21
+ return context.switchToHttp().getRequest();
22
+ }
23
+ function extractBearerToken(request) {
24
+ var _a, _b;
25
+ var _c;
26
+ const [type, token] = (_c = (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a.authorization) === null || _b === void 0 ? void 0 : _b.split(' ')) !== null && _c !== void 0 ? _c : [];
27
+ return type === 'Bearer' ? token : undefined;
28
+ }
29
+ //# sourceMappingURL=jwt-request.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-request.helper.js","sourceRoot":"","sources":["../../src/helper/jwt-request.helper.ts"],"names":[],"mappings":";;;;;;;AAIa,QAAA,mBAAmB,GAAG,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;AAEpE,yBAAgC,OAAY;IAC1C,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,CAAC,QAAA,mBAAmB,CAAC,GAAG,IAAI,CAAC;IACtC,CAAC;AACH,CAAC;AAED,uBAA8B,OAAY;IACxC,OAAO,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAG,QAAA,mBAAmB,CAAC,MAAK,IAAI,CAAC;AACjD,CAAC;AAED,wBAA+B,OAAyB;IACtD,IAAI,OAAO,CAAC,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;QACjC,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;AAC7C,CAAC;AAED,4BAAmC,OAAgB;;;IACjD,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,SAAG,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAE,aAAa,0CAAE,KAAK,CAAC,GAAG,CAAC,mCAAI,EAAE,CAAC;IACvE,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/C,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export type ValidateJwtEnvOptions = {
2
2
  required?: boolean;
3
+ minSecretLength?: number;
3
4
  };
4
5
  export declare function validateJwtEnv(options?: ValidateJwtEnvOptions): void;
@@ -1,16 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateJwtEnv = validateJwtEnv;
4
+ const DEFAULT_MIN_SECRET_LENGTH = 32;
5
+ function assertNonEmpty(name, value) {
6
+ if (!(value === null || value === void 0 ? void 0 : value.trim())) {
7
+ throw new Error(`${name} is required`);
8
+ }
9
+ return value.trim();
10
+ }
11
+ function assertMinLength(name, value, minLength) {
12
+ if (value.length < minLength) {
13
+ throw new Error(`${name} must be at least ${minLength} characters`);
14
+ }
15
+ }
4
16
  function validateJwtEnv(options = {}) {
5
- const { required = true } = options;
17
+ const { required = true, minSecretLength = DEFAULT_MIN_SECRET_LENGTH } = options;
6
18
  if (!required) {
7
19
  return;
8
20
  }
9
- if (!process.env.JWT_ACCESS_TOKEN_SECRET) {
10
- throw new Error('JWT_ACCESS_TOKEN_SECRET is required');
11
- }
12
- if (!process.env.JWT_REFRESH_TOKEN_SECRET) {
13
- throw new Error('JWT_REFRESH_TOKEN_SECRET is required');
21
+ const accessSecret = assertNonEmpty('JWT_ACCESS_TOKEN_SECRET', process.env.JWT_ACCESS_TOKEN_SECRET);
22
+ const refreshSecret = assertNonEmpty('JWT_REFRESH_TOKEN_SECRET', process.env.JWT_REFRESH_TOKEN_SECRET);
23
+ assertMinLength('JWT_ACCESS_TOKEN_SECRET', accessSecret, minSecretLength);
24
+ assertMinLength('JWT_REFRESH_TOKEN_SECRET', refreshSecret, minSecretLength);
25
+ if (accessSecret === refreshSecret) {
26
+ throw new Error('JWT_ACCESS_TOKEN_SECRET and JWT_REFRESH_TOKEN_SECRET must differ');
14
27
  }
28
+ assertNonEmpty('JWT_ACCESS_TOKEN_EXPIRES', process.env.JWT_ACCESS_TOKEN_EXPIRES);
29
+ assertNonEmpty('JWT_REFRESH_TOKEN_EXPIRES', process.env.JWT_REFRESH_TOKEN_EXPIRES);
15
30
  }
16
31
  //# sourceMappingURL=validate-jwt-env.helper.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate-jwt-env.helper.js","sourceRoot":"","sources":["../../src/helper/validate-jwt-env.helper.ts"],"names":[],"mappings":";;;AAKA,wBAA+B,OAAO,GAA0B,EAAE;IAChE,MAAM,EAAE,QAAQ,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IACpC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO;IACT,CAAC;IAED,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;IACzD,CAAC;IACD,IAAI,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,KAAK,aAAa,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,cAAc,CACZ,0BAA0B,EAC1B,OAAO,CAAC,GAAG,CAAC,wBAAwB,CACrC,CAAC;IACF,cAAc,CACZ,2BAA2B,EAC3B,OAAO,CAAC,GAAG,CAAC,yBAAyB,CACtC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export * from './strategy/jwt.strategy';
14
14
  export * from './models/auth.model';
15
15
  export * from './helper/jwt.type';
16
16
  export * from './helper/jwt-options.helper';
17
+ export * from './helper/jwt-request.helper';
17
18
  export * from './helper/validate-jwt-env.helper';
18
19
  export * from './helper/get-client-ip.helper';
19
20
  export * from './authorization-error-response.type';
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __exportStar(require("./strategy/jwt.strategy"), exports);
33
33
  __exportStar(require("./models/auth.model"), exports);
34
34
  __exportStar(require("./helper/jwt.type"), exports);
35
35
  __exportStar(require("./helper/jwt-options.helper"), exports);
36
+ __exportStar(require("./helper/jwt-request.helper"), exports);
36
37
  __exportStar(require("./helper/validate-jwt-env.helper"), exports);
37
38
  __exportStar(require("./helper/get-client-ip.helper"), exports);
38
39
  __exportStar(require("./authorization-error-response.type"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+DAA6D;AAApD,2HAAA,mBAAmB,OAAA;AAC5B,gDAA8C;AAArC,qGAAA,QAAQ,OAAA;AACjB,yEAAuD;AACvD,sEAAoD;AACpD,6DAA2C;AAC3C,uDAAqC;AACrC,6DAA2C;AAC3C,yDAAuC;AACvC,mEAAiD;AACjD,wDAAsC;AACtC,8DAA4C;AAC5C,wEAAsD;AACtD,0DAAwC;AACxC,sDAAoC;AACpC,oDAAkC;AAClC,8DAA4C;AAC5C,mEAAiD;AACjD,gEAA8C;AAC9C,sEAAoD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+DAA6D;AAApD,2HAAA,mBAAmB,OAAA;AAC5B,gDAA8C;AAArC,qGAAA,QAAQ,OAAA;AACjB,yEAAuD;AACvD,sEAAoD;AACpD,6DAA2C;AAC3C,uDAAqC;AACrC,6DAA2C;AAC3C,yDAAuC;AACvC,mEAAiD;AACjD,wDAAsC;AACtC,8DAA4C;AAC5C,wEAAsD;AACtD,0DAAwC;AACxC,sDAAoC;AACpC,oDAAkC;AAClC,8DAA4C;AAC5C,8DAA4C;AAC5C,mEAAiD;AACjD,gEAA8C;AAC9C,sEAAoD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zola_do/authorization",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "JWT auth, guards, strategies for NestJS",
5
5
  "author": "zolaDO",
6
6
  "license": "ISC",