@tsed/passport 8.11.1 → 8.11.2-rc.1

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 (71) hide show
  1. package/lib/esm/PassportModule.js +0 -1
  2. package/lib/esm/contants/constants.js +0 -1
  3. package/lib/esm/decorators/args.js +0 -1
  4. package/lib/esm/decorators/authenticate.js +0 -1
  5. package/lib/esm/decorators/authorize.js +0 -1
  6. package/lib/esm/decorators/protocol.js +0 -1
  7. package/lib/esm/domain/UserInfo.js +0 -1
  8. package/lib/esm/errors/PassportException.js +0 -1
  9. package/lib/esm/errors/PassportMessage.js +0 -1
  10. package/lib/esm/index.js +0 -1
  11. package/lib/esm/interfaces/BeforeInstall.js +0 -1
  12. package/lib/esm/interfaces/OnInstall.js +0 -1
  13. package/lib/esm/interfaces/OnVerify.js +0 -1
  14. package/lib/esm/interfaces/ProtocolMethods.js +0 -1
  15. package/lib/esm/interfaces/ProtocolOptions.js +0 -1
  16. package/lib/esm/interfaces/interfaces.js +0 -1
  17. package/lib/esm/middlewares/PassportMiddleware.js +0 -1
  18. package/lib/esm/services/PassportSerializerService.js +0 -1
  19. package/lib/esm/services/ProtocolsService.js +0 -1
  20. package/lib/esm/utils/getProtocolsFromRequest.js +0 -1
  21. package/package.json +11 -8
  22. package/lib/esm/PassportModule.js.map +0 -1
  23. package/lib/esm/contants/constants.js.map +0 -1
  24. package/lib/esm/decorators/args.js.map +0 -1
  25. package/lib/esm/decorators/authenticate.js.map +0 -1
  26. package/lib/esm/decorators/authorize.js.map +0 -1
  27. package/lib/esm/decorators/protocol.js.map +0 -1
  28. package/lib/esm/domain/UserInfo.js.map +0 -1
  29. package/lib/esm/errors/PassportException.js.map +0 -1
  30. package/lib/esm/errors/PassportMessage.js.map +0 -1
  31. package/lib/esm/index.js.map +0 -1
  32. package/lib/esm/interfaces/BeforeInstall.js.map +0 -1
  33. package/lib/esm/interfaces/OnInstall.js.map +0 -1
  34. package/lib/esm/interfaces/OnVerify.js.map +0 -1
  35. package/lib/esm/interfaces/ProtocolMethods.js.map +0 -1
  36. package/lib/esm/interfaces/ProtocolOptions.js.map +0 -1
  37. package/lib/esm/interfaces/interfaces.js.map +0 -1
  38. package/lib/esm/middlewares/PassportMiddleware.js.map +0 -1
  39. package/lib/esm/services/PassportSerializerService.js.map +0 -1
  40. package/lib/esm/services/ProtocolsService.js.map +0 -1
  41. package/lib/esm/utils/getProtocolsFromRequest.js.map +0 -1
  42. package/src/PassportModule.spec.ts +0 -75
  43. package/src/PassportModule.ts +0 -58
  44. package/src/contants/constants.ts +0 -1
  45. package/src/decorators/__snapshots__/authorize.spec.ts.snap +0 -53
  46. package/src/decorators/args.spec.ts +0 -28
  47. package/src/decorators/args.ts +0 -14
  48. package/src/decorators/authenticate.spec.ts +0 -55
  49. package/src/decorators/authenticate.ts +0 -25
  50. package/src/decorators/authorize.spec.ts +0 -96
  51. package/src/decorators/authorize.ts +0 -25
  52. package/src/decorators/protocol.ts +0 -29
  53. package/src/domain/UserInfo.ts +0 -13
  54. package/src/errors/PassportException.ts +0 -8
  55. package/src/errors/PassportMessage.ts +0 -10
  56. package/src/index.ts +0 -22
  57. package/src/interfaces/BeforeInstall.ts +0 -3
  58. package/src/interfaces/OnInstall.ts +0 -5
  59. package/src/interfaces/OnVerify.ts +0 -3
  60. package/src/interfaces/ProtocolMethods.ts +0 -5
  61. package/src/interfaces/ProtocolOptions.ts +0 -8
  62. package/src/interfaces/interfaces.ts +0 -21
  63. package/src/middlewares/PassportMiddleware.spec.ts +0 -301
  64. package/src/middlewares/PassportMiddleware.ts +0 -36
  65. package/src/services/PassportSerializerService.spec.ts +0 -51
  66. package/src/services/PassportSerializerService.ts +0 -38
  67. package/src/services/ProtocolsService.spec.ts +0 -166
  68. package/src/services/ProtocolsService.ts +0 -169
  69. package/src/utils/getProtocolsFromRequest.spec.ts +0 -97
  70. package/src/utils/getProtocolsFromRequest.ts +0 -41
  71. package/vitest.config.mts +0 -21
