nestjs-authentication 0.4.2 → 0.5.0

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 (197) hide show
  1. package/CHANGELOG.md +77 -2
  2. package/README.md +108 -0
  3. package/SECURITY.md +15 -0
  4. package/dist/classes/email.password.user.d.ts +13 -0
  5. package/dist/classes/email.password.user.d.ts.map +1 -0
  6. package/dist/classes/email.password.user.js +3 -0
  7. package/dist/classes/email.password.user.js.map +1 -0
  8. package/dist/constants.d.ts +13 -0
  9. package/dist/constants.d.ts.map +1 -1
  10. package/dist/constants.js +14 -1
  11. package/dist/constants.js.map +1 -1
  12. package/dist/context/auth.context.d.ts +38 -0
  13. package/dist/context/auth.context.d.ts.map +1 -0
  14. package/dist/context/auth.context.js +39 -0
  15. package/dist/context/auth.context.js.map +1 -0
  16. package/dist/context/decorators.d.ts +25 -0
  17. package/dist/context/decorators.d.ts.map +1 -0
  18. package/dist/context/decorators.js +38 -0
  19. package/dist/context/decorators.js.map +1 -0
  20. package/dist/context/index.d.ts +5 -0
  21. package/dist/context/index.d.ts.map +1 -0
  22. package/dist/context/index.js +21 -0
  23. package/dist/context/index.js.map +1 -0
  24. package/dist/context/interceptor.d.ts +15 -0
  25. package/dist/context/interceptor.d.ts.map +1 -0
  26. package/dist/context/interceptor.js +51 -0
  27. package/dist/context/interceptor.js.map +1 -0
  28. package/dist/context/service.d.ts +18 -0
  29. package/dist/context/service.d.ts.map +1 -0
  30. package/dist/context/service.js +45 -0
  31. package/dist/context/service.js.map +1 -0
  32. package/dist/encryptor.d.ts +20 -0
  33. package/dist/encryptor.d.ts.map +1 -1
  34. package/dist/encryptor.js +36 -6
  35. package/dist/encryptor.js.map +1 -1
  36. package/dist/encryptors/index.d.ts +3 -0
  37. package/dist/encryptors/index.d.ts.map +1 -0
  38. package/dist/encryptors/index.js +19 -0
  39. package/dist/encryptors/index.js.map +1 -0
  40. package/dist/encryptors/migrating.d.ts +28 -0
  41. package/dist/encryptors/migrating.d.ts.map +1 -0
  42. package/dist/encryptors/migrating.js +72 -0
  43. package/dist/encryptors/migrating.js.map +1 -0
  44. package/dist/encryptors/scrypt.d.ts +42 -0
  45. package/dist/encryptors/scrypt.d.ts.map +1 -0
  46. package/dist/encryptors/scrypt.js +85 -0
  47. package/dist/encryptors/scrypt.js.map +1 -0
  48. package/dist/errors.d.ts +32 -0
  49. package/dist/errors.d.ts.map +1 -0
  50. package/dist/errors.js +63 -0
  51. package/dist/errors.js.map +1 -0
  52. package/dist/guard.d.ts +115 -15
  53. package/dist/guard.d.ts.map +1 -1
  54. package/dist/guard.js +302 -90
  55. package/dist/guard.js.map +1 -1
  56. package/dist/index.d.ts +10 -4
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +15 -5
  59. package/dist/index.js.map +1 -1
  60. package/dist/injection.d.ts +3 -0
  61. package/dist/injection.d.ts.map +1 -0
  62. package/dist/injection.js +9 -0
  63. package/dist/injection.js.map +1 -0
  64. package/dist/interfaces/auth.user.d.ts +16 -5
  65. package/dist/interfaces/auth.user.d.ts.map +1 -1
  66. package/dist/interfaces/auth.user.js +6 -0
  67. package/dist/interfaces/auth.user.js.map +1 -1
  68. package/dist/interfaces/encrypt.d.ts +29 -1
  69. package/dist/interfaces/encrypt.d.ts.map +1 -1
  70. package/dist/interfaces/options.d.ts +52 -5
  71. package/dist/interfaces/options.d.ts.map +1 -1
  72. package/dist/interfaces/provider.d.ts +26 -3
  73. package/dist/interfaces/provider.d.ts.map +1 -1
  74. package/dist/module.d.ts +48 -6
  75. package/dist/module.d.ts.map +1 -1
  76. package/dist/module.js +153 -50
  77. package/dist/module.js.map +1 -1
  78. package/dist/providers/auth.provider.d.ts +12 -4
  79. package/dist/providers/auth.provider.d.ts.map +1 -1
  80. package/dist/providers/auth.provider.js +7 -1
  81. package/dist/providers/auth.provider.js.map +1 -1
  82. package/dist/providers/email.password.auth.provider.d.ts +75 -0
  83. package/dist/providers/email.password.auth.provider.d.ts.map +1 -0
  84. package/dist/providers/email.password.auth.provider.js +120 -0
  85. package/dist/providers/email.password.auth.provider.js.map +1 -0
  86. package/dist/providers/memory.user.provider.d.ts +39 -6
  87. package/dist/providers/memory.user.provider.d.ts.map +1 -1
  88. package/dist/providers/memory.user.provider.js +70 -15
  89. package/dist/providers/memory.user.provider.js.map +1 -1
  90. package/dist/scopes/decorators.d.ts +41 -0
  91. package/dist/scopes/decorators.d.ts.map +1 -0
  92. package/dist/scopes/decorators.js +44 -0
  93. package/dist/scopes/decorators.js.map +1 -0
  94. package/dist/scopes/index.d.ts +4 -0
  95. package/dist/scopes/index.d.ts.map +1 -0
  96. package/dist/scopes/index.js +20 -0
  97. package/dist/scopes/index.js.map +1 -0
  98. package/dist/scopes/service.d.ts +32 -0
  99. package/dist/scopes/service.d.ts.map +1 -0
  100. package/dist/scopes/service.js +90 -0
  101. package/dist/scopes/service.js.map +1 -0
  102. package/dist/scopes/utils.d.ts +8 -0
  103. package/dist/scopes/utils.d.ts.map +1 -0
  104. package/dist/scopes/utils.js +29 -0
  105. package/dist/scopes/utils.js.map +1 -0
  106. package/dist/service.d.ts +25 -6
  107. package/dist/service.d.ts.map +1 -1
  108. package/dist/service.js +36 -5
  109. package/dist/service.js.map +1 -1
  110. package/dist/session/index.d.ts +3 -0
  111. package/dist/session/index.d.ts.map +1 -0
  112. package/dist/session/index.js +19 -0
  113. package/dist/session/index.js.map +1 -0
  114. package/dist/session/manager.d.ts +32 -0
  115. package/dist/session/manager.d.ts.map +1 -0
  116. package/dist/session/manager.js +131 -0
  117. package/dist/session/manager.js.map +1 -0
  118. package/dist/session/request.d.ts +14 -0
  119. package/dist/session/request.d.ts.map +1 -0
  120. package/dist/session/request.js +59 -0
  121. package/dist/session/request.js.map +1 -0
  122. package/dist/strategies/emailpassword.d.ts +20 -7
  123. package/dist/strategies/emailpassword.d.ts.map +1 -1
  124. package/dist/strategies/emailpassword.js +42 -19
  125. package/dist/strategies/emailpassword.js.map +1 -1
  126. package/dist/strategies/extract.d.ts +23 -0
  127. package/dist/strategies/extract.d.ts.map +1 -0
  128. package/dist/strategies/extract.js +57 -0
  129. package/dist/strategies/extract.js.map +1 -0
  130. package/dist/strategies/index.d.ts +8 -0
  131. package/dist/strategies/index.d.ts.map +1 -0
  132. package/dist/strategies/index.js +24 -0
  133. package/dist/strategies/index.js.map +1 -0
  134. package/dist/strategies/interfaces.d.ts +65 -0
  135. package/dist/strategies/interfaces.d.ts.map +1 -0
  136. package/dist/{classes/memory.user.js → strategies/interfaces.js} +1 -1
  137. package/dist/strategies/interfaces.js.map +1 -0
  138. package/dist/strategies/jwt.d.ts +87 -15
  139. package/dist/strategies/jwt.d.ts.map +1 -1
  140. package/dist/strategies/jwt.js +140 -9
  141. package/dist/strategies/jwt.js.map +1 -1
  142. package/dist/strategies/passport.d.ts +22 -0
  143. package/dist/strategies/passport.d.ts.map +1 -0
  144. package/dist/strategies/passport.js +90 -0
  145. package/dist/strategies/passport.js.map +1 -0
  146. package/dist/strategies/registry.d.ts +33 -0
  147. package/dist/strategies/registry.d.ts.map +1 -0
  148. package/dist/strategies/registry.js +67 -0
  149. package/dist/strategies/registry.js.map +1 -0
  150. package/dist/strategies/session.d.ts +18 -0
  151. package/dist/strategies/session.d.ts.map +1 -0
  152. package/dist/strategies/session.js +79 -0
  153. package/dist/strategies/session.js.map +1 -0
  154. package/dist/transports/graphql.d.ts +28 -0
  155. package/dist/transports/graphql.d.ts.map +1 -0
  156. package/dist/transports/graphql.js +59 -0
  157. package/dist/transports/graphql.js.map +1 -0
  158. package/dist/transports/http.d.ts +13 -0
  159. package/dist/transports/http.d.ts.map +1 -0
  160. package/dist/transports/http.js +31 -0
  161. package/dist/transports/http.js.map +1 -0
  162. package/dist/transports/index.d.ts +8 -0
  163. package/dist/transports/index.d.ts.map +1 -0
  164. package/dist/transports/index.js +24 -0
  165. package/dist/transports/index.js.map +1 -0
  166. package/dist/transports/interfaces.d.ts +36 -0
  167. package/dist/transports/interfaces.d.ts.map +1 -0
  168. package/dist/transports/interfaces.js +3 -0
  169. package/dist/transports/interfaces.js.map +1 -0
  170. package/dist/transports/registry.d.ts +25 -0
  171. package/dist/transports/registry.d.ts.map +1 -0
  172. package/dist/transports/registry.js +55 -0
  173. package/dist/transports/registry.js.map +1 -0
  174. package/dist/transports/rpc.d.ts +23 -0
  175. package/dist/transports/rpc.d.ts.map +1 -0
  176. package/dist/transports/rpc.js +54 -0
  177. package/dist/transports/rpc.js.map +1 -0
  178. package/dist/transports/utils.d.ts +21 -0
  179. package/dist/transports/utils.d.ts.map +1 -0
  180. package/dist/transports/utils.js +86 -0
  181. package/dist/transports/utils.js.map +1 -0
  182. package/dist/transports/ws.d.ts +31 -0
  183. package/dist/transports/ws.d.ts.map +1 -0
  184. package/dist/transports/ws.js +52 -0
  185. package/dist/transports/ws.js.map +1 -0
  186. package/package.json +102 -60
  187. package/dist/classes/memory.user.d.ts +0 -7
  188. package/dist/classes/memory.user.d.ts.map +0 -1
  189. package/dist/classes/memory.user.js.map +0 -1
  190. package/dist/providers/auth.memory.user.provider.d.ts +0 -15
  191. package/dist/providers/auth.memory.user.provider.d.ts.map +0 -1
  192. package/dist/providers/auth.memory.user.provider.js +0 -30
  193. package/dist/providers/auth.memory.user.provider.js.map +0 -1
  194. package/dist/user.serializer.d.ts +0 -10
  195. package/dist/user.serializer.d.ts.map +0 -1
  196. package/dist/user.serializer.js +0 -53
  197. package/dist/user.serializer.js.map +0 -1
