@pristine-ts/jwt 0.0.139 → 0.0.143

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 (38) hide show
  1. package/dist/lib/cjs/decorators/jwt-payload.decorators.js +20 -1
  2. package/dist/lib/cjs/decorators/jwt-payload.decorators.js.map +1 -1
  3. package/dist/lib/cjs/errors/invalid-jwt.error.js +10 -1
  4. package/dist/lib/cjs/errors/invalid-jwt.error.js.map +1 -1
  5. package/dist/lib/cjs/errors/jwt-authorization-header.error.js +6 -1
  6. package/dist/lib/cjs/errors/jwt-authorization-header.error.js.map +1 -1
  7. package/dist/lib/cjs/guards/jwt-protected.guard.js +12 -0
  8. package/dist/lib/cjs/guards/jwt-protected.guard.js.map +1 -1
  9. package/dist/lib/cjs/jwt.module.js +1 -6
  10. package/dist/lib/cjs/jwt.module.js.map +1 -1
  11. package/dist/lib/cjs/managers/jwt.manager.js +23 -0
  12. package/dist/lib/cjs/managers/jwt.manager.js.map +1 -1
  13. package/dist/lib/cjs/resolvers/jwt-payload-parameter-decorator.resolver.js +20 -2
  14. package/dist/lib/cjs/resolvers/jwt-payload-parameter-decorator.resolver.js.map +1 -1
  15. package/dist/lib/esm/decorators/jwt-payload.decorators.js +20 -1
  16. package/dist/lib/esm/decorators/jwt-payload.decorators.js.map +1 -1
  17. package/dist/lib/esm/errors/invalid-jwt.error.js +10 -1
  18. package/dist/lib/esm/errors/invalid-jwt.error.js.map +1 -1
  19. package/dist/lib/esm/errors/jwt-authorization-header.error.js +6 -1
  20. package/dist/lib/esm/errors/jwt-authorization-header.error.js.map +1 -1
  21. package/dist/lib/esm/guards/jwt-protected.guard.js +12 -0
  22. package/dist/lib/esm/guards/jwt-protected.guard.js.map +1 -1
  23. package/dist/lib/esm/jwt.module.js +1 -6
  24. package/dist/lib/esm/jwt.module.js.map +1 -1
  25. package/dist/lib/esm/managers/jwt.manager.js +23 -0
  26. package/dist/lib/esm/managers/jwt.manager.js.map +1 -1
  27. package/dist/lib/esm/resolvers/jwt-payload-parameter-decorator.resolver.js +21 -3
  28. package/dist/lib/esm/resolvers/jwt-payload-parameter-decorator.resolver.js.map +1 -1
  29. package/dist/types/decorators/jwt-payload.decorators.d.ts +3 -0
  30. package/dist/types/errors/invalid-jwt.error.d.ts +10 -1
  31. package/dist/types/errors/jwt-authorization-header.error.d.ts +6 -1
  32. package/dist/types/guards/jwt-protected.guard.d.ts +12 -0
  33. package/dist/types/interfaces/jwt-manager.interface.d.ts +7 -0
  34. package/dist/types/interfaces/jwt-payload-decorator.interface.d.ts +3 -0
  35. package/dist/types/managers/jwt.manager.d.ts +19 -0
  36. package/dist/types/resolvers/jwt-payload-parameter-decorator.resolver.d.ts +17 -2
  37. package/package.json +5 -5
  38. package/readme.md +1 -0