@@ -1,166 +0,0 @@
1
- import {runInContext} from "@tsed/di";
2
- import {Req} from "@tsed/platform-http";
3
- import {PlatformTest} from "@tsed/platform-http/testing";
4
- import Passport from "passport";
5
-
6
- import {PassportMessage} from "../errors/PassportMessage.js";
7
- import {Protocol, ProtocolsService} from "../index.js";
8
-
9
- // tslint:disable-next-line:variable-name
10
- const Strategy = vi.fn();
11
-
12
- describe("ProtocolsService", () => {
13
- @Protocol({
14
- name: "local",
15
- useStrategy: Strategy as any,
16
- settings: {
17
- prop1: "prop1",
18
- prop2: "prop2"
19
- }
20
- })
21
- class LocalProtocol {
22
- $beforeInstall(settings: any) {
23
- return Promise.resolve(settings);
24
- }
25
-
26
- $onInstall() {}
27
-
28
- $onVerify(@Req() req: Req) {
29
- return {id: 0};
30
- }
31
- }
32
-
33
- beforeEach(() =>
34
- PlatformTest.create({
35
- passport: {
36
- protocols: {
37
- local: {
38
- settings: {
39
- prop2: "prop2-server",
40
- prop3: "prop3"
41
- }
42
- }
43
- }
44
- }
45
- })
46
- );
47
- afterEach(PlatformTest.reset);
48
-
49
- beforeEach(() => {
50
- vi.spyOn(LocalProtocol.prototype, "$onInstall");
51
- vi.spyOn(LocalProtocol.prototype, "$onVerify");
52
- vi.spyOn(LocalProtocol.prototype, "$beforeInstall");
53
- vi.spyOn(Passport, "use").mockReturnValue(undefined as any);
54
- Strategy.mockReset();
55
- });
56
-
57
- it("should create a protocol", async () => {
58
- const protocolService = PlatformTest.get<ProtocolsService>(ProtocolsService);
59
- // GIVEN
60
- const provider = PlatformTest.injector.getProvider(LocalProtocol)!;
61
-
62
- // WHEN
63
- const result = await protocolService.invoke(provider);
64
-
65
- // THEN
66
- expect(result).toBeInstanceOf(LocalProtocol);
67
- expect(result.$onInstall).toHaveBeenCalledWith(protocolService.strategies.get("local"));
68
- expect(result.$beforeInstall).toHaveBeenCalledWith({
69
- passReqToCallback: true,
70
- prop1: "prop1",
71
- prop2: "prop2-server",
72
- prop3: "prop3"
73
- });
74
- expect(Passport.use).toHaveBeenCalledWith("local", protocolService.strategies.get("local"));
75
- expect(Strategy).toHaveBeenCalledWith(
76
- {
77
- passReqToCallback: true,
78
- prop1: "prop1",
79
- prop2: "prop2-server",
80
- prop3: "prop3"
81
- },
82
- expect.any(Function)
83
- );
84
- expect(protocolService.getProtocolsNames().includes("local")).toEqual(true);
85
- });
86
-
87
- it("should call metadata", async () => {
88
- const protocolService = PlatformTest.get<ProtocolsService>(ProtocolsService);
89
- // GIVEN
90
- (LocalProtocol.prototype.$onVerify as any).mockReturnValue({id: 0});
91
- const provider = PlatformTest.injector.getProvider(LocalProtocol)!;
92
- const $ctx = PlatformTest.createRequestContext();
93
-
94
- // WHEN
95
- const protocol = await protocolService.invoke(provider);
96
-
97
- const resultDone: any = await new Promise((resolve) => {
98
- const verify = Strategy.mock.calls[0][1];
99
-
100
- return runInContext($ctx, () => verify($ctx.getRequest(), "test", (...args: any[]) => resolve(args)));
101
- });
102
-
103
- // THEN
104
- expect(resultDone).toEqual([null, {id: 0}]);
105
- expect(protocol.$onVerify).toHaveBeenCalledWith($ctx.getRequest(), $ctx);
106
- });
107
-
108
- it("should call metadata and catch error", async () => {
109
- const protocolService = PlatformTest.get<ProtocolsService>(ProtocolsService);
110
- // GIVEN
111
- const error = new Error("message");
112
- (LocalProtocol.prototype.$onVerify as any).mockRejectedValue(error);
113
-
114
- const provider = PlatformTest.injector.getProvider(LocalProtocol)!;
115
- const $ctx = PlatformTest.createRequestContext();
116
-
117
- // WHEN
118
- const result = await protocolService.invoke(provider);
119
- const resultDone: any = await new Promise((resolve) => {
120
- const verify = Strategy.mock.calls[0][1];
121
-
122
- return runInContext($ctx, () => verify($ctx.getRequest(), "test", (...args: any[]) => resolve(args)));
123
- });
124
-
125
- // THEN
126
- expect(result.$onVerify).toHaveBeenCalledWith($ctx.getRequest(), $ctx);
127
- expect(resultDone).toEqual([error, false, {message: "message"}]);
128
- });
129
-
130
- it("should call metadata and catch PassportMessage", async () => {
131
- const protocolService = PlatformTest.get<ProtocolsService>(ProtocolsService);
132
- // GIVEN
133
- const error = new PassportMessage("message", {type: "allow"});
134
- (LocalProtocol.prototype.$onVerify as any).mockRejectedValue(error);
135
-
136
- const provider = PlatformTest.injector.getProvider(LocalProtocol)!;
137
- const $ctx = PlatformTest.createRequestContext();
138
-
139
- // WHEN
140
- const result = await protocolService.invoke(provider);
141
- const resultDone: any = await new Promise((resolve) => {
142
- const verify = Strategy.mock.calls[0][1];
143
-
144
- return runInContext($ctx, () => verify($ctx.getRequest(), "test", (...args: any[]) => resolve(args)));
145
- });
146
-
147
- // THEN
148
- expect(result.$onVerify).toHaveBeenCalledWith($ctx.getRequest(), $ctx);
149
- expect(resultDone).toEqual([null, false, {message: "message", type: "allow"}]);
150
- });
151
-
152
- it("should call metadata and catch missing $ctx", async () => {
153
- const protocolService = PlatformTest.get<ProtocolsService>(ProtocolsService);
154
- // GIVEN
155
- const provider = PlatformTest.injector.getProvider(LocalProtocol)!;
156
-
157
- // WHEN
158
- await protocolService.invoke(provider);
159
- const resultDone: any = await new Promise((resolve) => {
160
- Strategy.mock.calls[0][1]({}, "test", (...args: any[]) => resolve(args));
161
- });
162
-
163
- // THEN
164
- expect(resultDone).toEqual([new Error("Headers already sent"), false, {message: "Headers already sent"}]);
165
- });
166
- });
@@ -1,169 +0,0 @@
1
- import {promisify} from "node:util";
2
-
3
- import {ancestorsOf} from "@tsed/core";
4
- import {Inject, Injectable, InjectorService, Provider} from "@tsed/di";
5
- import {Unauthorized} from "@tsed/exceptions";
6
- import {PlatformContext, PlatformHandler} from "@tsed/platform-http";
7
- import Passport, {Strategy} from "passport";
8
-
9
- import {PROVIDER_TYPE_PROTOCOL} from "../contants/constants.js";
10
- import {PassportException} from "../errors/PassportException.js";
11
- import {PassportMessage} from "../errors/PassportMessage.js";
12
- import type {ProtocolMethods} from "../interfaces/ProtocolMethods.js";
13
- import type {ProtocolOptions} from "../interfaces/ProtocolOptions.js";
14
-
15
- /**
16
- * @ignore
17
- */
18
- @Injectable()
19
- export class ProtocolsService {
20
- readonly strategies: Map<string, Strategy> = new Map();
21
-
22
- @Inject()
23
- protected platformHandler: PlatformHandler;
24
-
25
- @Inject()
26
- private injector: InjectorService;
27
-
28
- public getProtocols(): Provider[] {
29
- return this.injector.getProviders(PROVIDER_TYPE_PROTOCOL);
30
- }
31
-
32
- public getProtocolsNames(): string[] {
33
- return Array.from(this.strategies.keys());
34
- }
35
-
36
- /**
37
- * Invoke provider and bind it to passport.
38
- * @param provider
39
- */
40
- async invoke(provider: Provider) {
41
- let {name, useStrategy: strategy, settings} = this.getOptions(provider);
42
- const protocol = this.injector.get<ProtocolMethods & Record<string, any>>(provider.provide)!;
43
-
44
- if (protocol.$beforeInstall) {
45
- settings = (await protocol.$beforeInstall(settings)) || settings;
46
- }
47
-
48
- const instance = new strategy(settings, this.createHandler(provider));
49
-
50
- this.strategies.set(name, instance);
51
-
52
- Passport.use(name, instance);
53
-
54
- if (protocol.$onInstall) {
55
- await protocol.$onInstall(instance);
56
- }
57
-
58
- protocol.$strategy = instance;
59
-
60
- return protocol;
61
- }
62
-
63
- /**
64
- * Call authenticate passport method.
65
- * @param protocols
66
- * @param options
67
- * @param ctx
68
- */
69
- public authenticate(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext) {
70
- return this.call("authenticate", protocols, options, ctx);
71
- }
72
-
73
- /**
74
- * Call authorize passport method.
75
- * @param protocols
76
- * @param options
77
- * @param ctx
78
- */
79
- public authorize(protocols: string | string[], options: Record<string, any>, ctx: PlatformContext) {
80
- return this.call("authorize", protocols, options, ctx);
81
- }
82
-
83
- /**
84
- * Call passport authenticate or authorize depending on the chosen method.
85
- * @param method
86
- * @param protocols
87
- * @param options
88
- * @param ctx
89
- * @private
90
- */
91
- private async call(
92
- method: "authenticate" | "authorize",
93
- protocols: string | string[],
94
- options: Record<string, any>,
95
- ctx: PlatformContext
96
- ) {
97
- const request = ctx.getRequest();
98
- const response = ctx.getResponse();
99
- protocols = ([] as string[]).concat(protocols);
100
-
101
- if (protocols.length === 0) {
102
- throw new Unauthorized("Not authorized");
103
- }
104
-
105
- try {
106
- options.failWithError = true;
107
- // @ts-ignore
108
- const fn = promisify(Passport[method](protocols.length === 1 ? protocols[0] : protocols, options));
109
-
110
- await fn(request, response);
111
- } catch (er) {
112
- if (!ancestorsOf(er).includes(Error)) {
113
- throw new PassportException(er);
114
- }
115
-
116
- throw er;
117
- }
118
- }
119
-
120
- /**
121
- * Create strategy options based on decorator metadata and global configuration
122
- * @param provider
123
- * @private
124
- */
125
- private getOptions(provider: Provider<any>): ProtocolOptions {
126
- const {name} = provider.store.get("protocol");
127
- const {useStrategy = Strategy, settings = {}}: ProtocolOptions = this.injector.settings.get(`passport.protocols.${name}`) || {};
128
-
129
- return {
130
- name,
131
- useStrategy,
132
- settings: {
133
- ...settings,
134
- passReqToCallback: true
135
- }
136
- };
137
- }
138
-
139
- /**
140
- * Create the verifier handler for passport
141
- * @param provider
142
- * @private
143
- */
144
- private createHandler(provider: Provider) {
145
- const middleware = this.platformHandler.createCustomHandler(provider, "$onVerify");
146
-
147
- return async (req: any, ...args: any[]) => {
148
- const done = args[args.length - 1];
149
-
150
- if (req.$ctx) {
151
- req.$ctx.set("PROTOCOL_ARGS", args.slice(0, -1));
152
-
153
- try {
154
- await middleware(req.$ctx);
155
-
156
- done(null, ...[].concat(req.$ctx.data));
157
- } catch (err) {
158
- if (err instanceof PassportMessage) {
159
- done(null, false, {...err.opts, message: err.message});
160
- } else {
161
- done(err, false, {message: err.message});
162
- }
163
- }
164
- } else {
165
- done(new Error("Headers already sent"), false, {message: "Headers already sent"});
166
- }
167
- };
168
- }
169
- }
@@ -1,97 +0,0 @@
1
- import {getProtocolsFromRequest} from "./getProtocolsFromRequest.js";
2
-
3
- describe("getProtocolsFromRequest", () => {
4
- it("should allow all protocol (from default protocols)", () => {
5
- const defaultProtocols = ["default"];
6
- const req = {};
7
- const result = getProtocolsFromRequest(req, "*", defaultProtocols);
8
-
9
- expect(result).toEqual(["default"]);
10
- });
11
-
12
- it("should allow all protocol (from default protocols - array)", () => {
13
- const defaultProtocols = ["default"];
14
- const req = {};
15
- const result = getProtocolsFromRequest(req, ["*"], defaultProtocols);
16
-
17
- expect(result).toEqual(["default"]);
18
- });
19
-
20
- it("should return providers", () => {
21
- const defaultProtocols = ["default"];
22
- const req = {};
23
- const result = getProtocolsFromRequest(req, ["basic", "local"], defaultProtocols);
24
-
25
- expect(result).toEqual(["basic", "local"]);
26
- });
27
-
28
- it("should get protocol from request (params)", () => {
29
- const defaultProtocols = ["default"];
30
- const req = {
31
- params: {
32
- protocol: "basic"
33
- }
34
- };
35
- const result = getProtocolsFromRequest(req, ":protocol", defaultProtocols);
36
-
37
- expect(result).toEqual(["basic"]);
38
- });
39
-
40
- it("should not get protocol from request", () => {
41
- const defaultProtocols = ["default"];
42
- const req = {
43
- params: {}
44
- };
45
- const result = getProtocolsFromRequest(req, ":protocol", defaultProtocols);
46
-
47
- expect(result).toEqual([]);
48
- });
49
-
50
- it("should get protocol from request (query)", () => {
51
- const defaultProtocols = ["default"];
52
- const req = {
53
- query: {
54
- protocol: "basic"
55
- }
56
- };
57
- const result = getProtocolsFromRequest(req, ":protocol", defaultProtocols);
58
-
59
- expect(result).toEqual(["basic"]);
60
- });
61
-
62
- it("should get protocol from request (body)", () => {
63
- const defaultProtocols = ["default"];
64
- const req = {
65
- body: {
66
- protocol: "basic"
67
- }
68
- };
69
- const result = getProtocolsFromRequest(req, ":protocol", defaultProtocols);
70
-
71
- expect(result).toEqual(["basic"]);
72
- });
73
-
74
- it("should return basic protocol", () => {
75
- const defaultProtocols = ["default"];
76
- const req = {
77
- params: {
78
- protocol: "basic"
79
- }
80
- };
81
- const result = getProtocolsFromRequest(req, "basic", defaultProtocols);
82
-
83
- expect(result).toEqual(["basic"]);
84
- });
85
-
86
- it("should not return protocol when protocol doesn't match", () => {
87
- const defaultProtocols = ["default"];
88
- const req = {
89
- params: {
90
- protocol: "basic"
91
- }
92
- };
93
- const result = getProtocolsFromRequest(req, "other", defaultProtocols);
94
-
95
- expect(result).toEqual([]);
96
- });
97
- });
@@ -1,41 +0,0 @@
1
- import {isArray} from "@tsed/core";
2
-
3
- /**
4
- * @ignore
5
- */
6
- function getProtocolName(req: any) {
7
- const {query = {}, params = {}, body = {}} = req;
8
-
9
- return params.protocol || query.protocol || body.protocol;
10
- }
11
-
12
- /**
13
- * @ignore
14
- */
15
- export function getProtocolsFromRequest(req: any, protocol: string | string[], defaultProtocols: string[]): string[] {
16
- if (!isArray(protocol)) {
17
- if (protocol === "*") {
18
- return defaultProtocols;
19
- }
20
-
21
- const protocolReq = getProtocolName(req);
22
-
23
- if (protocol === ":protocol") {
24
- return [protocolReq].filter(Boolean);
25
- }
26
-
27
- if (protocolReq && protocolReq !== protocol) {
28
- return [];
29
- }
30
-
31
- return [protocol];
32
- }
33
-
34
- const protocols: string[] = protocol;
35
-
36
- if (protocols.includes("*")) {
37
- return defaultProtocols;
38
- }
39
-
40
- return protocols;
41
- }
package/vitest.config.mts DELETED
@@ -1,21 +0,0 @@
1
- // @ts-ignore
2
- import {presets} from "@tsed/vitest/presets";
3
- import {defineConfig} from "vitest/config";
4
-
5
- export default defineConfig(
6
- {
7
- ...presets,
8
- test: {
9
- ...presets.test,
10
- coverage: {
11
- ...presets.test.coverage,
12
- thresholds: {
13
- statements: 0,
14
- branches: 0,
15
- functions: 0,
16
- lines: 0
17
- }
18
- }
19
- }
20
- }
21
- );