@@ -0,0 +1,45 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AuthContextService = void 0;
10
+ const common_1 = require("@nestjs/common");
11
+ const auth_context_1 = require("./auth.context");
12
+ /**
13
+ * Reads the {@link AuthContext} of the current request in any service (no request scope needed).
14
+ */
15
+ let AuthContextService = class AuthContextService {
16
+ /** Returns the auth context, or `undefined` if the request is not authenticated. */
17
+ get() {
18
+ return (0, auth_context_1.getAuthContext)();
19
+ }
20
+ /** Returns the authenticated user, or `undefined`. */
21
+ getUser() {
22
+ var _a;
23
+ return (_a = this.get()) === null || _a === void 0 ? void 0 : _a.user;
24
+ }
25
+ /**
26
+ * Returns the authenticated user.
27
+ *
28
+ * @throws UnauthorizedException if the request is not authenticated
29
+ */
30
+ getUserOrFail() {
31
+ const auth = this.get();
32
+ if (auth === undefined) {
33
+ throw new common_1.UnauthorizedException();
34
+ }
35
+ return auth.user;
36
+ }
37
+ isAuthenticated() {
38
+ return this.get() !== undefined;
39
+ }
40
+ };
41
+ exports.AuthContextService = AuthContextService;
42
+ exports.AuthContextService = AuthContextService = __decorate([
43
+ (0, common_1.Injectable)()
44
+ ], AuthContextService);
45
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/context/service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAmE;AAEnE,iDAA6D;AAE7D;;GAEG;AAEI,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAC3B,oFAAoF;IACpF,GAAG;QACC,OAAO,IAAA,6BAAc,GAAQ,CAAC;IAClC,CAAC;IAED,sDAAsD;IACtD,OAAO;;QACH,OAAO,MAAA,IAAI,CAAC,GAAG,EAAE,0CAAE,IAAI,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,aAAa;QACT,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACxB,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,MAAM,IAAI,8BAAqB,EAAE,CAAC;QACtC,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,eAAe;QACX,OAAO,IAAI,CAAC,GAAG,EAAE,KAAK,SAAS,CAAC;IACpC,CAAC;CACJ,CAAA;AA3BY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;GACA,kBAAkB,CA2B9B"}
@@ -1,7 +1,27 @@
1
1
  import { IEncryptor, IEncryptorOptions } from './interfaces/encrypt';
