@tsed/passport 6.87.2 → 6.90.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 (131) hide show
  1. package/lib/{PassportModule.d.ts → cjs/PassportModule.d.ts} +0 -0
  2. package/lib/{PassportModule.js → cjs/PassportModule.js} +16 -16
  3. package/lib/cjs/PassportModule.js.map +1 -0
  4. package/lib/{contants → cjs/contants}/index.d.ts +0 -0
  5. package/lib/{contants → cjs/contants}/index.js +0 -0
  6. package/lib/cjs/contants/index.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/args.d.ts +0 -0
  8. package/lib/{decorators → cjs/decorators}/args.js +2 -2
  9. package/lib/cjs/decorators/args.js.map +1 -0
  10. package/lib/{decorators → cjs/decorators}/authenticate.d.ts +0 -0
  11. package/lib/{decorators → cjs/decorators}/authenticate.js +1 -1
  12. package/lib/cjs/decorators/authenticate.js.map +1 -0
  13. package/lib/{decorators → cjs/decorators}/authorize.d.ts +0 -0
  14. package/lib/{decorators → cjs/decorators}/authorize.js +1 -1
  15. package/lib/cjs/decorators/authorize.js.map +1 -0
  16. package/lib/{decorators → cjs/decorators}/protocol.d.ts +0 -0
  17. package/lib/{decorators → cjs/decorators}/protocol.js +2 -2
  18. package/lib/cjs/decorators/protocol.js.map +1 -0
  19. package/lib/{domain → cjs/domain}/UserInfo.d.ts +0 -0
  20. package/lib/cjs/domain/UserInfo.js +22 -0
  21. package/lib/cjs/domain/UserInfo.js.map +1 -0
  22. package/lib/{errors → cjs/errors}/PassportException.d.ts +0 -0
  23. package/lib/{errors → cjs/errors}/PassportException.js +0 -0
  24. package/lib/cjs/errors/PassportException.js.map +1 -0
  25. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  26. package/lib/cjs/index.js +20 -0
  27. package/lib/cjs/index.js.map +1 -0
  28. package/lib/{interfaces → cjs/interfaces}/IProtocol.d.ts +0 -0
  29. package/lib/{interfaces → cjs/interfaces}/IProtocol.js +0 -0
  30. package/lib/cjs/interfaces/IProtocol.js.map +1 -0
  31. package/lib/{interfaces → cjs/interfaces}/OnInstall.d.ts +0 -0
  32. package/lib/{interfaces → cjs/interfaces}/OnInstall.js +0 -0
  33. package/lib/cjs/interfaces/OnInstall.js.map +1 -0
  34. package/lib/{interfaces → cjs/interfaces}/OnVerify.d.ts +0 -0
  35. package/lib/{interfaces → cjs/interfaces}/OnVerify.js +0 -0
  36. package/lib/cjs/interfaces/OnVerify.js.map +1 -0
  37. package/lib/{interfaces → cjs/interfaces}/ProtocolOptions.d.ts +0 -0
  38. package/lib/{interfaces → cjs/interfaces}/ProtocolOptions.js +0 -0
  39. package/lib/cjs/interfaces/ProtocolOptions.js.map +1 -0
  40. package/lib/{interfaces → cjs/interfaces}/index.d.ts +0 -0
  41. package/lib/cjs/interfaces/index.js +8 -0
  42. package/lib/cjs/interfaces/index.js.map +1 -0
  43. package/lib/{middlewares → cjs/middlewares}/PassportMiddleware.d.ts +0 -0
  44. package/lib/{middlewares → cjs/middlewares}/PassportMiddleware.js +11 -11
  45. package/lib/cjs/middlewares/PassportMiddleware.js.map +1 -0
  46. package/lib/{services → cjs/services}/PassportSerializerService.d.ts +0 -0
  47. package/lib/{services → cjs/services}/PassportSerializerService.js +7 -7
  48. package/lib/cjs/services/PassportSerializerService.js.map +1 -0
  49. package/lib/{services → cjs/services}/ProtocolsService.d.ts +0 -0
  50. package/lib/{services → cjs/services}/ProtocolsService.js +6 -6
  51. package/lib/cjs/services/ProtocolsService.js.map +1 -0
  52. package/lib/{utils → cjs/utils}/getProtocolsFromRequest.d.ts +0 -0
  53. package/lib/{utils → cjs/utils}/getProtocolsFromRequest.js +1 -1
  54. package/lib/cjs/utils/getProtocolsFromRequest.js.map +1 -0
  55. package/lib/esm/PassportModule.js +58 -0
  56. package/lib/esm/PassportModule.js.map +1 -0
  57. package/lib/esm/contants/index.js +2 -0
  58. package/lib/esm/contants/index.js.map +1 -0
  59. package/lib/esm/decorators/args.js +13 -0
  60. package/lib/esm/decorators/args.js.map +1 -0
  61. package/lib/esm/decorators/authenticate.js +12 -0
  62. package/lib/esm/decorators/authenticate.js.map +1 -0
  63. package/lib/esm/decorators/authorize.js +12 -0
  64. package/lib/esm/decorators/authorize.js.map +1 -0
  65. package/lib/esm/decorators/protocol.js +21 -0
  66. package/lib/esm/decorators/protocol.js.map +1 -0
  67. package/lib/esm/domain/UserInfo.js +18 -0
  68. package/lib/esm/domain/UserInfo.js.map +1 -0
  69. package/lib/esm/errors/PassportException.js +8 -0
  70. package/lib/esm/errors/PassportException.js.map +1 -0
  71. package/lib/esm/index.js +17 -0
  72. package/lib/esm/index.js.map +1 -0
  73. package/lib/esm/interfaces/IProtocol.js +2 -0
  74. package/lib/esm/interfaces/IProtocol.js.map +1 -0
  75. package/lib/esm/interfaces/OnInstall.js +2 -0
  76. package/lib/esm/interfaces/OnInstall.js.map +1 -0
  77. package/lib/esm/interfaces/OnVerify.js +2 -0
  78. package/lib/esm/interfaces/OnVerify.js.map +1 -0
  79. package/lib/esm/interfaces/ProtocolOptions.js +2 -0
  80. package/lib/esm/interfaces/ProtocolOptions.js.map +1 -0
  81. package/lib/esm/interfaces/index.js +5 -0
  82. package/lib/esm/interfaces/index.js.map +1 -0
  83. package/lib/esm/middlewares/PassportMiddleware.js +41 -0
  84. package/lib/esm/middlewares/PassportMiddleware.js.map +1 -0
  85. package/lib/esm/services/PassportSerializerService.js +39 -0
  86. package/lib/esm/services/PassportSerializerService.js.map +1 -0
  87. package/lib/esm/services/ProtocolsService.js +129 -0
  88. package/lib/esm/services/ProtocolsService.js.map +1 -0
  89. package/lib/esm/utils/getProtocolsFromRequest.js +32 -0
  90. package/lib/esm/utils/getProtocolsFromRequest.js.map +1 -0
  91. package/lib/types/PassportModule.d.ts +18 -0
  92. package/lib/types/contants/index.d.ts +1 -0
  93. package/lib/types/decorators/args.d.ts +6 -0
  94. package/lib/types/decorators/authenticate.d.ts +8 -0
  95. package/lib/types/decorators/authorize.d.ts +8 -0
  96. package/lib/types/decorators/protocol.d.ts +11 -0
  97. package/lib/types/domain/UserInfo.d.ts +5 -0
  98. package/lib/types/errors/PassportException.d.ts +4 -0
  99. package/lib/types/index.d.ts +12 -0
  100. package/lib/types/interfaces/IProtocol.d.ts +4 -0
  101. package/lib/types/interfaces/OnInstall.d.ts +4 -0
  102. package/lib/types/interfaces/OnVerify.d.ts +3 -0
  103. package/lib/types/interfaces/ProtocolOptions.d.ts +7 -0
  104. package/lib/types/interfaces/index.d.ts +20 -0
  105. package/lib/types/middlewares/PassportMiddleware.d.ts +7 -0
  106. package/lib/types/services/PassportSerializerService.d.ts +9 -0
  107. package/lib/types/services/ProtocolsService.d.ts +53 -0
  108. package/lib/types/utils/getProtocolsFromRequest.d.ts +4 -0
  109. package/package.json +21 -17
  110. package/tsconfig.compile.esm.json +23 -0
  111. package/lib/PassportModule.js.map +0 -1
  112. package/lib/contants/index.js.map +0 -1
  113. package/lib/decorators/args.js.map +0 -1
  114. package/lib/decorators/authenticate.js.map +0 -1
  115. package/lib/decorators/authorize.js.map +0 -1
  116. package/lib/decorators/protocol.js.map +0 -1
  117. package/lib/domain/UserInfo.js +0 -22
  118. package/lib/domain/UserInfo.js.map +0 -1
  119. package/lib/errors/PassportException.js.map +0 -1
  120. package/lib/index.js +0 -20
  121. package/lib/index.js.map +0 -1
  122. package/lib/interfaces/IProtocol.js.map +0 -1
  123. package/lib/interfaces/OnInstall.js.map +0 -1
  124. package/lib/interfaces/OnVerify.js.map +0 -1
  125. package/lib/interfaces/ProtocolOptions.js.map +0 -1
  126. package/lib/interfaces/index.js +0 -8
  127. package/lib/interfaces/index.js.map +0 -1
  128. package/lib/middlewares/PassportMiddleware.js.map +0 -1
  129. package/lib/services/PassportSerializerService.js.map +0 -1
  130. package/lib/services/ProtocolsService.js.map +0 -1
  131. package/lib/utils/getProtocolsFromRequest.js.map +0 -1