@@ -1,8 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jwtPayload = void 0;
4
+ /**
5
+ * This decorator can be used to inject the decoded JWT payload of a request in a parameter of a method in a controller.
6
+ */
4
7
  const jwtPayload = () => {
5
- return (target, propertyKey, parameterIndex) => {
8
+ return (
9
+ /**
10
+ * The class on which the decorator is used.
11
+ */
12
+ target,
13
+ /**
14
+ * The method on which the decorator is used.
15
+ */
16
+ propertyKey,
17
+ /**
18
+ * The index of the parameter for which the decorator is used.
19
+ */
20
+ parameterIndex) => {
21
+ // Verify that the object target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"] exists or we create it.
22
+ // This object is a convention defined by Pristine on where to save controller method parameter decorator information and is used in the router to retrieve that information.
6
23
  if (target.constructor.prototype.hasOwnProperty("__metadata__") === false) {
7
24
  target.constructor.prototype["__metadata__"] = {};
8
25
  }
@@ -15,9 +32,11 @@ const jwtPayload = () => {
15
32
  if (target.constructor.prototype["__metadata__"]["methods"][propertyKey].hasOwnProperty("arguments") === false) {
16
33
  target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"] = [];
17
34
  }
35
+ // Set the type of method parameter. Each decorator has it's own type.
18
36
  const methodParameter = {
19
37
  type: "jwtPayload"
20
38
  };
39
+ // Save the method parameter with the proper parameter index (index of the parameter in the list of parameters of a method).
21
40
  target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"][parameterIndex] = methodParameter;
22
41
  };
23
42
  };
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-payload.decorators.js","sourceRoot":"","sources":["../../../../src/decorators/jwt-payload.decorators.ts"],"names":[],"mappings":";;;AAEO,MAAM,UAAU,GAAG,GAAG,EAAE;IAC3B,OAAO,CACH,MAAc,EACd,WAA4B,EAC5B,cAAsB,EACxB,EAAE;QACA,IAAI,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,cAAc,CAAC,KAAK,KAAK,EAAE;YACvE,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,EAAE,CAAA;SACpD;QAED,IAAI,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,cAAc,CAAC,SAAS,CAAC,KAAK,KAAK,EAAE;YAClF,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE,CAAA;SAC/D;QAED,IAAI,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,KAAK,KAAK,EAAE;YAC/F,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAA;SAC5E;QAED,IAAI,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,KAAK,KAAK,EAAE;YAC5G,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CA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AAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,KAAK,KAAK,EAAE;YAC5G,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC;SAC1F;QAED,sEAAsE;QACtE,MAAM,eAAe,GAAiC;YAClD,IAAI,EAAE,YAAY;SACrB,CAAC;QAEF,4HAA4H;QAC5H,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,CAAC,WAAW,CAAC,CAAC,cAAc,CAAC,GAAG,eAAe,CAAC;IACxH,CAAC,CAAA;AACL,CAAC,CAAC;AA3CW,QAAA,UAAU,cA2CrB"}
@@ -1,11 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InvalidJwtError = void 0;
4
+ const common_1 = require("@pristine-ts/common");
4
5
  /**
5
6
  * This Error is thrown when you try to decode a JWT but the token is invalid.
6
7
  */
7
- const common_1 = require("@pristine-ts/common");
8
8
  class InvalidJwtError extends common_1.LoggableError {
9
+ /**
10
+ * This Error is thrown when you try to decode a JWT but the token is invalid.
11
+ * @param message The error message to show.
12
+ * @param previousError The previous error.
13
+ * @param request The request that contained the JWT.
14
+ * @param token The actual JWT.
15
+ * @param algorithm The algorithm used to decode the JWT.
16
+ * @param publicKey The public key used to decode the JWT.
17
+ */
9
18
  constructor(message, previousError, request, token, algorithm, publicKey) {
10
19
  super(message, {
11
20
  request,
@@ -1 +1 @@
1
- {"version":3,"file":"invalid-jwt.error.js","sourceRoot":"","sources":["../../../../src/errors/invalid-jwt.error.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,gDAAoE;AAEpE,MAAa,eAAgB,SAAQ,sBAAa;IAE9C,YAAmB,OAAe,EAAE,aAAoB,EAAE,OAAyB,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;QAC5G,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;YACP,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;SACZ,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CACnE;AAfD,0CAeC"}
1
+ {"version":3,"file":"invalid-jwt.error.js","sourceRoot":"","sources":["../../../../src/errors/invalid-jwt.error.ts"],"names":[],"mappings":";;;AAAA,gDAAoE;AAEpE;;GAEG;AACH,MAAa,eAAgB,SAAQ,sBAAa;IAE9C;;;;;;;;OAQG;IACH,YAAmB,OAAe,EAAE,aAAoB,EAAE,OAAyB,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;QAC5G,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;YACP,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;SACZ,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CACnE;AAxBD,0CAwBC"}
@@ -1,11 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JwtAuthorizationHeaderError = void 0;
4
+ const common_1 = require("@pristine-ts/common");
4
5
  /**
5
6
  * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
6
7
  */
7
- const common_1 = require("@pristine-ts/common");
8
8
  class JwtAuthorizationHeaderError extends common_1.LoggableError {
9
+ /**
10
+ * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
11
+ * @param message The error message.
12
+ * @param request The request that is missing the AuthorizationHeader.
13
+ */
9
14
  constructor(message, request) {
10
15
  super(message, {
11
16
  request,
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-authorization-header.error.js","sourceRoot":"","sources":["../../../../src/errors/jwt-authorization-header.error.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,gDAAoE;AAEpE,MAAa,2BAA4B,SAAQ,sBAAa;IAC1D,YAAmB,OAAe,EAAE,OAAyB;QACzD,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;SACV,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CAC/E;AAVD,kEAUC"}
1
+ {"version":3,"file":"jwt-authorization-header.error.js","sourceRoot":"","sources":["../../../../src/errors/jwt-authorization-header.error.ts"],"names":[],"mappings":";;;AAAA,gDAAoE;AAEpE;;GAEG;AACH,MAAa,2BAA4B,SAAQ,sBAAa;IAC1D;;;;OAIG;IACH,YAAmB,OAAe,EAAE,OAAyB;QACzD,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;SACV,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CAC/E;AAfD,kEAeC"}
@@ -14,11 +14,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.JwtProtectedGuard = void 0;
16
16
  const tsyringe_1 = require("tsyringe");
17
+ /**
18
+ * This guard is used to verify that a route can only be accessed when a request has a valid JWT.
19
+ */
17
20
  let JwtProtectedGuard = class JwtProtectedGuard {
18
21
  constructor(jwtManager) {
19
22
  this.jwtManager = jwtManager;
20
23
  this.keyname = "jwt.protected";
21
24
  }
25
+ /**
26
+ * Verifies if the JWT is valid and authorizes access if it is.
27
+ * @param request
28
+ * @param identity
29
+ */
22
30
  isAuthorized(request, identity) {
23
31
  return new Promise((resolve) => {
24
32
  this.jwtManager.validateAndDecode(request)
@@ -26,6 +34,10 @@ let JwtProtectedGuard = class JwtProtectedGuard {
26
34
  .catch(reason => resolve(false));
27
35
  });
28
36
  }
37
+ /**
38
+ * Sets the context for the guard.
39
+ * @param context
40
+ */
29
41
  setContext(context) {
30
42
  this.guardContext = context;
31
43
  return Promise.resolve();
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-protected.guard.js","sourceRoot":"","sources":["../../../../src/guards/jwt-protected.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAA4C;AAO5C,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC1B,YAA4D,UAA+B;QAA/B,eAAU,GAAV,UAAU,CAAqB;QAGpF,YAAO,GAAG,eAAe,CAAC;IAFjC,CAAC;IAMD,YAAY,CAAC,OAAyB,EAAE,QAA4B;QAChE,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;YACpC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC;iBACrC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;iBAC5B,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;IACP,CAAC;IAED,UAAU,CAAC,OAAY;QACnB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC;QAE5B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;CACJ,CAAA;AArBY,iBAAiB;IAD7B,qBAAU,EAAE;IAEI,WAAA,iBAAM,CAAC,qBAAqB,CAAC,CAAA;;GADjC,iBAAiB,CAqB7B;AArBY,8CAAiB"}
1
+ {"version":3,"file":"jwt-protected.guard.js","sourceRoot":"","sources":["../../../../src/guards/jwt-protected.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAA4C;AAM5C;;GAEG;AAEH,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC1B,YAA4D,UAA+B;QAA/B,eAAU,GAAV,UAAU,CAAqB;QAGpF,YAAO,GAAG,eAAe,CAAC;IAFjC,CAAC;IAMD;;;;OAIG;IACH,YAAY,CAAC,OAAyB,EAAE,QAA4B;QAChE,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;YACpC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC;iBACrC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;iBAC5B,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;IACP,CAAC;IAED;;;OAGG;IACH,UAAU,CAAC,OAAY;QACnB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC;QAE5B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;CACJ,CAAA;AA9BY,iBAAiB;IAD7B,qBAAU,EAAE;IAEI,WAAA,iBAAM,CAAC,qBAAqB,CAAC,CAAA;;GADjC,iBAAiB,CA8B7B;AA9BY,8CAAiB"}
@@ -12,7 +12,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.JwtModule = void 0;
14
14
  const networking_1 = require("@pristine-ts/networking");
15
- const jwt_manager_1 = require("./managers/jwt.manager");
16
15
  const jwt_module_keyname_1 = require("./jwt.module.keyname");
17
16
  const configuration_1 = require("@pristine-ts/configuration");
18
17
  __exportStar(require("./decorators/decorators"), exports);
@@ -50,10 +49,6 @@ exports.JwtModule = {
50
49
  ],
51
50
  importModules: [
52
51
  networking_1.NetworkingModule,
53
- ],
54
- providerRegistrations: [{
55
- token: "JwtManagerInterface",
56
- useToken: jwt_manager_1.JwtManager,
57
- }]
52
+ ]
58
53
  };
59
54
  //# sourceMappingURL=jwt.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.module.js","sourceRoot":"","sources":["../../../src/jwt.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AACA,wDAAyD;AACzD,wDAAkD;AAGlD,6DAAsD;AACtD,8DAAuE;AAEvE,0DAAwC;AACxC,kDAAgC;AAChC,kDAAgC;AAChC,0DAAwC;AACxC,sDAAoC;AACpC,wDAAsC;AACtC,uDAAqC;AAExB,QAAA,SAAS,GAAoB;IACtC,OAAO,EAAE,qCAAgB;IACzB,wBAAwB,EAAE;QACtB;YACI,aAAa,EAAE,qCAAgB,GAAG,YAAY;YAC9C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,OAAO;YACrB,gBAAgB,EAAE;gBACd,IAAI,2CAA2B,CAAC,wBAAwB,CAAC;aAC5D;SACJ;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,YAAY;YAC9C,UAAU,EAAE,IAAI;SACnB;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;KACJ;IACD,aAAa,EAAE;QACX,6BAAgB;KACnB;IACD,qBAAqB,EAAE,CAAC;YACpB,KAAK,EAAE,qBAAqB;YAC5B,QAAQ,EAAE,wBAAU;SACvB,CAAC;CACL,CAAA"}
1
+ {"version":3,"file":"jwt.module.js","sourceRoot":"","sources":["../../../src/jwt.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AACA,wDAAyD;AAIzD,6DAAsD;AACtD,8DAAuE;AAEvE,0DAAwC;AACxC,kDAAgC;AAChC,kDAAgC;AAChC,0DAAwC;AACxC,sDAAoC;AACpC,wDAAsC;AACtC,uDAAqC;AAExB,QAAA,SAAS,GAAoB;IACtC,OAAO,EAAE,qCAAgB;IACzB,wBAAwB,EAAE;QACtB;YACI,aAAa,EAAE,qCAAgB,GAAG,YAAY;YAC9C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,OAAO;YACrB,gBAAgB,EAAE;gBACd,IAAI,2CAA2B,CAAC,wBAAwB,CAAC;aAC5D;SACJ;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,YAAY;YAC9C,UAAU,EAAE,IAAI;SACnB;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;QACD;YACI,aAAa,EAAE,qCAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;KACJ;IACD,aAAa,EAAE;QACX,6BAAgB;KACnB;CACJ,CAAA"}
@@ -15,16 +15,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.JwtManager = void 0;
16
16
  require("reflect-metadata");
17
17
  const tsyringe_1 = require("tsyringe");
18
+ const common_1 = require("@pristine-ts/common");
18
19
  const jsonwebtoken_1 = require("jsonwebtoken");
19
20
  const jwt_authorization_header_error_1 = require("../errors/jwt-authorization-header.error");
20
21
  const invalid_jwt_error_1 = require("../errors/invalid-jwt.error");
22
+ const jwt_module_keyname_1 = require("../jwt.module.keyname");
23
+ /**
24
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
25
+ */
21
26
  let JwtManager = class JwtManager {
27
+ /**
28
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
29
+ * @param publicKey The public key to use to validate the JWT token.
30
+ * @param algorithm The algorithm to use to decode the JWT token.
31
+ * @param privateKey
32
+ * @param passphrase
33
+ */
22
34
  constructor(publicKey, algorithm, privateKey, passphrase) {
23
35
  this.publicKey = publicKey;
24
36
  this.algorithm = algorithm;
25
37
  this.privateKey = privateKey;
26
38
  this.passphrase = passphrase;
27
39
  }
40
+ /**
41
+ * Validates that the request contains the Authorization and that it is properly formed and returns the JWT.
42
+ * @param request The request to validate.
43
+ * @private
44
+ */
28
45
  validateRequestAndReturnToken(request) {
29
46
  if (request.headers === undefined || request.headers.hasOwnProperty("Authorization") === false) {
30
47
  throw new jwt_authorization_header_error_1.JwtAuthorizationHeaderError("The Authorization header wasn't found in the Request.", request);
@@ -38,6 +55,10 @@ let JwtManager = class JwtManager {
38
55
  }
39
56
  return authorizationHeader.substr(7, authorizationHeader.length);
40
57
  }
58
+ /**
59
+ * Validates that the request is authorized by validating the JWT and returning the decoded JWT.
60
+ * @param request The request to validate that contains the JWT.
61
+ */
41
62
  validateAndDecode(request) {
42
63
  return new Promise((resolve, reject) => {
43
64
  try {
@@ -58,6 +79,8 @@ let JwtManager = class JwtManager {
58
79
  }
59
80
  };
60
81
  JwtManager = __decorate([
82
+ common_1.moduleScoped(jwt_module_keyname_1.JwtModuleKeyname),
83
+ common_1.tag("JwtManagerInterface"),
61
84
  tsyringe_1.injectable(),
62
85
  __param(0, tsyringe_1.inject("%pristine.jwt.publicKey%")),
63
86
  __param(1, tsyringe_1.inject("%pristine.jwt.algorithm%")),
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.manager.js","sourceRoot":"","sources":["../../../../src/managers/jwt.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4BAAyB;AACzB,uCAAuF;AAGvF,+CAA8C;AAC9C,6FAAqF;AACrF,mEAA4D;AAI5D,IAAa,UAAU,GAAvB,MAAa,UAAU;IACnB,YACyD,SAAiB,EACjB,SAAoB,EACnB,UAAmB,EACnB,UAAmB;QAHpB,cAAS,GAAT,SAAS,CAAQ;QACjB,cAAS,GAAT,SAAS,CAAW;QACnB,eAAU,GAAV,UAAU,CAAS;QACnB,eAAU,GAAV,UAAU,CAAS;IAE7E,CAAC;IAEO,6BAA6B,CAAC,OAAyB;QAC3D,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,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) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
11
14
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
15
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
16
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -21,13 +24,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
21
24
  exports.JwtPayloadParameterDecoratorResolver = void 0;
22
25
  require("reflect-metadata");
23
26
  const tsyringe_1 = require("tsyringe");
24
- const jwt_manager_1 = require("../managers/jwt.manager");
25
27
  const common_1 = require("@pristine-ts/common");
26
28
  const jwt_module_keyname_1 = require("../jwt.module.keyname");
29
+ /**
30
+ * The JwtPayloadParameterDecoratorResolver resolves the decoded JWT in the parameter of a route of a controller when the @jwtPayload() decorator is used.
31
+ * It is tagged as an MethodParameterDecoratorResolver so it can be automatically injected with the all the other MethodParameterDecoratorResolvers.
32
+ */
27
33
  let JwtPayloadParameterDecoratorResolver = class JwtPayloadParameterDecoratorResolver {
28
34
  constructor(jwtManager) {
29
35
  this.jwtManager = jwtManager;
30
36
  }
37
+ /**
38
+ * Resolves the decoded JWT.
39
+ * @param methodArgument The argument of the method that needs to be resolved.
40
+ * @param request The request being handled by the controller method.
41
+ * @param routeParameters The parameters of the route (path parameter).
42
+ * @param identity The identity of the user making the request.
43
+ */
31
44
  resolve(methodArgument, request, routeParameters, identity) {
32
45
  return __awaiter(this, void 0, void 0, function* () {
33
46
  // Here, we need to decrypt the header and return the decrypted jwt payload
@@ -40,6 +53,10 @@ let JwtPayloadParameterDecoratorResolver = class JwtPayloadParameterDecoratorRes
40
53
  return payload;
41
54
  });
42
55
  }
56
+ /**
57
+ * Verifies if the resolver supports this type of method argument.
58
+ * @param methodArgument The argument of the method that needs to be resolved.
59
+ */
43
60
  supports(methodArgument) {
44
61
  return methodArgument && methodArgument.hasOwnProperty("type") && methodArgument.type === "jwtPayload";
45
62
  }
@@ -48,7 +65,8 @@ JwtPayloadParameterDecoratorResolver = __decorate([
48
65
  common_1.moduleScoped(jwt_module_keyname_1.JwtModuleKeyname),
49
66
  common_1.tag(common_1.ServiceDefinitionTagEnum.MethodParameterDecoratorResolver),
50
67
  tsyringe_1.injectable(),
51
- __metadata("design:paramtypes", [jwt_manager_1.JwtManager])
68
+ __param(0, tsyringe_1.inject("JwtManagerInterface")),
69
+ __metadata("design:paramtypes", [Object])
52
70
  ], JwtPayloadParameterDecoratorResolver);
53
71
  exports.JwtPayloadParameterDecoratorResolver = JwtPayloadParameterDecoratorResolver;
54
72
  //# sourceMappingURL=jwt-payload-parameter-decorator.resolver.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-payload-parameter-decorator.resolver.js","sourceRoot":"","sources":["../../../../src/resolvers/jwt-payload-parameter-decorator.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,4BAAyB;AACzB,uCAAoC;AAEpC,yDAAmD;AACnD,gDAAmG;AACnG,8DAAuD;AAKvD,IAAa,oCAAoC,GAAjD,MAAa,oCAAoC;IAE7C,YAA6B,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IACnD,CAAC;IAEK,OAAO,CAAC,cAAmB,EACnB,OAAgB,EAChB,eAAwC,EACxC,QAA4B;;YAEtC,2EAA2E;YAC3E,IAAI,OAAO,GAAG,EAAE,CAAC;YAEjB,IAAI;gBACA,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;aAC9D;YAAC,OAAO,CAAC,EAAE;aACX;YAED,OAAO,OAAO,CAAC;QACnB,CAAC;KAAA;IAED,QAAQ,CAAC,cAAmB;QACxB,OAAO,cAAc,IAAI,cAAc,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC,IAAI,KAAK,YAAY,CAAC;IAC3G,CAAC;CACJ,CAAA;AAxBY,oCAAoC;IAHhD,qBAAY,CAAC,qCAAgB,CAAC;IAC9B,YAAG,CAAC,iCAAwB,CAAC,gCAAgC,CAAC;IAC9D,qBAAU,EAAE;qCAGgC,wBAAU;GAF1C,oCAAoC,CAwBhD;AAxBY,oFAAoC"}
1
+ {"version":3,"file":"jwt-payload-parameter-decorator.resolver.js","sourceRoot":"","sources":["../../../../src/resolvers/jwt-payload-parameter-decorator.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,4BAAyB;AACzB,uCAA4C;AAE5C,gDAAmG;AACnG,8DAAuD;AAGvD;;;GAGG;AAIH,IAAa,oCAAoC,GAAjD,MAAa,oCAAoC;IAE7C,YAA4D,UAA+B;QAA/B,eAAU,GAAV,UAAU,CAAqB;IAC3F,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,cAAmB,EACnB,OAAgB,EAChB,eAAwC,EACxC,QAA4B;;YAEtC,2EAA2E;YAC3E,IAAI,OAAO,GAAG,EAAE,CAAC;YAEjB,IAAI;gBACA,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;aAC9D;YAAC,OAAO,CAAC,EAAE;aACX;YAED,OAAO,OAAO,CAAC;QACnB,CAAC;KAAA;IAED;;;OAGG;IACH,QAAQ,CAAC,cAAmB;QACxB,OAAO,cAAc,IAAI,cAAc,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC,IAAI,KAAK,YAAY,CAAC;IAC3G,CAAC;CACJ,CAAA;AAnCY,oCAAoC;IAHhD,qBAAY,CAAC,qCAAgB,CAAC;IAC9B,YAAG,CAAC,iCAAwB,CAAC,gCAAgC,CAAC;IAC9D,qBAAU,EAAE;IAGI,WAAA,iBAAM,CAAC,qBAAqB,CAAC,CAAA;;GAFjC,oCAAoC,CAmChD;AAnCY,oFAAoC"}
@@ -1,5 +1,22 @@
1
+ /**
2
+ * This decorator can be used to inject the decoded JWT payload of a request in a parameter of a method in a controller.
3
+ */
1
4
  export const jwtPayload = () => {
2
- return (target, propertyKey, parameterIndex) => {
5
+ return (
6
+ /**
7
+ * The class on which the decorator is used.
8
+ */
9
+ target,
10
+ /**
11
+ * The method on which the decorator is used.
12
+ */
13
+ propertyKey,
14
+ /**
15
+ * The index of the parameter for which the decorator is used.
16
+ */
17
+ parameterIndex) => {
18
+ // Verify that the object target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"] exists or we create it.
19
+ // This object is a convention defined by Pristine on where to save controller method parameter decorator information and is used in the router to retrieve that information.
3
20
  if (target.constructor.prototype.hasOwnProperty("__metadata__") === false) {
4
21
  target.constructor.prototype["__metadata__"] = {};
5
22
  }
@@ -12,9 +29,11 @@ export const jwtPayload = () => {
12
29
  if (target.constructor.prototype["__metadata__"]["methods"][propertyKey].hasOwnProperty("arguments") === false) {
13
30
  target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"] = [];
14
31
  }
32
+ // Set the type of method parameter. Each decorator has it's own type.
15
33
  const methodParameter = {
16
34
  type: "jwtPayload"
17
35
  };
36
+ // Save the method parameter with the proper parameter index (index of the parameter in the list of parameters of a method).
18
37
  target.constructor.prototype["__metadata__"]["methods"][propertyKey]["arguments"][parameterIndex] = methodParameter;
19
38
  };
20
39
  };
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-payload.decorators.js","sourceRoot":"","sources":["../../../../src/decorators/jwt-payload.decorators.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,EAAE;IAC3B,OAAO,CACH,MAAc,EACd,WAA4B,EAC5B,cAAsB,EACxB,EAAE;QACA,IAAI,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,cAAc,CAAC,KAAK,KAAK,EAAE;YACvE,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,EAAE,CAAA;SACp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
2
  /**
2
3
  * This Error is thrown when you try to decode a JWT but the token is invalid.
3
4
  */
4
- import { LoggableError } from "@pristine-ts/common";
5
5
  export class InvalidJwtError extends LoggableError {
6
+ /**
7
+ * This Error is thrown when you try to decode a JWT but the token is invalid.
8
+ * @param message The error message to show.
9
+ * @param previousError The previous error.
10
+ * @param request The request that contained the JWT.
11
+ * @param token The actual JWT.
12
+ * @param algorithm The algorithm used to decode the JWT.
13
+ * @param publicKey The public key used to decode the JWT.
14
+ */
6
15
  constructor(message, previousError, request, token, algorithm, publicKey) {
7
16
  super(message, {
8
17
  request,
@@ -1 +1 @@
1
- {"version":3,"file":"invalid-jwt.error.js","sourceRoot":"","sources":["../../../../src/errors/invalid-jwt.error.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAC,aAAa,EAAmB,MAAM,qBAAqB,CAAC;AAEpE,MAAM,OAAO,eAAgB,SAAQ,aAAa;IAE9C,YAAmB,OAAe,EAAE,aAAoB,EAAE,OAAyB,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;QAC5G,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;YACP,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;SACZ,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CACnE"}
1
+ {"version":3,"file":"invalid-jwt.error.js","sourceRoot":"","sources":["../../../../src/errors/invalid-jwt.error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAmB,MAAM,qBAAqB,CAAC;AAEpE;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,aAAa;IAE9C;;;;;;;;OAQG;IACH,YAAmB,OAAe,EAAE,aAAoB,EAAE,OAAyB,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;QAC5G,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;YACP,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;SACZ,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CACnE"}
@@ -1,8 +1,13 @@
1
+ import { LoggableError } from "@pristine-ts/common";
1
2
  /**
2
3
  * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
3
4
  */
4
- import { LoggableError } from "@pristine-ts/common";
5
5
  export class JwtAuthorizationHeaderError extends LoggableError {
6
+ /**
7
+ * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
8
+ * @param message The error message.
9
+ * @param request The request that is missing the AuthorizationHeader.
10
+ */
6
11
  constructor(message, request) {
7
12
  super(message, {
8
13
  request,
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-authorization-header.error.js","sourceRoot":"","sources":["../../../../src/errors/jwt-authorization-header.error.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAC,aAAa,EAAmB,MAAM,qBAAqB,CAAC;AAEpE,MAAM,OAAO,2BAA4B,SAAQ,aAAa;IAC1D,YAAmB,OAAe,EAAE,OAAyB;QACzD,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;SACV,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CAC/E"}
1
+ {"version":3,"file":"jwt-authorization-header.error.js","sourceRoot":"","sources":["../../../../src/errors/jwt-authorization-header.error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAmB,MAAM,qBAAqB,CAAC;AAEpE;;GAEG;AACH,MAAM,OAAO,2BAA4B,SAAQ,aAAa;IAC1D;;;;OAIG;IACH,YAAmB,OAAe,EAAE,OAAyB;QACzD,KAAK,CAAC,OAAO,EAAE;YACX,OAAO;SACV,CAAC,CAAC;QAEH,gCAAgC;QAChC,kDAAkD;QAClD,gIAAgI;QAChI,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;IAAI,CAAC;CAC/E"}
@@ -11,11 +11,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
11
  return function (target, key) { decorator(target, key, paramIndex); }
12
12
  };
13
13
  import { inject, injectable } from "tsyringe";
14
+ /**
15
+ * This guard is used to verify that a route can only be accessed when a request has a valid JWT.
16
+ */
14
17
  let JwtProtectedGuard = class JwtProtectedGuard {
15
18
  constructor(jwtManager) {
16
19
  this.jwtManager = jwtManager;
17
20
  this.keyname = "jwt.protected";
18
21
  }
22
+ /**
23
+ * Verifies if the JWT is valid and authorizes access if it is.
24
+ * @param request
25
+ * @param identity
26
+ */
19
27
  isAuthorized(request, identity) {
20
28
  return new Promise((resolve) => {
21
29
  this.jwtManager.validateAndDecode(request)
@@ -23,6 +31,10 @@ let JwtProtectedGuard = class JwtProtectedGuard {
23
31
  .catch(reason => resolve(false));
24
32
  });
25
33
  }
34
+ /**
35
+ * Sets the context for the guard.
36
+ * @param context
37
+ */
26
38
  setContext(context) {
27
39
  this.guardContext = context;
28
40
  return Promise.resolve();
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-protected.guard.js","sourceRoot":"","sources":["../../../../src/guards/jwt-protected.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAO5C,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC1B,YAA4D,UAA+B;QAA/B,eAAU,GAAV,UAAU,CAAqB;QAGpF,YAAO,GAAG,eAAe,CAAC;IAFjC,CAAC;IAMD,YAAY,CAAC,OAAyB,EAAE,QAA4B;QAChE,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;YACpC,IAAI,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;QAE5B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC7B,CAAC;CACJ,CAAA;AA9BY,iBAAiB;IAD7B,UAAU,EAAE;IAEI,WAAA,MAAM,CAAC,qBAAqB,CAAC,CAAA;;GADjC,iBAAiB,CA8B7B;SA9BY,iBAAiB"}
@@ -1,5 +1,4 @@
1
1
  import { NetworkingModule } from "@pristine-ts/networking";
2
- import { JwtManager } from "./managers/jwt.manager";
3
2
  import { JwtModuleKeyname } from "./jwt.module.keyname";
4
3
  import { EnvironmentVariableResolver } from "@pristine-ts/configuration";
5
4
  export * from "./decorators/decorators";
@@ -37,10 +36,6 @@ export const JwtModule = {
37
36
  ],
38
37
  importModules: [
39
38
  NetworkingModule,
40
- ],
41
- providerRegistrations: [{
42
- token: "JwtManagerInterface",
43
- useToken: JwtManager,
44
- }]
39
+ ]
45
40
  };
46
41
  //# sourceMappingURL=jwt.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.module.js","sourceRoot":"","sources":["../../../src/jwt.module.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAC,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAC,UAAU,EAAC,MAAM,wBAAwB,CAAC;AAGlD,OAAO,EAAC,gBAAgB,EAAC,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAC,2BAA2B,EAAC,MAAM,4BAA4B,CAAC;AAEvE,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AAErC,MAAM,CAAC,MAAM,SAAS,GAAoB;IACtC,OAAO,EAAE,gBAAgB;IACzB,wBAAwB,EAAE;QACtB;YACI,aAAa,EAAE,gBAAgB,GAAG,YAAY;YAC9C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,OAAO;YACrB,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ACI,aAAa,EAAE,gBAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;QACD;YACI,aAAa,EAAE,gBAAgB,GAAG,aAAa;YAC/C,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,EAAE;SACnB;KACJ;IACD,aAAa,EAAE;QACX,gBAAgB;KACnB;CACJ,CAAA"}
@@ -12,16 +12,33 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  };
13
13
  import "reflect-metadata";
14
14
  import { inject, injectable } from "tsyringe";
15
+ import { moduleScoped, tag } from "@pristine-ts/common";
15
16
  import { verify } from "jsonwebtoken";
16
17
  import { JwtAuthorizationHeaderError } from "../errors/jwt-authorization-header.error";
17
18
  import { InvalidJwtError } from "../errors/invalid-jwt.error";
19
+ import { JwtModuleKeyname } from "../jwt.module.keyname";
20
+ /**
21
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
22
+ */
18
23
  let JwtManager = class JwtManager {
24
+ /**
25
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
26
+ * @param publicKey The public key to use to validate the JWT token.
27
+ * @param algorithm The algorithm to use to decode the JWT token.
28
+ * @param privateKey
29
+ * @param passphrase
30
+ */
19
31
  constructor(publicKey, algorithm, privateKey, passphrase) {
20
32
  this.publicKey = publicKey;
21
33
  this.algorithm = algorithm;
22
34
  this.privateKey = privateKey;
23
35
  this.passphrase = passphrase;
24
36
  }
37
+ /**
38
+ * Validates that the request contains the Authorization and that it is properly formed and returns the JWT.
39
+ * @param request The request to validate.
40
+ * @private
41
+ */
25
42
  validateRequestAndReturnToken(request) {
26
43
  if (request.headers === undefined || request.headers.hasOwnProperty("Authorization") === false) {
27
44
  throw new JwtAuthorizationHeaderError("The Authorization header wasn't found in the Request.", request);
@@ -35,6 +52,10 @@ let JwtManager = class JwtManager {
35
52
  }
36
53
  return authorizationHeader.substr(7, authorizationHeader.length);
37
54
  }
55
+ /**
56
+ * Validates that the request is authorized by validating the JWT and returning the decoded JWT.
57
+ * @param request The request to validate that contains the JWT.
58
+ */
38
59
  validateAndDecode(request) {
39
60
  return new Promise((resolve, reject) => {
40
61
  try {
@@ -55,6 +76,8 @@ let JwtManager = class JwtManager {
55
76
  }
56
77
  };
57
78
  JwtManager = __decorate([
79
+ moduleScoped(JwtModuleKeyname),
80
+ tag("JwtManagerInterface"),
58
81
  injectable(),
59
82
  __param(0, inject("%pristine.jwt.publicKey%")),
60
83
  __param(1, inject("%pristine.jwt.algorithm%")),
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.manager.js","sourceRoot":"","sources":["../../../../src/managers/jwt.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,kBAAkB,CAAA;AACzB,OAAO,EAAiC,MAAM,EAAE,UAAU,EAAY,MAAM,UAAU,CAAC;AAGvF,OAAO,EAAY,MAAM,EAAC,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAC,2BAA2B,EAAC,MAAM,0CAA0C,CAAC;AACrF,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAI5D,IAAa,UAAU,GAAvB,MAAa,UAAU;IACnB,YACyD,SAAiB,EACjB,SAAoB,EACnB,UAAmB,EACnB,UAAmB;QAHpB,cAAS,GAAT,SAAS,CAAQ;QACjB,cAAS,GAAT,SAAS,CAAW;QACnB,eAAU,GAAV,UAAU,CAAS;QACnB,eAAU,GAAV,UAAU,CAAS;IAE7E,CAAC;IAEO,6BAA6B,CAAC,OAAyB;QAC3D,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,KAAK,KAAK,EAAE;YAC5F,MAAM,IAAI,2BAA2B,CAAC,uDAAuD,EAAE,OAAO,CAAC,CAAC;SAC3G;QAED,MAAM,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;QAE1D,IAAI,mBAAmB,KAAK,SAAS,EAAE;YACnC,MAAM,IAAI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7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
10
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
12
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -17,14 +20,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
17
20
  });
18
21
  };
19
22
  import "reflect-metadata";
20
- import { injectable } from "tsyringe";
21
- import { JwtManager } from "../managers/jwt.manager";
23
+ import { inject, injectable } from "tsyringe";
22
24
  import { moduleScoped, ServiceDefinitionTagEnum, tag } from "@pristine-ts/common";
23
25
  import { JwtModuleKeyname } from "../jwt.module.keyname";
26
+ /**
27
+ * The JwtPayloadParameterDecoratorResolver resolves the decoded JWT in the parameter of a route of a controller when the @jwtPayload() decorator is used.
28
+ * It is tagged as an MethodParameterDecoratorResolver so it can be automatically injected with the all the other MethodParameterDecoratorResolvers.
29
+ */
24
30
  let JwtPayloadParameterDecoratorResolver = class JwtPayloadParameterDecoratorResolver {
25
31
  constructor(jwtManager) {
26
32
  this.jwtManager = jwtManager;
27
33
  }
34
+ /**
35
+ * Resolves the decoded JWT.
36
+ * @param methodArgument The argument of the method that needs to be resolved.
37
+ * @param request The request being handled by the controller method.
38
+ * @param routeParameters The parameters of the route (path parameter).
39
+ * @param identity The identity of the user making the request.
40
+ */
28
41
  resolve(methodArgument, request, routeParameters, identity) {
29
42
  return __awaiter(this, void 0, void 0, function* () {
30
43
  // Here, we need to decrypt the header and return the decrypted jwt payload
@@ -37,6 +50,10 @@ let JwtPayloadParameterDecoratorResolver = class JwtPayloadParameterDecoratorRes
37
50
  return payload;
38
51
  });
39
52
  }
53
+ /**
54
+ * Verifies if the resolver supports this type of method argument.
55
+ * @param methodArgument The argument of the method that needs to be resolved.
56
+ */
40
57
  supports(methodArgument) {
41
58
  return methodArgument && methodArgument.hasOwnProperty("type") && methodArgument.type === "jwtPayload";
42
59
  }
@@ -45,7 +62,8 @@ JwtPayloadParameterDecoratorResolver = __decorate([
45
62
  moduleScoped(JwtModuleKeyname),
46
63
  tag(ServiceDefinitionTagEnum.MethodParameterDecoratorResolver),
47
64
  injectable(),
48
- __metadata("design:paramtypes", [JwtManager])
65
+ __param(0, inject("JwtManagerInterface")),
66
+ __metadata("design:paramtypes", [Object])
49
67
  ], JwtPayloadParameterDecoratorResolver);
50
68
  export { JwtPayloadParameterDecoratorResolver };
51
69
  //# sourceMappingURL=jwt-payload-parameter-decorator.resolver.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"jwt-payload-parameter-decorator.resolver.js","sourceRoot":"","sources":["../../../../src/resolvers/jwt-payload-parameter-decorator.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,kBAAkB,CAAA;AACzB,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAC,UAAU,EAAC,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAoB,YAAY,EAAE,wBAAwB,EAAE,GAAG,EAAC,MAAM,qBAAqB,CAAC;AACnG,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAKvD,IAAa,oCAAoC,GAAjD,MAAa,oCAAoC;IAE7C,YAA6B,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IACnD,CAAC;IAEK,OAAO,CAAC,cAAmB,EACnB,OAAgB,EAChB,eAAwC,EACxC,QAA4B;;YAEtC,2EAA2E;YAC3E,IAAI,OAAO,GAAG,EAAE,CAAC;YAEjB,IAAI;gBACA,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;aAC9D;YAAC,OAAO,CAAC,EAAE;aACX;YAED,OAAO,OAAO,CAAC;QACnB,CAAC;KAAA;IAED,QAAQ,CAAC,cAAmB;QACxB,OAAO,cAAc,IAAI,cAAc,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC,IAAI,KAAK,YAAY,CAAC;IAC3G,CAAC;CACJ,CAAA;AAxBY,oCAAoC;IAHhD,YAAY,CAAC,gBAAgB,CAAC;IAC9B,GAAG,CAAC,wBAAwB,CAAC,gCAAgC,CAAC;IAC9D,UAAU,EAAE;qCAGgC,UAAU;GAF1C,oCAAoC,CAwBhD;SAxBY,oCAAoC"}
1
+ {"version":3,"file":"jwt-payload-parameter-decorator.resolver.js","sourceRoot":"","sources":["../../../../src/resolvers/jwt-payload-parameter-decorator.resolver.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,kBAAkB,CAAA;AACzB,OAAO,EAAC,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAoB,YAAY,EAAE,wBAAwB,EAAE,GAAG,EAAC,MAAM,qBAAqB,CAAC;AACnG,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAGvD;;;GAGG;AAIH,IAAa,oCAAoC,GAAjD,MAAa,oCAAoC;IAE7C,YAA4D,UAA+B;QAA/B,eAAU,GAAV,UAAU,CAAqB;IAC3F,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,cAAmB,EACnB,OAAgB,EAChB,eAAwC,EACxC,QAA4B;;YAEtC,2EAA2E;YAC3E,IAAI,OAAO,GAAG,EAAE,CAAC;YAEjB,IAAI;gBACA,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;aAC9D;YAAC,OAAO,CAAC,EAAE;aACX;YAED,OAAO,OAAO,CAAC;QACnB,CAAC;KAAA;IAED;;;OAGG;IACH,QAAQ,CAAC,cAAmB;QACxB,OAAO,cAAc,IAAI,cAAc,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,cAAc,CAAC,IAAI,KAAK,YAAY,CAAC;IAC3G,CAAC;CACJ,CAAA;AAnCY,oCAAoC;IAHhD,YAAY,CAAC,gBAAgB,CAAC;IAC9B,GAAG,CAAC,wBAAwB,CAAC,gCAAgC,CAAC;IAC9D,UAAU,EAAE;IAGI,WAAA,MAAM,CAAC,qBAAqB,CAAC,CAAA;;GAFjC,oCAAoC,CAmChD;SAnCY,oCAAoC"}
@@ -1 +1,4 @@
1
+ /**
2
+ * This decorator can be used to inject the decoded JWT payload of a request in a parameter of a method in a controller.
3
+ */
1
4
  export declare const jwtPayload: () => (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
@@ -1,7 +1,16 @@
1
+ import { LoggableError, RequestInterface } from "@pristine-ts/common";
1
2
  /**
2
3
  * This Error is thrown when you try to decode a JWT but the token is invalid.
3
4
  */
4
- import { LoggableError, RequestInterface } from "@pristine-ts/common";
5
5
  export declare class InvalidJwtError extends LoggableError {
6
+ /**
7
+ * This Error is thrown when you try to decode a JWT but the token is invalid.
8
+ * @param message The error message to show.
9
+ * @param previousError The previous error.
10
+ * @param request The request that contained the JWT.
11
+ * @param token The actual JWT.
12
+ * @param algorithm The algorithm used to decode the JWT.
13
+ * @param publicKey The public key used to decode the JWT.
14
+ */
6
15
  constructor(message: string, previousError: Error, request: RequestInterface, token: any, algorithm: any, publicKey: any);
7
16
  }
@@ -1,7 +1,12 @@
1
+ import { LoggableError, RequestInterface } from "@pristine-ts/common";
1
2
  /**
2
3
  * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
3
4
  */
4
- import { LoggableError, RequestInterface } from "@pristine-ts/common";
5
5
  export declare class JwtAuthorizationHeaderError extends LoggableError {
6
+ /**
7
+ * This Error is thrown when there's you try to decode a JWT in a request but the AuthorizationHeader is missing.
8
+ * @param message The error message.
9
+ * @param request The request that is missing the AuthorizationHeader.
10
+ */
6
11
  constructor(message: string, request: RequestInterface);
7
12
  }
@@ -2,11 +2,23 @@ import { JwtManagerInterface } from "../interfaces/jwt-manager.interface";
2
2
  import { IdentityInterface, RequestInterface } from "@pristine-ts/common";
3
3
  import { GuardInterface } from "@pristine-ts/security";
4
4
  import { GuardContextInterface } from "@pristine-ts/security";
5
+ /**
6
+ * This guard is used to verify that a route can only be accessed when a request has a valid JWT.
7
+ */
5
8
  export declare class JwtProtectedGuard implements GuardInterface {
6
9
  private readonly jwtManager;
7
10
  constructor(jwtManager: JwtManagerInterface);
8
11
  keyname: string;
9
12
  guardContext: GuardContextInterface;
13
+ /**
14
+ * Verifies if the JWT is valid and authorizes access if it is.
15
+ * @param request
16
+ * @param identity
17
+ */
10
18
  isAuthorized(request: RequestInterface, identity?: IdentityInterface): Promise<boolean>;
19
+ /**
20
+ * Sets the context for the guard.
21
+ * @param context
22
+ */
11
23
  setContext(context: any): Promise<void>;
12
24
  }
@@ -1,4 +1,11 @@
1
1
  import { RequestInterface } from "@pristine-ts/common";
2
+ /**
3
+ * The JwtManager Interface defines the methods that a JWT manager must implement.
4
+ */
2
5
  export interface JwtManagerInterface {
6
+ /**
7
+ * Validates and returns the decoded JWT.
8
+ * @param request The request containing the JWT.
9
+ */
3
10
  validateAndDecode(request: RequestInterface): Promise<any>;
4
11
  }
@@ -1,4 +1,7 @@
1
1
  import { ParameterDecoratorInterface } from "@pristine-ts/networking";
2
+ /**
3
+ * The JwtPayloadDecoratorInterface defines what a JWT payload decorator needs.
4
+ */
2
5
  export interface JwtPayloadDecoratorInterface extends ParameterDecoratorInterface {
3
6
  type: "jwtPayload";
4
7
  }
@@ -2,12 +2,31 @@ import "reflect-metadata";
2
2
  import { RequestInterface } from "@pristine-ts/common";
3
3
  import { Algorithm } from "jsonwebtoken";
4
4
  import { JwtManagerInterface } from "../interfaces/jwt-manager.interface";
5
+ /**
6
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
7
+ */
5
8
  export declare class JwtManager implements JwtManagerInterface {
6
9
  private readonly publicKey;
7
10
  private readonly algorithm;
8
11
  private readonly privateKey?;
9
12
  private readonly passphrase?;
13
+ /**
14
+ * The JwtManager makes decodes and validates JWT token so that they can be used.
15
+ * @param publicKey The public key to use to validate the JWT token.
16
+ * @param algorithm The algorithm to use to decode the JWT token.
17
+ * @param privateKey
18
+ * @param passphrase
19
+ */
10
20
  constructor(publicKey: string, algorithm: Algorithm, privateKey?: string | undefined, passphrase?: string | undefined);
21
+ /**
22
+ * Validates that the request contains the Authorization and that it is properly formed and returns the JWT.
23
+ * @param request The request to validate.
24
+ * @private
25
+ */
11
26
  private validateRequestAndReturnToken;
27
+ /**
28
+ * Validates that the request is authorized by validating the JWT and returning the decoded JWT.
29
+ * @param request The request to validate that contains the JWT.
30
+ */
12
31
  validateAndDecode(request: RequestInterface): Promise<any>;
13
32
  }
@@ -1,12 +1,27 @@
1
1
  import "reflect-metadata";
2
2
  import { ControllerMethodParameterDecoratorResolverInterface, Request } from "@pristine-ts/networking";
3
- import { JwtManager } from "../managers/jwt.manager";
4
3
  import { IdentityInterface } from "@pristine-ts/common";
4
+ import { JwtManagerInterface } from "../interfaces/jwt-manager.interface";
5
+ /**
6
+ * The JwtPayloadParameterDecoratorResolver resolves the decoded JWT in the parameter of a route of a controller when the @jwtPayload() decorator is used.
7
+ * It is tagged as an MethodParameterDecoratorResolver so it can be automatically injected with the all the other MethodParameterDecoratorResolvers.
8
+ */
5
9
  export declare class JwtPayloadParameterDecoratorResolver implements ControllerMethodParameterDecoratorResolverInterface {
6
10
  private readonly jwtManager;
7
- constructor(jwtManager: JwtManager);
11
+ constructor(jwtManager: JwtManagerInterface);
12
+ /**
13
+ * Resolves the decoded JWT.
14
+ * @param methodArgument The argument of the method that needs to be resolved.
15
+ * @param request The request being handled by the controller method.
16
+ * @param routeParameters The parameters of the route (path parameter).
17
+ * @param identity The identity of the user making the request.
18
+ */
8
19
  resolve(methodArgument: any, request: Request, routeParameters: {
9
20
  [p: string]: string;
10
21
  }, identity?: IdentityInterface): Promise<any>;
22
+ /**
23
+ * Verifies if the resolver supports this type of method argument.
24
+ * @param methodArgument The argument of the method that needs to be resolved.
25
+ */
11
26
  supports(methodArgument: any): boolean;
12
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pristine-ts/jwt",
3
- "version": "0.0.139",
3
+ "version": "0.0.143",
4
4
  "description": "",
5
5
  "module": "dist/lib/esm/jwt.module.js",
6
6
  "main": "dist/lib/cjs/jwt.module.js",
@@ -19,9 +19,9 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@pristine-ts/common": "^0.0.139",
23
- "@pristine-ts/networking": "^0.0.139",
24
- "@pristine-ts/security": "^0.0.139",
22
+ "@pristine-ts/common": "^0.0.143",
23
+ "@pristine-ts/networking": "^0.0.143",
24
+ "@pristine-ts/security": "^0.0.143",
25
25
  "jsonwebtoken": "^8.5.1"
26
26
  },
27
27
  "devDependencies": {
@@ -61,5 +61,5 @@
61
61
  "src/*.{js,ts}"
62
62
  ]
63
63
  },
64
- "gitHead": "9057da83ea4c70c321246e5201b57ce7f3239ff6"
64
+ "gitHead": "b755c7c2a01ad7fce56c6050b4c09cea05afd5ff"
65
65
  }
package/readme.md CHANGED
@@ -0,0 +1 @@
1
+ # JWT module