2
+ /**
3
+ * Encrypts values with aes-256-ctr and a fixed key and iv: the same value always gives the same result,
4
+ * so an encrypted password can be compared with the stored one.
5
+ *
6
+ * Not recommended for new applications: a fixed key and iv leak the length and the common prefixes of the passwords,
7
+ * and anyone with the key can decrypt them. Use {@link ScryptEncryptor}, and {@link MigratingEncryptor} to migrate.
8
+ *
9
+ * This is the default {@link IEncryptor}: provided by the {@link AuthModule} when the `encrypt` option is set,
10
+ * with the `Encryptor` class and, unless the `encryptor` option is set, the {@link ENCRYPTOR} token.
11
+ */
2
12
  export declare class Encryptor implements IEncryptor {
3
13
  protected options: IEncryptorOptions;
14
+ protected readonly key: Buffer;
15
+ protected readonly iv: Buffer;
16
+ /**
17
+ * @throws Error if the key is not 32 bytes or the iv is not 16 bytes (hex strings)
18
+ */
4
19
  constructor(options: IEncryptorOptions);
20
+ /** Returns the encrypted value as a hex string. */
5
21
  encrypt(value: string): string;
22
+ /** Returns `true` if the value is a hex string (the format of the values encrypted by this encryptor). */
23
+ canVerify(encrypted: string): boolean;
24
+ /** Returns `true` if the value encrypted matches `encrypted` (constant time comparison). */
25
+ compare(value: string, encrypted: string): boolean;
6
26
  }