@@ -0,0 +1,21 @@
1
+ import { StoreSet, useDecorators } from "@tsed/core";
2
+ import { Configuration, Injectable } from "@tsed/di";
3
+ import { PROVIDER_TYPE_PROTOCOL } from "../contants";
4
+ /**
5
+ * Declare a new Protocol base on a Passport Strategy
6
+ *
7
+ * @decorator
8
+ * @class
9
+ */
10
+ export function Protocol(options) {
11
+ return useDecorators(Injectable({
12
+ type: PROVIDER_TYPE_PROTOCOL
13
+ }), StoreSet("protocol", options), Configuration({
14
+ passport: {
15
+ protocols: {
16
+ [options.name]: options
17
+ }
18
+ }
19
+ }));
20
+ }
21
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../src/decorators/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,aAAa,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AAEnD,OAAO,EAAC,sBAAsB,EAAC,MAAM,aAAa,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAU,OAAoC;IACpE,OAAO,aAAa,CAClB,UAAU,CAAC;QACT,IAAI,EAAE,sBAAsB;KAC7B,CAAC,EACF,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,EAC7B,aAAa,CAAC;QACZ,QAAQ,EAAE;YACR,SAAS,EAAE;gBACT,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO;aACxB;SACF;KACF,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {StoreSet, useDecorators} from \"@tsed/core\";\nimport {Configuration, Injectable} from \"@tsed/di\";\nimport {ProtocolOptions} from \"../interfaces/ProtocolOptions\";\nimport {PROVIDER_TYPE_PROTOCOL} from \"../contants\";\n\n/**\n * Declare a new Protocol base on a Passport Strategy\n *\n * @decorator\n * @class\n */\nexport function Protocol<T = any>(options: ProtocolOptionsDecorator<T>) {\n return useDecorators(\n Injectable({\n type: PROVIDER_TYPE_PROTOCOL\n }),\n StoreSet(\"protocol\", options),\n Configuration({\n passport: {\n protocols: {\n [options.name]: options\n }\n }\n })\n );\n}\n\nexport type ProtocolOptionsDecorator<T = any> = {name: string} & Partial<ProtocolOptions<T>>;\n"]}
@@ -0,0 +1,18 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Format, Property } from "@tsed/schema";
3
+ export class UserInfo {
4
+ }
5
+ __decorate([
6
+ Property(),
7
+ __metadata("design:type", String)
8
+ ], UserInfo.prototype, "id", void 0);
9
+ __decorate([
10
+ Property(),
11
+ Format("email"),
12
+ __metadata("design:type", String)
13
+ ], UserInfo.prototype, "email", void 0);
14
+ __decorate([
15
+ Property(),
16
+ __metadata("design:type", String)
17
+ ], UserInfo.prototype, "password", void 0);
18
+ //# sourceMappingURL=UserInfo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UserInfo.js","sourceRoot":"","sources":["../../../src/domain/UserInfo.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AAE9C,MAAM,OAAO,QAAQ;CAUpB;AARC;IADC,QAAQ,EAAE;;oCACA;AAIX;IAFC,QAAQ,EAAE;IACV,MAAM,CAAC,OAAO,CAAC;;uCACF;AAGd;IADC,QAAQ,EAAE;;0CACM","sourcesContent":["import {Format, Property} from \"@tsed/schema\";\n\nexport class UserInfo {\n @Property()\n id: string;\n\n @Property()\n @Format(\"email\")\n email: string;\n\n @Property()\n password: string;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ import { Exception } from "@tsed/exceptions";
2
+ export class PassportException extends Exception {
3
+ constructor(error) {
4
+ super(error.status || 500, error.message);
5
+ this.name = error.name || this.name;
6
+ }
7
+ }
8
+ //# sourceMappingURL=PassportException.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PassportException.js","sourceRoot":"","sources":["../../../src/errors/PassportException.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAE3C,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC9C,YAAY,KAAU;QACpB,KAAK,CAAC,KAAK,CAAC,MAAM,IAAI,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC;IACtC,CAAC;CACF","sourcesContent":["import {Exception} from \"@tsed/exceptions\";\n\nexport class PassportException extends Exception {\n constructor(error: any) {\n super(error.status || 500, error.message);\n this.name = error.name || this.name;\n }\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import "./PassportModule";
2
+ // Decorator
3
+ export * from "./decorators/authenticate";
4
+ export * from "./decorators/authorize";
5
+ export * from "./decorators/protocol";
6
+ export * from "./decorators/args";
7
+ // Interfaces
8
+ export * from "./interfaces";
9
+ export * from "./domain/UserInfo";
10
+ export * from "./errors/PassportException";
11
+ // Registries and Services
12
+ export * from "./contants";
13
+ export * from "./services/ProtocolsService";
14
+ export * from "./services/PassportSerializerService";
15
+ // Middlewares
16
+ export * from "./middlewares/PassportMiddleware";
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,YAAY;AACZ,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAElC,aAAa;AACb,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAE3C,0BAA0B;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sCAAsC,CAAC;AAErD,cAAc;AACd,cAAc,kCAAkC,CAAC","sourcesContent":["import \"./PassportModule\";\n\n// Decorator\nexport * from \"./decorators/authenticate\";\nexport * from \"./decorators/authorize\";\nexport * from \"./decorators/protocol\";\nexport * from \"./decorators/args\";\n\n// Interfaces\nexport * from \"./interfaces\";\nexport * from \"./domain/UserInfo\";\nexport * from \"./errors/PassportException\";\n\n// Registries and Services\nexport * from \"./contants\";\nexport * from \"./services/ProtocolsService\";\nexport * from \"./services/PassportSerializerService\";\n\n// Middlewares\nexport * from \"./middlewares/PassportMiddleware\";\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IProtocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IProtocol.js","sourceRoot":"","sources":["../../../src/interfaces/IProtocol.ts"],"names":[],"mappings":"","sourcesContent":["import {OnInstall} from \"./OnInstall\";\nimport {OnVerify} from \"./OnVerify\";\n\nexport interface IProtocol extends OnVerify, OnInstall {}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=OnInstall.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OnInstall.js","sourceRoot":"","sources":["../../../src/interfaces/OnInstall.ts"],"names":[],"mappings":"","sourcesContent":["import {Strategy} from \"passport\";\n\nexport interface OnInstall {\n $onInstall(strategy: Strategy): void;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=OnVerify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OnVerify.js","sourceRoot":"","sources":["../../../src/interfaces/OnVerify.ts"],"names":[],"mappings":"","sourcesContent":["export interface OnVerify {\n $onVerify(...args: any[]): Promise<any | any[]> | any | any[];\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ProtocolOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtocolOptions.js","sourceRoot":"","sources":["../../../src/interfaces/ProtocolOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {Strategy} from \"passport\";\n\nexport interface ProtocolOptions<T = any> {\n name: string;\n useStrategy: Type<Strategy>;\n settings: T;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export * from "./ProtocolOptions";
2
+ export * from "./IProtocol";
3
+ export * from "./OnInstall";
4
+ export * from "./OnVerify";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAkBA,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {ProtocolOptions} from \"./ProtocolOptions\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n passport: {\n userProperty?: string;\n pauseStream?: string;\n userInfoModel?: Type<any> | false;\n protocols?: {\n [protocolName: string]: Partial<ProtocolOptions>;\n };\n };\n }\n }\n}\n\nexport * from \"./ProtocolOptions\";\nexport * from \"./IProtocol\";\nexport * from \"./OnInstall\";\nexport * from \"./OnVerify\";\n"]}
@@ -0,0 +1,41 @@
1
+ var PassportMiddleware_1;
2
+ import { __decorate, __metadata, __param } from "tslib";
3
+ import { Context, Inject, Middleware } from "@tsed/common";
4
+ import { ProtocolsService } from "../services/ProtocolsService";
5
+ import { getProtocolsFromRequest } from "../utils/getProtocolsFromRequest";
6
+ let PassportMiddleware = PassportMiddleware_1 = class PassportMiddleware {
7
+ shouldSkip(ctx) {
8
+ const request = ctx.getRequest();
9
+ return request.user && request.isAuthenticated();
10
+ }
11
+ async use(ctx) {
12
+ const endpoint = ctx.endpoint;
13
+ const request = ctx.getRequest();
14
+ if (this.shouldSkip(ctx)) {
15
+ return;
16
+ }
17
+ const { options, protocol, method, originalUrl = true } = endpoint.store.get(PassportMiddleware_1);
18
+ const protocols = getProtocolsFromRequest(request, protocol, this.protocolsService.getProtocolsNames());
19
+ if (originalUrl) {
20
+ request.url = request.originalUrl;
21
+ }
22
+ await (method === "authenticate"
23
+ ? this.protocolsService.authenticate(protocols, options, ctx)
24
+ : this.protocolsService.authorize(protocols, options, ctx));
25
+ }
26
+ };
27
+ __decorate([
28
+ Inject(ProtocolsService),
29
+ __metadata("design:type", ProtocolsService)
30
+ ], PassportMiddleware.prototype, "protocolsService", void 0);
31
+ __decorate([
32
+ __param(0, Context()),
33
+ __metadata("design:type", Function),
34
+ __metadata("design:paramtypes", [Object]),
35
+ __metadata("design:returntype", Promise)
36
+ ], PassportMiddleware.prototype, "use", null);
37
+ PassportMiddleware = PassportMiddleware_1 = __decorate([
38
+ Middleware()
39
+ ], PassportMiddleware);
40
+ export { PassportMiddleware };
41
+ //# sourceMappingURL=PassportMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PassportMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PassportMiddleware.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAC,MAAM,cAAc,CAAC;AACzD,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAC,uBAAuB,EAAC,MAAM,kCAAkC,CAAC;AAGzE,IAAa,kBAAkB,0BAA/B,MAAa,kBAAkB;IAI7B,UAAU,CAAC,GAAY;QACrB,MAAM,OAAO,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;QACjC,OAAO,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,GAAG,CAAY,GAAY;QAC/B,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;QAC9B,MAAM,OAAO,GAAG,GAAG,CAAC,UAAU,EAAE,CAAC;QAEjC,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACxB,OAAO;SACR;QACD,MAAM,EAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,EAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,oBAAkB,CAAC,CAAC;QAC/F,MAAM,SAAS,GAAG,uBAAuB,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,gBAAgB,CAAC,iBAAiB,EAAE,CAAC,CAAC;QAExG,IAAI,WAAW,EAAE;YACf,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC;SACnC;QAED,MAAM,CAAC,MAAM,KAAK,cAAc;YAC9B,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,SAAS,EAAE,OAAO,EAAE,GAAG,CAAC;YAC7D,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,SAAS,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AAzBC;IADC,MAAM,CAAC,gBAAgB,CAAC;8BACP,gBAAgB;4DAAC;AAOnC;IAAW,WAAA,OAAO,EAAE,CAAA;;;;6CAiBnB;AA1BU,kBAAkB;IAD9B,UAAU,EAAE;GACA,kBAAkB,CA2B9B;SA3BY,kBAAkB","sourcesContent":["import {Context, Inject, Middleware} from \"@tsed/common\";\nimport {ProtocolsService} from \"../services/ProtocolsService\";\nimport {getProtocolsFromRequest} from \"../utils/getProtocolsFromRequest\";\n\n@Middleware()\nexport class PassportMiddleware {\n @Inject(ProtocolsService)\n protocolsService: ProtocolsService;\n\n shouldSkip(ctx: Context) {\n const request = ctx.getRequest();\n return request.user && request.isAuthenticated();\n }\n\n async use(@Context() ctx: Context) {\n const endpoint = ctx.endpoint;\n const request = ctx.getRequest();\n\n if (this.shouldSkip(ctx)) {\n return;\n }\n const {options, protocol, method, originalUrl = true} = endpoint.store.get(PassportMiddleware);\n const protocols = getProtocolsFromRequest(request, protocol, this.protocolsService.getProtocolsNames());\n\n if (originalUrl) {\n request.url = request.originalUrl;\n }\n\n await (method === \"authenticate\"\n ? this.protocolsService.authenticate(protocols, options, ctx)\n : this.protocolsService.authorize(protocols, options, ctx));\n }\n}\n"]}
@@ -0,0 +1,39 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Constant, Service } from "@tsed/di";
3
+ import { deserialize, serialize } from "@tsed/json-mapper";
4
+ import { UserInfo } from "../domain/UserInfo";
5
+ /**
6
+ * @ignore
7
+ */
8
+ let PassportSerializerService = class PassportSerializerService {
9
+ serialize(user, done) {
10
+ try {
11
+ const obj = serialize(user, { type: this.model });
12
+ // remove password from serialized object
13
+ delete obj.password;
14
+ done(null, JSON.stringify(obj));
15
+ }
16
+ catch (er) {
17
+ // istanbul ignore next
18
+ done(er);
19
+ }
20
+ }
21
+ deserialize(obj, done) {
22
+ try {
23
+ const user = deserialize(JSON.parse(obj), { type: this.model });
24
+ done(null, user);
25
+ }
26
+ catch (er) {
27
+ done(er);
28
+ }
29
+ }
30
+ };
31
+ __decorate([
32
+ Constant("passport.userInfoModel", UserInfo),
33
+ __metadata("design:type", Object)
34
+ ], PassportSerializerService.prototype, "model", void 0);
35
+ PassportSerializerService = __decorate([
36
+ Service()
37
+ ], PassportSerializerService);
38
+ export { PassportSerializerService };
39
+ //# sourceMappingURL=PassportSerializerService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PassportSerializerService.js","sourceRoot":"","sources":["../../../src/services/PassportSerializerService.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAC,WAAW,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AAEH,IAAa,yBAAyB,GAAtC,MAAa,yBAAyB;IAIpC,SAAS,CAAC,IAAc,EAAE,IAAS;QACjC,IAAI;YACF,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,EAAE,EAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAC,CAAC,CAAC;YAEhD,yCAAyC;YACzC,OAAO,GAAG,CAAC,QAAQ,CAAC;YAEpB,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;SACjC;QAAC,OAAO,EAAE,EAAE;YACX,uBAAuB;YACvB,IAAI,CAAC,EAAE,CAAC,CAAC;SACV;IACH,CAAC;IAED,WAAW,CAAC,GAAQ,EAAE,IAAS;QAC7B,IAAI;YACF,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAC,IAAI,EAAE,IAAI,CAAC,KAAK,EAAC,CAAC,CAAC;YAE9D,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;SAClB;QAAC,OAAO,EAAE,EAAE;YACX,IAAI,CAAC,EAAE,CAAC,CAAC;SACV;IACH,CAAC;CACF,CAAA;AAzBC;IADC,QAAQ,CAAC,wBAAwB,EAAE,QAAQ,CAAC;;wDACL;AAF7B,yBAAyB;IADrC,OAAO,EAAE;GACG,yBAAyB,CA2BrC;SA3BY,yBAAyB","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {Constant, Service} from \"@tsed/di\";\nimport {deserialize, serialize} from \"@tsed/json-mapper\";\nimport {UserInfo} from \"../domain/UserInfo\";\n\n/**\n * @ignore\n */\n@Service()\nexport class PassportSerializerService {\n @Constant(\"passport.userInfoModel\", UserInfo)\n private model: Type<UserInfo> | boolean;\n\n serialize(user: UserInfo, done: any) {\n try {\n const obj = serialize(user, {type: this.model});\n\n // remove password from serialized object\n delete obj.password;\n\n done(null, JSON.stringify(obj));\n } catch (er) {\n // istanbul ignore next\n done(er);\n }\n }\n\n deserialize(obj: any, done: any) {\n try {\n const user = deserialize(JSON.parse(obj), {type: this.model});\n\n done(null, user);\n } catch (er) {\n done(er);\n }\n }\n}\n"]}
@@ -0,0 +1,129 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { PlatformHandler } from "@tsed/common";
3
+ import { ancestorsOf } from "@tsed/core";
4
+ import { Injectable, InjectorService } from "@tsed/di";
5
+ import { Unauthorized } from "@tsed/exceptions";
6
+ import Passport, { Strategy } from "passport";
7
+ import { PassportException } from "../errors/PassportException";
8
+ import { PROVIDER_TYPE_PROTOCOL } from "../contants";
9
+ import { promisify } from "util";
10
+ /**
11
+ * @ignore
12
+ */
13
+ let ProtocolsService = class ProtocolsService {
14
+ constructor(injector) {
15
+ this.injector = injector;
16
+ this.strategies = new Map();
17
+ }
18
+ getProtocols() {
19
+ return Array.from(this.injector.getProviders(PROVIDER_TYPE_PROTOCOL));
20
+ }
21
+ getProtocolsNames() {
22
+ return Array.from(this.strategies.keys());
23
+ }
24
+ /**
25
+ * Invoke provider and bind it to passport.
26
+ * @param provider
27
+ */
28
+ invoke(provider) {
29
+ const { name, useStrategy: strategy, settings } = this.getOptions(provider);
30
+ const protocol = this.injector.get(provider.provide);
31
+ const instance = new strategy(settings, this.createHandler(provider));
32
+ this.strategies.set(name, instance);
33
+ Passport.use(name, instance);
34
+ if (protocol.$onInstall) {
35
+ protocol.$onInstall(instance);
36
+ }
37
+ protocol.$strategy = instance;
38
+ return protocol;
39
+ }
40
+ /**
41
+ * Call authenticate passport method.
42
+ * @param protocols
43
+ * @param options
44
+ * @param ctx
45
+ */
46
+ async authenticate(protocols, options, ctx) {
47
+ return this.call("authenticate", protocols, options, ctx);
48
+ }
49
+ /**
50
+ * Call authorize passport method.
51
+ * @param protocols
52
+ * @param options
53
+ * @param ctx
54
+ */
55
+ async authorize(protocols, options, ctx) {
56
+ return this.call("authorize", protocols, options, ctx);
57
+ }
58
+ /**
59
+ * Call passport authenticate or authorize depending on the chosen method.
60
+ * @param method
61
+ * @param protocols
62
+ * @param options
63
+ * @param ctx
64
+ * @private
65
+ */
66
+ async call(method, protocols, options, ctx) {
67
+ const request = ctx.getRequest();
68
+ const response = ctx.getResponse();
69
+ protocols = [].concat(protocols);
70
+ if (protocols.length === 0) {
71
+ throw new Unauthorized("Not authorized");
72
+ }
73
+ try {
74
+ options.failWithError = true;
75
+ // @ts-ignore
76
+ const fn = promisify(Passport[method](protocols.length === 1 ? protocols[0] : protocols, options));
77
+ await fn(request, response);
78
+ }
79
+ catch (er) {
80
+ if (!ancestorsOf(er).includes(Error)) {
81
+ throw new PassportException(er);
82
+ }
83
+ throw er;
84
+ }
85
+ }
86
+ /**
87
+ * Create strategy options based on decorator metadata and global configuration
88
+ * @param provider
89
+ * @private
90
+ */
91
+ getOptions(provider) {
92
+ const { name } = provider.store.get("protocol");
93
+ const { useStrategy = Strategy, settings = {} } = this.injector.settings.get(`passport.protocols.${name}`) || {};
94
+ return {
95
+ name,
96
+ useStrategy,
97
+ settings: {
98
+ ...settings,
99
+ passReqToCallback: true
100
+ }
101
+ };
102
+ }
103
+ /**
104
+ * Create the verifier handler for passport
105
+ * @param provider
106
+ * @private
107
+ */
108
+ createHandler(provider) {
109
+ const platformHandler = this.injector.get(PlatformHandler);
110
+ const middleware = platformHandler.createCustomHandler(provider, "$onVerify");
111
+ return async (req, ...args) => {
112
+ const done = args[args.length - 1];
113
+ req.$ctx.set("PROTOCOL_ARGS", args.slice(0, -1));
114
+ try {
115
+ await middleware(req.$ctx);
116
+ done(null, ...[].concat(req.$ctx.data));
117
+ }
118
+ catch (err) {
119
+ done(err, false, { message: err.message });
120
+ }
121
+ };
122
+ }
123
+ };
124
+ ProtocolsService = __decorate([
125
+ Injectable(),
126
+ __metadata("design:paramtypes", [InjectorService])
127
+ ], ProtocolsService);
128
+ export { ProtocolsService };
129
+ //# sourceMappingURL=ProtocolsService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtocolsService.js","sourceRoot":"","sources":["../../../src/services/ProtocolsService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAkB,eAAe,EAAC,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAC,WAAW,EAAC,MAAM,YAAY,CAAC;AACvC,OAAO,EAAC,UAAU,EAAE,eAAe,EAAW,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAC;AAC9C,OAAO,QAAQ,EAAE,EAAC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,CAAC,OAAO,EAAC,CAAC,CAAC;aAC1C;QACH,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AApIY,gBAAgB;IAD5B,UAAU,EAAE;qCAImB,eAAe;GAHlC,gBAAgB,CAoI5B;SApIY,gBAAgB","sourcesContent":["import {PlatformContext, PlatformHandler} from \"@tsed/common\";\nimport {ancestorsOf} from \"@tsed/core\";\nimport {Injectable, InjectorService, Provider} from \"@tsed/di\";\nimport {Unauthorized} from \"@tsed/exceptions\";\nimport Passport, {Strategy} from \"passport\";\nimport {PassportException} from \"../errors/PassportException\";\nimport {IProtocol, ProtocolOptions} from \"../interfaces\";\nimport {PROVIDER_TYPE_PROTOCOL} from \"../contants\";\nimport {promisify} from \"util\";\n\n/**\n * @ignore\n */\n@Injectable()\nexport class ProtocolsService {\n readonly strategies: Map<string, Strategy> = new Map();\n\n constructor(private injector: InjectorService) {}\n\n public getProtocols(): Provider[] {\n return Array.from(this.injector.getProviders(PROVIDER_TYPE_PROTOCOL));\n }\n\n public getProtocolsNames(): string[] {\n return Array.from(this.strategies.keys());\n }\n\n /**\n * Invoke provider and bind it to passport.\n * @param provider\n */\n public invoke(provider: Provider): any {\n const {name, useStrategy: strategy, settings} = this.getOptions(provider);\n const protocol = this.injector.get<IProtocol & any>(provider.provide)!;\n const instance = new strategy(settings, this.createHandler(provider));\n\n this.strategies.set(name, instance);\n\n Passport.use(name, instance);\n\n if (protocol.$onInstall) {\n protocol.$onInstall(instance);\n }\n\n protocol.$strategy = instance;\n\n return protocol;\n }\n\n /**\n * Call authenticate passport method.\n * @param protocols\n * @param options\n * @param ctx\n */\n public async authenticate(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext) {\n return this.call(\"authenticate\", protocols, options, ctx);\n }\n\n /**\n * Call authorize passport method.\n * @param protocols\n * @param options\n * @param ctx\n */\n public async authorize(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext) {\n return this.call(\"authorize\", protocols, options, ctx);\n }\n\n /**\n * Call passport authenticate or authorize depending on the chosen method.\n * @param method\n * @param protocols\n * @param options\n * @param ctx\n * @private\n */\n private async call(\n method: \"authenticate\" | \"authorize\",\n protocols: string | string[],\n options: Record<string, any>,\n ctx: PlatformContext\n ) {\n const request = ctx.getRequest();\n const response = ctx.getResponse();\n protocols = ([] as string[]).concat(protocols);\n\n if (protocols.length === 0) {\n throw new Unauthorized(\"Not authorized\");\n }\n\n try {\n options.failWithError = true;\n // @ts-ignore\n const fn = promisify(Passport[method](protocols.length === 1 ? protocols[0] : protocols, options));\n\n await fn(request, response);\n } catch (er) {\n if (!ancestorsOf(er).includes(Error)) {\n throw new PassportException(er);\n }\n\n throw er;\n }\n }\n\n /**\n * Create strategy options based on decorator metadata and global configuration\n * @param provider\n * @private\n */\n private getOptions(provider: Provider<any>): ProtocolOptions {\n const {name} = provider.store.get(\"protocol\");\n const {useStrategy = Strategy, settings = {}}: ProtocolOptions = this.injector.settings.get(`passport.protocols.${name}`) || {};\n\n return {\n name,\n useStrategy,\n settings: {\n ...settings,\n passReqToCallback: true\n }\n };\n }\n\n /**\n * Create the verifier handler for passport\n * @param provider\n * @private\n */\n private createHandler(provider: Provider<any>) {\n const platformHandler = this.injector.get<PlatformHandler>(PlatformHandler)!;\n const middleware = platformHandler.createCustomHandler(provider, \"$onVerify\");\n\n return async (req: any, ...args: any[]) => {\n const done = args[args.length - 1];\n req.$ctx.set(\"PROTOCOL_ARGS\", args.slice(0, -1));\n\n try {\n await middleware(req.$ctx);\n done(null, ...[].concat(req.$ctx.data));\n } catch (err) {\n done(err, false, {message: err.message});\n }\n };\n }\n}\n"]}
@@ -0,0 +1,32 @@
1
+ import { isArray } from "@tsed/core";
2
+ /**
3
+ * @ignore
4
+ */
5
+ function getProtocolName(req) {
6
+ const { query = {}, params = {}, body = {} } = req;
7
+ return params.protocol || query.protocol || body.protocol;
8
+ }
9
+ /**
10
+ * @ignore
11
+ */
12
+ export function getProtocolsFromRequest(req, protocol, defaultProtocols) {
13
+ if (!isArray(protocol)) {
14
+ if (protocol === "*") {
15
+ return defaultProtocols;
16
+ }
17
+ const protocolReq = getProtocolName(req);
18
+ if (protocol === ":protocol") {
19
+ return [protocolReq].filter(Boolean);
20
+ }
21
+ if (protocolReq && protocolReq !== protocol) {
22
+ return [];
23
+ }
24
+ return [protocol];
25
+ }
26
+ const protocols = protocol;
27
+ if (protocols.includes("*")) {
28
+ return defaultProtocols;
29
+ }
30
+ return protocols;
31
+ }
32
+ //# sourceMappingURL=getProtocolsFromRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getProtocolsFromRequest.js","sourceRoot":"","sources":["../../../src/utils/getProtocolsFromRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AAEnC;;GAEG;AACH,SAAS,eAAe,CAAC,GAAQ;IAC/B,MAAM,EAAC,KAAK,GAAG,EAAE,EAAE,MAAM,GAAG,EAAE,EAAE,IAAI,GAAG,EAAE,EAAC,GAAG,GAAG,CAAC;IAEjD,OAAO,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC;AAC5D,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAQ,EAAE,QAA2B,EAAE,gBAA0B;IACvG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QACtB,IAAI,QAAQ,KAAK,GAAG,EAAE;YACpB,OAAO,gBAAgB,CAAC;SACzB;QAED,MAAM,WAAW,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;QAEzC,IAAI,QAAQ,KAAK,WAAW,EAAE;YAC5B,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;SACtC;QAED,IAAI,WAAW,IAAI,WAAW,KAAK,QAAQ,EAAE;YAC3C,OAAO,EAAE,CAAC;SACX;QAED,OAAO,CAAC,QAAQ,CAAC,CAAC;KACnB;IAED,MAAM,SAAS,GAAa,QAAQ,CAAC;IAErC,IAAI,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QAC3B,OAAO,gBAAgB,CAAC;KACzB;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import {isArray} from \"@tsed/core\";\n\n/**\n * @ignore\n */\nfunction getProtocolName(req: any) {\n const {query = {}, params = {}, body = {}} = req;\n\n return params.protocol || query.protocol || body.protocol;\n}\n\n/**\n * @ignore\n */\nexport function getProtocolsFromRequest(req: any, protocol: string | string[], defaultProtocols: string[]): string[] {\n if (!isArray(protocol)) {\n if (protocol === \"*\") {\n return defaultProtocols;\n }\n\n const protocolReq = getProtocolName(req);\n\n if (protocol === \":protocol\") {\n return [protocolReq].filter(Boolean);\n }\n\n if (protocolReq && protocolReq !== protocol) {\n return [];\n }\n\n return [protocol];\n }\n\n const protocols: string[] = protocol;\n\n if (protocols.includes(\"*\")) {\n return defaultProtocols;\n }\n\n return protocols;\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import { BeforeRoutesInit, OnInit, PlatformApplication, Logger } from "@tsed/common";
2
+ import { PassportSerializerService } from "./services/PassportSerializerService";
3
+ import { ProtocolsService } from "./services/ProtocolsService";
4
+ /**
5
+ * @ignore
6
+ */
7
+ export declare class PassportModule implements OnInit, BeforeRoutesInit {
8
+ private app;
9
+ private protocolsService;
10
+ private passportSerializer;
11
+ userProperty: string;
12
+ pauseStream: boolean;
13
+ platformName: string;
14
+ logger: Logger;
15
+ constructor(app: PlatformApplication, protocolsService: ProtocolsService, passportSerializer: PassportSerializerService);
16
+ $onInit(): Promise<any> | void;
17
+ $beforeRoutesInit(): void | Promise<any>;
18
+ }
@@ -0,0 +1 @@
1
+ export declare const PROVIDER_TYPE_PROTOCOL = "protocol";
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Inject args resolve by the passport strategy on verify method.
3
+ * @decorator
4
+ */
5
+ export declare function Args(): any;
6
+ export declare function Arg(index: number): any;
@@ -0,0 +1,8 @@
1
+ import { AuthenticateOptions as PassportAuthenticateOptions } from "passport";
2
+ export interface AuthenticateOptions extends PassportAuthenticateOptions {
3
+ security?: {
4
+ [key: string]: string[];
5
+ };
6
+ originalUrl?: boolean;
7
+ }
8
+ export declare function Authenticate(protocol?: string | string[], options?: AuthenticateOptions): Function;
@@ -0,0 +1,8 @@
1
+ import { AuthenticateOptions } from "passport";
2
+ export interface AuthorizeOptions extends AuthenticateOptions {
3
+ security?: {
4
+ [key: string]: string[];
5
+ };
6
+ originalUrl?: boolean;
7
+ }
8
+ export declare function Authorize(protocol?: string | string[], options?: AuthorizeOptions): Function;
@@ -0,0 +1,11 @@
1
+ import { ProtocolOptions } from "../interfaces/ProtocolOptions";
2
+ /**
3
+ * Declare a new Protocol base on a Passport Strategy
4
+ *
5
+ * @decorator
6
+ * @class
7
+ */
8
+ export declare function Protocol<T = any>(options: ProtocolOptionsDecorator<T>): any;
9
+ export declare type ProtocolOptionsDecorator<T = any> = {
10
+ name: string;
11
+ } & Partial<ProtocolOptions<T>>;
@@ -0,0 +1,5 @@
1
+ export declare class UserInfo {
2
+ id: string;
3
+ email: string;
4
+ password: string;
5
+ }
@@ -0,0 +1,4 @@
1
+ import { Exception } from "@tsed/exceptions";
2
+ export declare class PassportException extends Exception {
3
+ constructor(error: any);
4
+ }
@@ -0,0 +1,12 @@
1
+ import "./PassportModule";
2
+ export * from "./decorators/authenticate";
3
+ export * from "./decorators/authorize";
4
+ export * from "./decorators/protocol";
5
+ export * from "./decorators/args";
6
+ export * from "./interfaces";
7
+ export * from "./domain/UserInfo";
8
+ export * from "./errors/PassportException";
9
+ export * from "./contants";
10
+ export * from "./services/ProtocolsService";
11
+ export * from "./services/PassportSerializerService";
12
+ export * from "./middlewares/PassportMiddleware";
@@ -0,0 +1,4 @@
1
+ import { OnInstall } from "./OnInstall";
2
+ import { OnVerify } from "./OnVerify";
3
+ export interface IProtocol extends OnVerify, OnInstall {
4
+ }
@@ -0,0 +1,4 @@
1
+ import { Strategy } from "passport";
2
+ export interface OnInstall {
3
+ $onInstall(strategy: Strategy): void;
4
+ }
@@ -0,0 +1,3 @@
1
+ export interface OnVerify {
2
+ $onVerify(...args: any[]): Promise<any | any[]> | any | any[];
3
+ }
@@ -0,0 +1,7 @@
1
+ import { Type } from "@tsed/core";
2
+ import { Strategy } from "passport";
3
+ export interface ProtocolOptions<T = any> {
4
+ name: string;
5
+ useStrategy: Type<Strategy>;
6
+ settings: T;
7
+ }
@@ -0,0 +1,20 @@
1
+ import { Type } from "@tsed/core";
2
+ import { ProtocolOptions } from "./ProtocolOptions";
3
+ declare global {
4
+ namespace TsED {
5
+ interface Configuration {
6
+ passport: {
7
+ userProperty?: string;
8
+ pauseStream?: string;
9
+ userInfoModel?: Type<any> | false;
10
+ protocols?: {
11
+ [protocolName: string]: Partial<ProtocolOptions>;
12
+ };
13
+ };
14
+ }
15
+ }
16
+ }
17
+ export * from "./ProtocolOptions";
18
+ export * from "./IProtocol";
19
+ export * from "./OnInstall";
20
+ export * from "./OnVerify";
@@ -0,0 +1,7 @@
1
+ import { Context } from "@tsed/common";
2
+ import { ProtocolsService } from "../services/ProtocolsService";
3
+ export declare class PassportMiddleware {
4
+ protocolsService: ProtocolsService;
5
+ shouldSkip(ctx: Context): any;
6
+ use(ctx: Context): Promise<void>;
7
+ }
@@ -0,0 +1,9 @@
1
+ import { UserInfo } from "../domain/UserInfo";
2
+ /**
3
+ * @ignore
4
+ */
5
+ export declare class PassportSerializerService {
6
+ private model;
7
+ serialize(user: UserInfo, done: any): void;
8
+ deserialize(obj: any, done: any): void;
9
+ }
@@ -0,0 +1,53 @@
1
+ import { PlatformContext } from "@tsed/common";
2
+ import { InjectorService, Provider } from "@tsed/di";
3
+ import { Strategy } from "passport";
4
+ /**
5
+ * @ignore
6
+ */
7
+ export declare class ProtocolsService {
8
+ private injector;
9
+ readonly strategies: Map<string, Strategy>;
10
+ constructor(injector: InjectorService);
11
+ getProtocols(): Provider[];
12
+ getProtocolsNames(): string[];
13
+ /**
14
+ * Invoke provider and bind it to passport.
15
+ * @param provider
16
+ */
17
+ invoke(provider: Provider): any;
18
+ /**
19
+ * Call authenticate passport method.
20
+ * @param protocols
21
+ * @param options
22
+ * @param ctx
23
+ */
24
+ authenticate(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext): Promise<void>;
25
+ /**
26
+ * Call authorize passport method.
27
+ * @param protocols
28
+ * @param options
29
+ * @param ctx
30
+ */
31
+ authorize(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext): Promise<void>;
32
+ /**
33
+ * Call passport authenticate or authorize depending on the chosen method.
34
+ * @param method
35
+ * @param protocols
36
+ * @param options
37
+ * @param ctx
38
+ * @private
39
+ */
40
+ private call;
41
+ /**
42
+ * Create strategy options based on decorator metadata and global configuration
43
+ * @param provider
44
+ * @private
45
+ */
46
+ private getOptions;
47
+ /**
48
+ * Create the verifier handler for passport
49
+ * @param provider
50
+ * @private
51
+ */
52
+ private createHandler;
53
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @ignore
3
+ */
4
+ export declare function getProtocolsFromRequest(req: any, protocol: string | string[], defaultProtocols: string[]): string[];