@tsed/passport 6.100.0 → 6.101.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.
- package/jest.config.js +14 -0
- package/lib/cjs/PassportModule.js +3 -2
- package/lib/cjs/PassportModule.js.map +1 -1
- package/lib/cjs/interfaces/{IProtocol.js → BeforeInstall.js} +1 -1
- package/lib/cjs/interfaces/BeforeInstall.js.map +1 -0
- package/lib/cjs/interfaces/OnInstall.js.map +1 -1
- package/lib/cjs/interfaces/ProtocolMethods.js +3 -0
- package/lib/cjs/interfaces/ProtocolMethods.js.map +1 -0
- package/lib/cjs/interfaces/ProtocolOptions.js.map +1 -1
- package/lib/cjs/interfaces/index.js +2 -1
- package/lib/cjs/interfaces/index.js.map +1 -1
- package/lib/cjs/services/ProtocolsService.js +6 -3
- package/lib/cjs/services/ProtocolsService.js.map +1 -1
- package/lib/esm/PassportModule.js +3 -2
- package/lib/esm/PassportModule.js.map +1 -1
- package/lib/esm/interfaces/BeforeInstall.js +2 -0
- package/lib/esm/interfaces/BeforeInstall.js.map +1 -0
- package/lib/esm/interfaces/OnInstall.js.map +1 -1
- package/lib/esm/interfaces/ProtocolMethods.js +2 -0
- package/lib/esm/interfaces/ProtocolMethods.js.map +1 -0
- package/lib/esm/interfaces/ProtocolOptions.js.map +1 -1
- package/lib/esm/interfaces/index.js +2 -1
- package/lib/esm/interfaces/index.js.map +1 -1
- package/lib/esm/services/ProtocolsService.js +6 -3
- package/lib/esm/services/ProtocolsService.js.map +1 -1
- package/lib/types/PassportModule.d.ts +1 -1
- package/lib/types/interfaces/BeforeInstall.d.ts +3 -0
- package/lib/types/interfaces/OnInstall.d.ts +1 -1
- package/lib/types/interfaces/ProtocolMethods.d.ts +10 -0
- package/lib/types/interfaces/ProtocolOptions.d.ts +2 -2
- package/lib/types/interfaces/index.d.ts +2 -1
- package/lib/types/services/ProtocolsService.d.ts +2 -1
- package/package.json +11 -10
- package/lib/cjs/interfaces/IProtocol.js.map +0 -1
- package/lib/esm/interfaces/IProtocol.js +0 -2
- package/lib/esm/interfaces/IProtocol.js.map +0 -1
- package/lib/types/interfaces/IProtocol.d.ts +0 -4
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "passport"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
statements: 99.36,
|
|
9
|
+
branches: 80.7,
|
|
10
|
+
functions: 100,
|
|
11
|
+
lines: 99.32
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -15,10 +15,11 @@ let PassportModule = class PassportModule {
|
|
|
15
15
|
this.protocolsService = protocolsService;
|
|
16
16
|
this.passportSerializer = passportSerializer;
|
|
17
17
|
}
|
|
18
|
-
$onInit() {
|
|
18
|
+
async $onInit() {
|
|
19
19
|
passport_1.default.serializeUser(this.passportSerializer.serialize.bind(this.passportSerializer));
|
|
20
20
|
passport_1.default.deserializeUser(this.passportSerializer.deserialize.bind(this.passportSerializer));
|
|
21
|
-
this.protocolsService.getProtocols().
|
|
21
|
+
const promises = this.protocolsService.getProtocols().map((provider) => this.protocolsService.invoke(provider));
|
|
22
|
+
await Promise.all(promises);
|
|
22
23
|
return undefined;
|
|
23
24
|
}
|
|
24
25
|
$beforeRoutesInit() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PassportModule.js","sourceRoot":"","sources":["../../src/PassportModule.ts"],"names":[],"mappings":";;;;AAAA,yCAAsI;AACtI,qEAAgC;AAChC,oFAA+E;AAC/E,kEAA6D;AAE7D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IAazB,YACU,GAAwB,EACxB,gBAAkC,EAClC,kBAA6C;QAF7C,QAAG,GAAH,GAAG,CAAqB;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;gBAClD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,kBAAQ,CAAC,OAAO,CAAC,EAAC,WAAW,EAAC,CAAC,CAAC,CAAC;gBAE9C,OAAO;YAET;gBACE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,IAAI,CAAC,YAAY,mCAAmC,CAAC,CAAC;SACvF;IACH,CAAC;CACF,CAAA;AAxCC;IADC,IAAA,iBAAQ,EAAC,uBAAuB,CAAC;;oDACb;AAGrB;IADC,IAAA,iBAAQ,EAAC,sBAAsB,CAAC;;mDACZ;AAGrB;IADC,IAAA,iBAAQ,EAAC,eAAe,CAAC;;oDACL;AAGrB;IADC,IAAA,eAAM,GAAE;2CACD,eAAM;8CAAC;AAXJ,cAAc;IAH1B,IAAA,eAAM,EAAC;QACN,KAAK,EAAE,sBAAa,CAAC,SAAS;KAC/B,CAAC;kDAee,4BAAmB;QACN,mCAAgB;QACd,qDAAyB;GAhB5C,cAAc,CA0C1B;AA1CY,wCAAc","sourcesContent":["import {BeforeRoutesInit, Constant, Inject, Module, OnInit, PlatformApplication, Provider, ProviderScope, Logger} from \"@tsed/common\";\nimport Passport from \"passport\";\nimport {PassportSerializerService} from \"./services/PassportSerializerService\";\nimport {ProtocolsService} from \"./services/ProtocolsService\";\n\n/**\n * @ignore\n */\n@Module({\n scope: ProviderScope.SINGLETON\n})\nexport class PassportModule implements OnInit, BeforeRoutesInit {\n @Constant(\"passport.userProperty\")\n userProperty: string;\n\n @Constant(\"passport.pauseStream\")\n pauseStream: boolean;\n\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n logger: Logger;\n\n constructor(\n private app: PlatformApplication,\n private protocolsService: ProtocolsService,\n private passportSerializer: PassportSerializerService\n ) {}\n\n async $onInit(): Promise<any> {\n Passport.serializeUser(this.passportSerializer.serialize.bind(this.passportSerializer));\n Passport.deserializeUser(this.passportSerializer.deserialize.bind(this.passportSerializer));\n\n const promises = this.protocolsService.getProtocols().map((provider: Provider) => this.protocolsService.invoke(provider));\n\n await Promise.all(promises);\n return undefined;\n }\n\n $beforeRoutesInit(): void | Promise<any> {\n const {userProperty, pauseStream} = this;\n switch (this.platformName) {\n case \"express\":\n this.app.use(Passport.initialize({userProperty}));\n this.app.use(Passport.session({pauseStream}));\n\n return;\n\n default:\n this.logger.warn(`Platform \"${this.platformName}\" not supported by @tsed/passport`);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BeforeInstall.js","sourceRoot":"","sources":["../../../src/interfaces/BeforeInstall.ts"],"names":[],"mappings":"","sourcesContent":["export interface BeforeInstall<Settings = any> {\n $beforeInstall(setting: Settings): Promise<Settings> | Settings | void;\n}\n"]}
|
|
@@ -1 +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
|
|
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 | Promise<void>;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProtocolMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ProtocolMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {OnInstall} from \"./OnInstall\";\nimport {OnVerify} from \"./OnVerify\";\nimport {BeforeInstall} from \"./BeforeInstall\";\n\n/**\n * @deprecated Use ProtocolMethods instead\n */\nexport interface IProtocol<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {}\nexport interface ProtocolMethods<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {}\n"]}
|
|
@@ -1 +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<
|
|
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<Settings = any> {\n name: string;\n useStrategy: Type<Strategy>;\n settings: Settings;\n}\n"]}
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
(0, tslib_1.__exportStar)(require("./ProtocolOptions"), exports);
|
|
5
|
-
(0, tslib_1.__exportStar)(require("./
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./ProtocolMethods"), exports);
|
|
6
6
|
(0, tslib_1.__exportStar)(require("./OnInstall"), exports);
|
|
7
7
|
(0, tslib_1.__exportStar)(require("./OnVerify"), exports);
|
|
8
|
+
(0, tslib_1.__exportStar)(require("./BeforeInstall"), exports);
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":";;;AAkBA,iEAAkC;AAClC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":";;;AAkBA,iEAAkC;AAClC,iEAAkC;AAClC,2DAA4B;AAC5B,0DAA2B;AAC3B,+DAAgC","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 \"./ProtocolMethods\";\nexport * from \"./OnInstall\";\nexport * from \"./OnVerify\";\nexport * from \"./BeforeInstall\";\n"]}
|
|
@@ -28,14 +28,17 @@ let ProtocolsService = class ProtocolsService {
|
|
|
28
28
|
* Invoke provider and bind it to passport.
|
|
29
29
|
* @param provider
|
|
30
30
|
*/
|
|
31
|
-
invoke(provider) {
|
|
32
|
-
|
|
31
|
+
async invoke(provider) {
|
|
32
|
+
let { name, useStrategy: strategy, settings } = this.getOptions(provider);
|
|
33
33
|
const protocol = this.injector.get(provider.provide);
|
|
34
|
+
if (protocol.$beforeInstall) {
|
|
35
|
+
settings = (await protocol.$beforeInstall(settings)) || settings;
|
|
36
|
+
}
|
|
34
37
|
const instance = new strategy(settings, this.createHandler(provider));
|
|
35
38
|
this.strategies.set(name, instance);
|
|
36
39
|
passport_1.default.use(name, instance);
|
|
37
40
|
if (protocol.$onInstall) {
|
|
38
|
-
protocol.$onInstall(instance);
|
|
41
|
+
await protocol.$onInstall(instance);
|
|
39
42
|
}
|
|
40
43
|
protocol.$strategy = instance;
|
|
41
44
|
return protocol;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProtocolsService.js","sourceRoot":"","sources":["../../../src/services/ProtocolsService.ts"],"names":[],"mappings":";;;;AAAA,yCAA8D;AAC9D,qCAAuC;AACvC,iCAA+D;AAC/D,iDAA8C;AAC9C,kEAA4C;AAC5C,mEAA8D;AAE9D,0CAAmD;AACnD,+BAA+B;AAE/B;;GAEG;AAEH,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAG3B,YAAoB,QAAyB;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;QAFpC,eAAU,GAA0B,IAAI,GAAG,EAAE,CAAC;IAEP,CAAC;IAE1C,YAAY;QACjB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,iCAAsB,CAAC,CAA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e,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YAEjD,IAAI;gBACF,MAAM,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAC3B,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;aACzC;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,EAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAC,CAAC,CAAC;aAC1C;QACH,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AAzIY,gBAAgB;IAD5B,IAAA,eAAU,GAAE;kDAImB,oBAAe;GAHlC,gBAAgB,CAyI5B;AAzIY,4CAAgB","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 {ProtocolMethods, 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 async invoke(provider: Provider) {\n let {name, useStrategy: strategy, settings} = this.getOptions(provider);\n const protocol = this.injector.get<ProtocolMethods & Record<string, any>>(provider.provide)!;\n\n if (protocol.$beforeInstall) {\n settings = (await protocol.$beforeInstall(settings)) || settings;\n }\n\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 await 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"]}
|
|
@@ -12,10 +12,11 @@ let PassportModule = class PassportModule {
|
|
|
12
12
|
this.protocolsService = protocolsService;
|
|
13
13
|
this.passportSerializer = passportSerializer;
|
|
14
14
|
}
|
|
15
|
-
$onInit() {
|
|
15
|
+
async $onInit() {
|
|
16
16
|
Passport.serializeUser(this.passportSerializer.serialize.bind(this.passportSerializer));
|
|
17
17
|
Passport.deserializeUser(this.passportSerializer.deserialize.bind(this.passportSerializer));
|
|
18
|
-
this.protocolsService.getProtocols().
|
|
18
|
+
const promises = this.protocolsService.getProtocols().map((provider) => this.protocolsService.invoke(provider));
|
|
19
|
+
await Promise.all(promises);
|
|
19
20
|
return undefined;
|
|
20
21
|
}
|
|
21
22
|
$beforeRoutesInit() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PassportModule.js","sourceRoot":"","sources":["../../src/PassportModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAmB,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAU,mBAAmB,EAAY,aAAa,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AACtI,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,EAAC,yBAAyB,EAAC,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAC,gBAAgB,EAAC,MAAM,6BAA6B,CAAC;AAE7D;;GAEG;AAIH,IAAa,cAAc,GAA3B,MAAa,cAAc;IAazB,YACU,GAAwB,EACxB,gBAAkC,EAClC,kBAA6C;QAF7C,QAAG,GAAH,GAAG,CAAqB;QACxB,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,uBAAkB,GAAlB,kBAAkB,CAA2B;IACpD,CAAC;IAEJ,OAAO;
|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AAC,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAC,YAAY,EAAC,CAAC,CAAC,CAAC;gBAClD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAC,WAAW,EAAC,CAAC,CAAC,CAAC;gBAE9C,OAAO;YAET;gBACE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,IAAI,CAAC,YAAY,mCAAmC,CAAC,CAAC;SACvF;IACH,CAAC;CACF,CAAA;AAxCC;IADC,QAAQ,CAAC,uBAAuB,CAAC;;oDACb;AAGrB;IADC,QAAQ,CAAC,sBAAsB,CAAC;;mDACZ;AAGrB;IADC,QAAQ,CAAC,eAAe,CAAC;;oDACL;AAGrB;IADC,MAAM,EAAE;8BACD,MAAM;8CAAC;AAXJ,cAAc;IAH1B,MAAM,CAAC;QACN,KAAK,EAAE,aAAa,CAAC,SAAS;KAC/B,CAAC;qCAee,mBAAmB;QACN,gBAAgB;QACd,yBAAyB;GAhB5C,cAAc,CA0C1B;SA1CY,cAAc","sourcesContent":["import {BeforeRoutesInit, Constant, Inject, Module, OnInit, PlatformApplication, Provider, ProviderScope, Logger} from \"@tsed/common\";\nimport Passport from \"passport\";\nimport {PassportSerializerService} from \"./services/PassportSerializerService\";\nimport {ProtocolsService} from \"./services/ProtocolsService\";\n\n/**\n * @ignore\n */\n@Module({\n scope: ProviderScope.SINGLETON\n})\nexport class PassportModule implements OnInit, BeforeRoutesInit {\n @Constant(\"passport.userProperty\")\n userProperty: string;\n\n @Constant(\"passport.pauseStream\")\n pauseStream: boolean;\n\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n logger: Logger;\n\n constructor(\n private app: PlatformApplication,\n private protocolsService: ProtocolsService,\n private passportSerializer: PassportSerializerService\n ) {}\n\n async $onInit(): Promise<any> {\n Passport.serializeUser(this.passportSerializer.serialize.bind(this.passportSerializer));\n Passport.deserializeUser(this.passportSerializer.deserialize.bind(this.passportSerializer));\n\n const promises = this.protocolsService.getProtocols().map((provider: Provider) => this.protocolsService.invoke(provider));\n\n await Promise.all(promises);\n return undefined;\n }\n\n $beforeRoutesInit(): void | Promise<any> {\n const {userProperty, pauseStream} = this;\n switch (this.platformName) {\n case \"express\":\n this.app.use(Passport.initialize({userProperty}));\n this.app.use(Passport.session({pauseStream}));\n\n return;\n\n default:\n this.logger.warn(`Platform \"${this.platformName}\" not supported by @tsed/passport`);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BeforeInstall.js","sourceRoot":"","sources":["../../../src/interfaces/BeforeInstall.ts"],"names":[],"mappings":"","sourcesContent":["export interface BeforeInstall<Settings = any> {\n $beforeInstall(setting: Settings): Promise<Settings> | Settings | void;\n}\n"]}
|
|
@@ -1 +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
|
|
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 | Promise<void>;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProtocolMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ProtocolMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {OnInstall} from \"./OnInstall\";\nimport {OnVerify} from \"./OnVerify\";\nimport {BeforeInstall} from \"./BeforeInstall\";\n\n/**\n * @deprecated Use ProtocolMethods instead\n */\nexport interface IProtocol<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {}\nexport interface ProtocolMethods<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {}\n"]}
|
|
@@ -1 +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<
|
|
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<Settings = any> {\n name: string;\n useStrategy: Type<Strategy>;\n settings: Settings;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAkBA,cAAc,mBAAmB,CAAC;AAClC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAkBA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,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 \"./ProtocolMethods\";\nexport * from \"./OnInstall\";\nexport * from \"./OnVerify\";\nexport * from \"./BeforeInstall\";\n"]}
|
|
@@ -25,14 +25,17 @@ let ProtocolsService = class ProtocolsService {
|
|
|
25
25
|
* Invoke provider and bind it to passport.
|
|
26
26
|
* @param provider
|
|
27
27
|
*/
|
|
28
|
-
invoke(provider) {
|
|
29
|
-
|
|
28
|
+
async invoke(provider) {
|
|
29
|
+
let { name, useStrategy: strategy, settings } = this.getOptions(provider);
|
|
30
30
|
const protocol = this.injector.get(provider.provide);
|
|
31
|
+
if (protocol.$beforeInstall) {
|
|
32
|
+
settings = (await protocol.$beforeInstall(settings)) || settings;
|
|
33
|
+
}
|
|
31
34
|
const instance = new strategy(settings, this.createHandler(provider));
|
|
32
35
|
this.strategies.set(name, instance);
|
|
33
36
|
Passport.use(name, instance);
|
|
34
37
|
if (protocol.$onInstall) {
|
|
35
|
-
protocol.$onInstall(instance);
|
|
38
|
+
await protocol.$onInstall(instance);
|
|
36
39
|
}
|
|
37
40
|
protocol.$strategy = instance;
|
|
38
41
|
return protocol;
|
|
@@ -1 +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,QAAQ,EAAC,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAC,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AAE9D,OAAO,EAAC,sBAAsB,EAAC,MAAM,aAAa,CAAC;AACnD,OAAO,EAAC,SAAS,EAAC,MAAM,MAAM,CAAC;AAE/B;;GAEG;AAEH,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAG3B,YAAoB,QAAyB;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;QAFpC,eAAU,GAA0B,IAAI,GAAG,EAAE,CAAC;IAEP,CAAC;IAE1C,YAAY;QACjB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,sBAAsB,CAAC,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AAe,CAAE,CAAC;QAC7E,MAAM,UAAU,GAAG,eAAe,CAAC,mBAAmB,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QAE9E,OAAO,KAAK,EAAE,GAAQ,EAAE,GAAG,IAAW,EAAE,EAAE;YACxC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YACnC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YAEjD,IAAI;gBACF,MAAM,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAC3B,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;aACzC;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,EAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAC,CAAC,CAAC;aAC1C;QACH,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AAzIY,gBAAgB;IAD5B,UAAU,EAAE;qCAImB,eAAe;GAHlC,gBAAgB,CAyI5B;SAzIY,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 {ProtocolMethods, 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 async invoke(provider: Provider) {\n let {name, useStrategy: strategy, settings} = this.getOptions(provider);\n const protocol = this.injector.get<ProtocolMethods & Record<string, any>>(provider.provide)!;\n\n if (protocol.$beforeInstall) {\n settings = (await protocol.$beforeInstall(settings)) || settings;\n }\n\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 await 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"]}
|
|
@@ -13,6 +13,6 @@ export declare class PassportModule implements OnInit, BeforeRoutesInit {
|
|
|
13
13
|
platformName: string;
|
|
14
14
|
logger: Logger;
|
|
15
15
|
constructor(app: PlatformApplication, protocolsService: ProtocolsService, passportSerializer: PassportSerializerService);
|
|
16
|
-
$onInit(): Promise<any
|
|
16
|
+
$onInit(): Promise<any>;
|
|
17
17
|
$beforeRoutesInit(): void | Promise<any>;
|
|
18
18
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { OnInstall } from "./OnInstall";
|
|
2
|
+
import { OnVerify } from "./OnVerify";
|
|
3
|
+
import { BeforeInstall } from "./BeforeInstall";
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated Use ProtocolMethods instead
|
|
6
|
+
*/
|
|
7
|
+
export interface IProtocol<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {
|
|
8
|
+
}
|
|
9
|
+
export interface ProtocolMethods<Settings = any> extends OnVerify, OnInstall, BeforeInstall<Settings> {
|
|
10
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PlatformContext } from "@tsed/common";
|
|
2
2
|
import { InjectorService, Provider } from "@tsed/di";
|
|
3
3
|
import { Strategy } from "passport";
|
|
4
|
+
import { ProtocolMethods } from "../interfaces";
|
|
4
5
|
/**
|
|
5
6
|
* @ignore
|
|
6
7
|
*/
|
|
@@ -14,7 +15,7 @@ export declare class ProtocolsService {
|
|
|
14
15
|
* Invoke provider and bind it to passport.
|
|
15
16
|
* @param provider
|
|
16
17
|
*/
|
|
17
|
-
invoke(provider: Provider): any
|
|
18
|
+
invoke(provider: Provider): Promise<ProtocolMethods<any> & Record<string, any>>;
|
|
18
19
|
/**
|
|
19
20
|
* Call authenticate passport method.
|
|
20
21
|
* @param protocols
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/passport",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.101.0",
|
|
4
4
|
"description": "Passport package for Ts.ED framework",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/cjs/index.js",
|
|
@@ -15,15 +15,16 @@
|
|
|
15
15
|
"build": "yarn run build:esm && yarn run build:cjs",
|
|
16
16
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
17
17
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
18
|
+
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2",
|
|
18
19
|
"start": "ts-node src/index.ts"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"tslib": "2.2.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"@tsed/common": "6.
|
|
25
|
-
"@tsed/core": "6.
|
|
26
|
-
"@tsed/di": "6.
|
|
25
|
+
"@tsed/common": "6.101.0",
|
|
26
|
+
"@tsed/core": "6.101.0",
|
|
27
|
+
"@tsed/di": "6.101.0",
|
|
27
28
|
"@types/passport": "1.0.4",
|
|
28
29
|
"@types/passport-http": "0.3.8",
|
|
29
30
|
"@types/passport-local": "1.0.34",
|
|
@@ -34,13 +35,13 @@
|
|
|
34
35
|
"passport-strategy": "1.0.0"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
|
-
"@tsed/common": "^6.
|
|
38
|
-
"@tsed/core": "^6.
|
|
39
|
-
"@tsed/di": "^6.
|
|
40
|
-
"@tsed/json-mapper": "^6.
|
|
38
|
+
"@tsed/common": "^6.101.0",
|
|
39
|
+
"@tsed/core": "^6.101.0",
|
|
40
|
+
"@tsed/di": "^6.101.0",
|
|
41
|
+
"@tsed/json-mapper": "^6.101.0",
|
|
41
42
|
"@tsed/logger": ">=6.0.0",
|
|
42
|
-
"@tsed/openspec": "^6.
|
|
43
|
-
"@tsed/schema": "^6.
|
|
43
|
+
"@tsed/openspec": "^6.101.0",
|
|
44
|
+
"@tsed/schema": "^6.101.0",
|
|
44
45
|
"passport": ">=0.4.1",
|
|
45
46
|
"passport-http": ">=0.3.0",
|
|
46
47
|
"passport-local": ">=1.0.0",
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1 +0,0 @@
|
|
|
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"]}
|