7
27
  //# sourceMappingURL=encryptor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"encryptor.d.ts","sourceRoot":"","sources":["../src/encryptor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAErE,qBACa,SAAU,YAAW,UAAU;IAC5B,SAAS,CAAC,OAAO,EAAE,iBAAiB;gBAA1B,OAAO,EAAE,iBAAiB;IAEhD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAUjC"}
1
+ {"version":3,"file":"encryptor.d.ts","sourceRoot":"","sources":["../src/encryptor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAErE;;;;;;;;;GASG;AACH,qBACa,SAAU,YAAW,UAAU;IAO5B,SAAS,CAAC,OAAO,EAAE,iBAAiB;IANhD,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAC/B,SAAS,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAE9B;;OAEG;gBACmB,OAAO,EAAE,iBAAiB;IAWhD,mDAAmD;IACnD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAK9B,0GAA0G;IAC1G,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIrC,4FAA4F;IAC5F,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO;CAKrD"}
package/dist/encryptor.js CHANGED
@@ -12,20 +12,50 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Encryptor = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const crypto_1 = require("crypto");
15
+ /**
16
+ * Encrypts values with aes-256-ctr and a fixed key and iv: the same value always gives the same result,
17
+ * so an encrypted password can be compared with the stored one.
18
+ *
19
+ * Not recommended for new applications: a fixed key and iv leak the length and the common prefixes of the passwords,
20
+ * and anyone with the key can decrypt them. Use {@link ScryptEncryptor}, and {@link MigratingEncryptor} to migrate.
21
+ *
22
+ * This is the default {@link IEncryptor}: provided by the {@link AuthModule} when the `encrypt` option is set,
23
+ * with the `Encryptor` class and, unless the `encryptor` option is set, the {@link ENCRYPTOR} token.
24
+ */
15
25
  let Encryptor = class Encryptor {
26
+ /**
27
+ * @throws Error if the key is not 32 bytes or the iv is not 16 bytes (hex strings)
28
+ */
16
29
  constructor(options) {
17
30
  this.options = options;
31
+ this.key = Buffer.from(options.key, 'hex');
32
+ this.iv = Buffer.from(options.iv, 'hex');
33
+ if (this.key.length !== 32) {
34
+ throw new Error('Encryptor: the key must be 32 bytes, as a hex string of 64 characters');
35
+ }
36
+ if (this.iv.length !== 16) {
37
+ throw new Error('Encryptor: the iv must be 16 bytes, as a hex string of 32 characters');
38
+ }
18
39
  }
40
+ /** Returns the encrypted value as a hex string. */
19
41
  encrypt(value) {
20
- const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', Buffer.from(this.options.key, 'hex'), Buffer.from(this.options.iv, 'hex'));
21
- let crypted = cipher.update(value, 'utf8', 'hex');
22
- crypted += cipher.final('hex');
23
- return crypted;
42
+ const cipher = (0, crypto_1.createCipheriv)('aes-256-ctr', this.key, this.iv);
43
+ return cipher.update(value, 'utf8', 'hex') + cipher.final('hex');
44
+ }
45
+ /** Returns `true` if the value is a hex string (the format of the values encrypted by this encryptor). */
46
+ canVerify(encrypted) {
47
+ return typeof encrypted === 'string' && /^(?:[0-9a-f]{2})*$/.test(encrypted);
48
+ }
49
+ /** Returns `true` if the value encrypted matches `encrypted` (constant time comparison). */
50
+ compare(value, encrypted) {
51
+ const actual = Buffer.from(this.encrypt(value));
52
+ const expected = Buffer.from(encrypted);
53
+ return actual.length === expected.length && (0, crypto_1.timingSafeEqual)(actual, expected);
24
54
  }
25
55
  };
26
- Encryptor = __decorate([
56
+ exports.Encryptor = Encryptor;
57
+ exports.Encryptor = Encryptor = __decorate([
27
58
  (0, common_1.Injectable)(),
28
59
  __metadata("design:paramtypes", [Object])
29
60
  ], Encryptor);
30
- exports.Encryptor = Encryptor;
31
61
  //# sourceMappingURL=encryptor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"encryptor.js","sourceRoot":"","sources":["../src/encryptor.ts"],"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ACD,IAAI,IAAI,CAAC,EAAE,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;QAC5F,CAAC;IACL,CAAC;IAED,mDAAmD;IACnD,OAAO,CAAC,KAAa;QACjB,MAAM,MAAM,GAAG,IAAA,uBAAc,EAAC,aAAa,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QAChE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACrE,CAAC;IAED,0GAA0G;IAC1G,SAAS,CAAC,SAAiB;QACvB,OAAO,OAAO,SAAS,KAAK,QAAQ,IAAI,oBAAoB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACjF,CAAC;IAED,4FAA4F;IAC5F,OAAO,CAAC,KAAa,EAAE,SAAiB;QACpC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAChD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACxC,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,IAAI,IAAA,wBAAe,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAClF,CAAC;CACJ,CAAA;AAnCY,8BAAS;oBAAT,SAAS;IADrB,IAAA,mBAAU,GAAE;;GACA,SAAS,CAmCrB"}
@@ -0,0 +1,3 @@
1
+ export * from './migrating';
2
+ export * from './scrypt';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/encryptors/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./migrating"), exports);
18
+ __exportStar(require("./scrypt"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/encryptors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,2CAAyB"}
@@ -0,0 +1,28 @@
1
+ import { IEncryptor } from '../interfaces/encrypt';
2
+ /**
3
+ * Migrates the stored passwords to a new encryptor, one user at a time, on login:
4
+ *
5
+ * - new passwords are encrypted with `current`
6
+ * - a stored value is checked with the first encryptor that can verify it (`canVerify()`), `current` first
7
+ * - `needsRehash()` is `true` for the values of the previous encryptors (and the ones `current` wants to rehash),
8
+ * so {@link EmailPasswordAuthProvider} stores the password again with `current` after a successful login
9
+ * (with its `updatePasswordHash` option)
10
+ *
11
+ * ```ts
12
+ * AuthModule.forRoot({
13
+ * encryptor: new MigratingEncryptor(new ScryptEncryptor(), [new Encryptor({ key, iv })])
14
+ * });
15
+ * ```
16
+ */
17
+ export declare class MigratingEncryptor implements IEncryptor {
18
+ protected readonly current: IEncryptor;
19
+ protected readonly legacy: IEncryptor[];
20
+ constructor(current: IEncryptor, legacy: IEncryptor[]);
21
+ encrypt(value: string): string | Promise<string>;
22
+ /** Returns the encryptor of a stored value: the first one that can verify it, `current` if none says so. */
23
+ protected getEncryptor(encrypted: string): IEncryptor | undefined;
24
+ canVerify(encrypted: string): boolean;
25
+ compare(value: string, encrypted: string): Promise<boolean>;
26
+ needsRehash(encrypted: string): boolean;
27
+ }
28
+ //# sourceMappingURL=migrating.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrating.d.ts","sourceRoot":"","sources":["../../src/encryptors/migrating.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,kBAAmB,YAAW,UAAU;IAE7C,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,UAAU;IACtC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,UAAU,EAAE;gBADpB,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,UAAU,EAAE;IAG3C,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhD,4GAA4G;IAC5G,SAAS,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS;IAUjE,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAI/B,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAajE,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAO1C"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MigratingEncryptor = void 0;
13
+ const crypto_1 = require("crypto");
14
+ /**
15
+ * Migrates the stored passwords to a new encryptor, one user at a time, on login:
16
+ *
17
+ * - new passwords are encrypted with `current`
18
+ * - a stored value is checked with the first encryptor that can verify it (`canVerify()`), `current` first
19
+ * - `needsRehash()` is `true` for the values of the previous encryptors (and the ones `current` wants to rehash),
20
+ * so {@link EmailPasswordAuthProvider} stores the password again with `current` after a successful login
21
+ * (with its `updatePasswordHash` option)
22
+ *
23
+ * ```ts
24
+ * AuthModule.forRoot({
25
+ * encryptor: new MigratingEncryptor(new ScryptEncryptor(), [new Encryptor({ key, iv })])
26
+ * });
27
+ * ```
28
+ */
29
+ class MigratingEncryptor {
30
+ constructor(current, legacy) {
31
+ this.current = current;
32
+ this.legacy = legacy;
33
+ }
34
+ encrypt(value) {
35
+ return this.current.encrypt(value);
36
+ }
37
+ /** Returns the encryptor of a stored value: the first one that can verify it, `current` if none says so. */
38
+ getEncryptor(encrypted) {
39
+ var _a, _b, _c, _d;
40
+ if ((_c = (_b = (_a = this.current).canVerify) === null || _b === void 0 ? void 0 : _b.call(_a, encrypted)) !== null && _c !== void 0 ? _c : false) {
41
+ return this.current;
42
+ }
43
+ return ((_d = this.legacy.find((e) => { var _a, _b; return (_b = (_a = e.canVerify) === null || _a === void 0 ? void 0 : _a.call(e, encrypted)) !== null && _b !== void 0 ? _b : false; })) !== null && _d !== void 0 ? _d : (this.current.canVerify ? undefined : this.current));
44
+ }
45
+ canVerify(encrypted) {
46
+ return this.getEncryptor(encrypted) !== undefined;
47
+ }
48
+ compare(value, encrypted) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const encryptor = this.getEncryptor(encrypted);
51
+ if (encryptor === undefined) {
52
+ return false;
53
+ }
54
+ if (typeof encryptor.compare === 'function') {
55
+ return yield encryptor.compare(value, encrypted);
56
+ }
57
+ const actual = Buffer.from(yield encryptor.encrypt(value));
58
+ const expected = Buffer.from(encrypted);
59
+ return actual.length === expected.length && (0, crypto_1.timingSafeEqual)(actual, expected);
60
+ });
61
+ }
62
+ needsRehash(encrypted) {
63
+ var _a, _b, _c;
64
+ const encryptor = this.getEncryptor(encrypted);
65
+ if (encryptor !== this.current) {
66
+ return true;
67
+ }
68
+ return (_c = (_b = (_a = this.current).needsRehash) === null || _b === void 0 ? void 0 : _b.call(_a, encrypted)) !== null && _c !== void 0 ? _c : false;
69
+ }
70
+ }
71
+ exports.MigratingEncryptor = MigratingEncryptor;
72
+ //# sourceMappingURL=migrating.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrating.js","sourceRoot":"","sources":["../../src/encryptors/migrating.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAyC;AAIzC;;;;;;;;;;;;;;GAcG;AACH,MAAa,kBAAkB;IAC3B,YACuB,OAAmB,EACnB,MAAoB;QADpB,YAAO,GAAP,OAAO,CAAY;QACnB,WAAM,GAAN,MAAM,CAAc;IACxC,CAAC;IAEJ,OAAO,CAAC,KAAa;QACjB,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IAED,4GAA4G;IAClG,YAAY,CAAC,SAAiB;;QACpC,IAAI,MAAA,MAAA,MAAA,IAAI,CAAC,OAAO,EAAC,SAAS,mDAAG,SAAS,CAAC,mCAAI,KAAK,EAAE,CAAC;YAC/C,OAAO,IAAI,CAAC,OAAO,CAAC;QACxB,CAAC;QACD,OAAO,CACH,MAAA,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,eAAC,OAAA,MAAA,MAAA,CAAC,CAAC,SAAS,kDAAG,SAAS,CAAC,mCAAI,KAAK,CAAA,EAAA,CAAC,mCAC1D,CAAC,IAAI,C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faces/encrypt';
3
+ /** Options of the {@link ScryptEncryptor}. */
4
+ export interface ScryptEncryptorOptions {
5
+ /** CPU / memory cost, a power of 2. Default `2 ** 16` (64 MiB with `r = 8`). */
6
+ cost?: number;
7
+ /** Block size. Default `8`. */
8
+ blockSize?: number;
9
+ /** Parallelization. Default `1`. */
10
+ parallelization?: number;
11
+ /** Salt length in bytes. Default `16`. */
12
+ saltLength?: number;
13
+ /** Hash length in bytes. Default `32`. */
14
+ keyLength?: number;
15
+ }
16
+ /**
17
+ * Hashes the passwords with scrypt (built in Node.js, no dependency), with a random salt per password.
18
+ *
19
+ * The stored value has the parameters, so they can be raised later (old hashes are still verified, and
20
+ * {@link needsRehash} tells which ones to update): `$scrypt$ln=16,r=8,p=1$<salt>$<hash>` (base64).
21
+ */
22
+ export declare class ScryptEncryptor implements IEncryptor {
23
+ protected readonly cost: number;
24
+ protected readonly blockSize: number;
25
+ protected readonly parallelization: number;
26
+ protected readonly saltLength: number;
27
+ protected readonly keyLength: number;
28
+ constructor(options?: ScryptEncryptorOptions);
29
+ protected scryptOptions(cost: number, blockSize: number, parallelization: number): ScryptOptions;
30
+ encrypt(value: string): Promise<string>;
31
+ protected parse(encrypted: string): {
32
+ cost: number;
33
+ blockSize: number;
34
+ parallelization: number;
35
+ salt: Buffer<ArrayBuffer>;
36
+ hash: Buffer<ArrayBuffer>;
37
+ } | undefined;
38
+ canVerify(encrypted: string): boolean;
39
+ compare(value: string, encrypted: string): Promise<boolean>;
40
+ needsRehash(encrypted: string): boolean;
41
+ }
42
+ //# sourceMappingURL=scrypt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scrypt.d.ts","sourceRoot":"","sources":["../../src/encryptors/scrypt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,aAAa,EAAmB,MAAM,QAAQ,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,8CAA8C;AAC9C,MAAM,WAAW,sBAAsB;IACnC,gFAAgF;IAChF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oCAAoC;IACpC,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,0CAA0C;IAC1C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0CAA0C;IAC1C,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAUD;;;;;GAKG;AACH,qBAAa,eAAgB,YAAW,UAAU;IAC9C,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAChC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IACrC,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IAC3C,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IACtC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;gBAEzB,OAAO,GAAE,sBAA2B;IAWhD,SAAS,CAAC,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,aAAa;IAK1F,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAY7C,SAAS,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM;;;;;;;IAejC,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAI/B,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAcjE,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAU1C"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ScryptEncryptor = void 0;
13
+ const crypto_1 = require("crypto");
14
+ const PREFIX = '$scrypt$';
15
+ function scryptAsync(password, salt, keyLength, options) {
16
+ return new Promise((resolve, reject) => (0, crypto_1.scrypt)(password, salt, keyLength, options, (err, key) => (err ? reject(err) : resolve(key))));
17
+ }
18
+ /**
19
+ * Hashes the passwords with scrypt (built in Node.js, no dependency), with a random salt per password.
20
+ *
21
+ * The stored value has the parameters, so they can be raised later (old hashes are still verified, and
22
+ * {@link needsRehash} tells which ones to update): `$scrypt$ln=16,r=8,p=1$<salt>$<hash>` (base64).
23
+ */
24
+ class ScryptEncryptor {
25
+ constructor(options = {}) {
26
+ var _a, _b, _c, _d, _e;
27
+ this.cost = (_a = options.cost) !== null && _a !== void 0 ? _a : Math.pow(2, 16);
28
+ this.blockSize = (_b = options.blockSize) !== null && _b !== void 0 ? _b : 8;
29
+ this.parallelization = (_c = options.parallelization) !== null && _c !== void 0 ? _c : 1;
30
+ this.saltLength = (_d = options.saltLength) !== null && _d !== void 0 ? _d : 16;
31
+ this.keyLength = (_e = options.keyLength) !== null && _e !== void 0 ? _e : 32;
32
+ if (!Number.isInteger(Math.log2(this.cost)) || this.cost < 2) {
33
+ throw new Error('ScryptEncryptor: the cost must be a power of 2');
34
+ }
35
+ }
36
+ scryptOptions(cost, blockSize, parallelization) {
37
+ // scrypt needs 128 * N * r bytes, more than the default maxmem (32 MiB) from N = 2 ** 15
38
+ return { N: cost, r: blockSize, p: parallelization, maxmem: 256 * cost * blockSize + 1024 * 1024 };
39
+ }
40
+ encrypt(value) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const salt = (0, crypto_1.randomBytes)(this.saltLength);
43
+ const hash = yield scryptAsync(value, salt, this.keyLength, this.scryptOptions(this.cost, this.blockSize, this.parallelization));
44
+ const params = `ln=${Math.log2(this.cost)},r=${this.blockSize},p=${this.parallelization}`;
45
+ return `${PREFIX}${params}$${salt.toString('base64')}$${hash.toString('base64')}`;
46
+ });
47
+ }
48
+ parse(encrypted) {
49
+ const match = /^\$scrypt\$ln=(\d+),r=(\d+),p=(\d+)\$([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]+)$/.exec(encrypted);
50
+ if (match === null) {
51
+ return undefined;
52
+ }
53
+ const [, ln, r, p, salt, hash] = match;
54
+ return {
55
+ cost: Math.pow(2, Number(ln)),
56
+ blockSize: Number(r),
57
+ parallelization: Number(p),
58
+ salt: Buffer.from(salt, 'base64'),
59
+ hash: Buffer.from(hash, 'base64')
60
+ };
61
+ }
62
+ canVerify(encrypted) {
63
+ return typeof encrypted === 'string' && this.parse(encrypted) !== undefined;
64
+ }
65
+ compare(value, encrypted) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const parsed = typeof encrypted === 'string' ? this.parse(encrypted) : undefined;
68
+ if (parsed === undefined || parsed.hash.length === 0) {
69
+ return false;
70
+ }
71
+ const hash = yield scryptAsync(value, parsed.salt, parsed.hash.length, this.scryptOptions(parsed.cost, parsed.blockSize, parsed.parallelization));
72
+ return (0, crypto_1.timingSafeEqual)(hash, parsed.hash);
73
+ });
74
+ }
75
+ needsRehash(encrypted) {
76
+ const parsed = this.parse(encrypted);
77
+ return (parsed === undefined ||
78
+ parsed.cost !== this.cost ||
79
+ parsed.blockSize !== this.blockSize ||
80
+ parsed.parallelization !== this.parallelization ||
81
+ parsed.hash.length !== this.keyLength);
82
+ }
83
+ }
84
+ exports.ScryptEncryptor = ScryptEncryptor;
85
+ //# sourceMappingURL=scrypt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scrypt.js","sourceRoot":"","sources":["../../src/encryptors/scrypt.ts"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@ -0,0 +1,32 @@
1
+ import { ExecutionContext, HttpException } from '@nestjs/common';
2
+ /**
3
+ * Creates the error thrown by the {@link AuthGuard} when a `required` request is not authenticated.
4
+ *
5
+ * @param context the execution context
6
+ * @param transport the name of the transport (`http`, `graphql`, `ws`, `rpc`...)
7
+ */
8
+ export type AuthExceptionFactory = (context: ExecutionContext, transport: string) => Error;
9
+ /**
10
+ * Converts an http exception to the error of the transport: a `WsException` for websockets and an `RpcException`
11
+ * for microservices (if `@nestjs/websockets` / `@nestjs/microservices` are installed), so their exception filters
12
+ * send it to the client. Http and GraphQL keep the http exception (GraphQL drivers format it).
13
+ */
14
+ export declare function toTransportException(exception: HttpException, transport: string): Error;
15
+ /**
16
+ * An {@link AuthExceptionFactory} that throws a 401 Unauthorized, the right status for a request that is not
17
+ * authenticated. With a `challenge`, the http response has a `WWW-Authenticate` header (e.g. `Bearer`).
18
+ *
19
+ * ```ts
20
+ * new AuthGuard({ strategies: { jwt: {} }, required: true, exceptionFactory: unauthorized({ challenge: 'Bearer' }) })
21
+ * ```
22
+ */
23
+ export declare function unauthorized(options?: {
24
+ challenge?: string;
25
+ message?: string;
26
+ }): AuthExceptionFactory;
27
+ /**
28
+ * The default {@link AuthExceptionFactory}: a 403 Forbidden (the error NestJS throws when a guard returns `false`),
29
+ * converted to the error of the transport.
30
+ */
31
+ export declare const forbidden: AuthExceptionFactory;
32
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAsB,aAAa,EAAyB,MAAM,gBAAgB,CAAC;AAE5G;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,KAAK,KAAK,CAAC;AAW3F;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK,CAWvF;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,oBAAoB,CAazG;AAED;;;GAGG;AACH,eAAO,MAAM,SAAS,EAAE,oBACyD,CAAC"}
package/dist/errors.js ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.forbidden = void 0;
4
+ exports.toTransportException = toTransportException;
5
+ exports.unauthorized = unauthorized;
6
+ const common_1 = require("@nestjs/common");
7
+ function loadOptional(name) {
8
+ try {
9
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
10
+ return require(name);
11
+ }
12
+ catch (_a) {
13
+ return undefined;
14
+ }
15
+ }
16
+ /**
17
+ * Converts an http exception to the error of the transport: a `WsException` for websockets and an `RpcException`
18
+ * for microservices (if `@nestjs/websockets` / `@nestjs/microservices` are installed), so their exception filters
19
+ * send it to the client. Http and GraphQL keep the http exception (GraphQL drivers format it).
20
+ */
21
+ function toTransportException(exception, transport) {
22
+ var _a, _b;
23
+ const error = { status: exception.getStatus(), message: exception.message };
24
+ if (transport === 'ws') {
25
+ const WsException = (_a = loadOptional('@nestjs/websockets')) === null || _a === void 0 ? void 0 : _a.WsException;
26
+ return WsException !== undefined ? new WsException(error) : exception;
27
+ }
28
+ if (transport === 'rpc') {
29
+ const RpcException = (_b = loadOptional('@nestjs/microservices')) === null || _b === void 0 ? void 0 : _b.RpcException;
30
+ return RpcException !== undefined ? new RpcException(error) : exception;
31
+ }
32
+ return exception;
33
+ }
34
+ /**
35
+ * An {@link AuthExceptionFactory} that throws a 401 Unauthorized, the right status for a request that is not
36
+ * authenticated. With a `challenge`, the http response has a `WWW-Authenticate` header (e.g. `Bearer`).
37
+ *
38
+ * ```ts
39
+ * new AuthGuard({ strategies: { jwt: {} }, required: true, exceptionFactory: unauthorized({ challenge: 'Bearer' }) })
40
+ * ```
41
+ */
42
+ function unauthorized(options = {}) {
43
+ return (context, transport) => {
44
+ if (options.challenge !== undefined && transport === 'http') {
45
+ const response = context.switchToHttp().getResponse();
46
+ // express: setHeader(), fastify: header()
47
+ if (typeof (response === null || response === void 0 ? void 0 : response.setHeader) === 'function') {
48
+ response.setHeader('WWW-Authenticate', options.challenge);
49
+ }
50
+ else if (typeof (response === null || response === void 0 ? void 0 : response.header) === 'function') {
51
+ response.header('WWW-Authenticate', options.challenge);
52
+ }
53
+ }
54
+ return toTransportException(new common_1.UnauthorizedException(options.message), transport);
55
+ };
56
+ }
57
+ /**
58
+ * The default {@link AuthExceptionFactory}: a 403 Forbidden (the error NestJS throws when a guard returns `false`),
59
+ * converted to the error of the transport.
60
+ */
61
+ const forbidden = (_context, transport) => toTransportException(new common_1.ForbiddenException('Forbidden resource'), transport);
62
+ exports.forbidden = forbidden;
63
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAwBA,oDAWC;AAUD,oCAaC;AA1DD,2CAA4G;AAU5G,SAAS,YAAY,CAAC,IAAY;IAC9B,IAAI,CAAC;QACD,iEAAiE;QACjE,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAAC,WAAM,CAAC;QACL,OAAO,SAAS,CAAC;IACrB,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,SAAgB,oBAAoB,CAAC,SAAwB,EAAE,SAAiB;;IAC5E,MAAM,KAAK,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,SAAS,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE,CAAC;IAC5E,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACrB,MAAM,WAAW,GAAG,MAAA,YAAY,CAAC,oBAAoB,CAAC,0CAAE,WAAW,CAAC;QACpE,OAAO,WAAW,KA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