@tsed/passport 8.11.0 → 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.
- package/lib/esm/PassportModule.js +0 -1
- package/lib/esm/contants/constants.js +0 -1
- package/lib/esm/decorators/args.js +0 -1
- package/lib/esm/decorators/authenticate.js +0 -1
- package/lib/esm/decorators/authorize.js +0 -1
- package/lib/esm/decorators/protocol.js +0 -1
- package/lib/esm/domain/UserInfo.js +0 -1
- package/lib/esm/errors/PassportException.js +0 -1
- package/lib/esm/errors/PassportMessage.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/interfaces/BeforeInstall.js +0 -1
- package/lib/esm/interfaces/OnInstall.js +0 -1
- package/lib/esm/interfaces/OnVerify.js +0 -1
- package/lib/esm/interfaces/ProtocolMethods.js +0 -1
- package/lib/esm/interfaces/ProtocolOptions.js +0 -1
- package/lib/esm/interfaces/interfaces.js +0 -1
- package/lib/esm/middlewares/PassportMiddleware.js +0 -1
- package/lib/esm/services/PassportSerializerService.js +0 -1
- package/lib/esm/services/ProtocolsService.js +0 -1
- package/lib/esm/utils/getProtocolsFromRequest.js +0 -1
- package/package.json +11 -8
- package/lib/esm/PassportModule.js.map +0 -1
- package/lib/esm/contants/constants.js.map +0 -1
- package/lib/esm/decorators/args.js.map +0 -1
- package/lib/esm/decorators/authenticate.js.map +0 -1
- package/lib/esm/decorators/authorize.js.map +0 -1
- package/lib/esm/decorators/protocol.js.map +0 -1
- package/lib/esm/domain/UserInfo.js.map +0 -1
- package/lib/esm/errors/PassportException.js.map +0 -1
- package/lib/esm/errors/PassportMessage.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/BeforeInstall.js.map +0 -1
- package/lib/esm/interfaces/OnInstall.js.map +0 -1
- package/lib/esm/interfaces/OnVerify.js.map +0 -1
- package/lib/esm/interfaces/ProtocolMethods.js.map +0 -1
- package/lib/esm/interfaces/ProtocolOptions.js.map +0 -1
- package/lib/esm/interfaces/interfaces.js.map +0 -1
- package/lib/esm/middlewares/PassportMiddleware.js.map +0 -1
- package/lib/esm/services/PassportSerializerService.js.map +0 -1
- package/lib/esm/services/ProtocolsService.js.map +0 -1
- package/lib/esm/utils/getProtocolsFromRequest.js.map +0 -1
- package/src/PassportModule.spec.ts +0 -75
- package/src/PassportModule.ts +0 -58
- package/src/contants/constants.ts +0 -1
- package/src/decorators/__snapshots__/authorize.spec.ts.snap +0 -53
- package/src/decorators/args.spec.ts +0 -28
- package/src/decorators/args.ts +0 -14
- package/src/decorators/authenticate.spec.ts +0 -55
- package/src/decorators/authenticate.ts +0 -25
- package/src/decorators/authorize.spec.ts +0 -96
- package/src/decorators/authorize.ts +0 -25
- package/src/decorators/protocol.ts +0 -29
- package/src/domain/UserInfo.ts +0 -13
- package/src/errors/PassportException.ts +0 -8
- package/src/errors/PassportMessage.ts +0 -10
- package/src/index.ts +0 -22
- package/src/interfaces/BeforeInstall.ts +0 -3
- package/src/interfaces/OnInstall.ts +0 -5
- package/src/interfaces/OnVerify.ts +0 -3
- package/src/interfaces/ProtocolMethods.ts +0 -5
- package/src/interfaces/ProtocolOptions.ts +0 -8
- package/src/interfaces/interfaces.ts +0 -21
- package/src/middlewares/PassportMiddleware.spec.ts +0 -301
- package/src/middlewares/PassportMiddleware.ts +0 -36
- package/src/services/PassportSerializerService.spec.ts +0 -51
- package/src/services/PassportSerializerService.ts +0 -38
- package/src/services/ProtocolsService.spec.ts +0 -166
- package/src/services/ProtocolsService.ts +0 -169
- package/src/utils/getProtocolsFromRequest.spec.ts +0 -97
- package/src/utils/getProtocolsFromRequest.ts +0 -41
- package/vitest.config.mts +0 -21
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import {Store} from "@tsed/core";
|
|
2
|
-
import {Controller, inject} from "@tsed/di";
|
|
3
|
-
import {Middleware, UseBefore} from "@tsed/platform-middlewares";
|
|
4
|
-
import {Get, getSpec} from "@tsed/schema";
|
|
5
|
-
|
|
6
|
-
import {Authorize, PassportMiddleware} from "../index.js";
|
|
7
|
-
|
|
8
|
-
@Middleware()
|
|
9
|
-
class AuthoriseBucket {
|
|
10
|
-
use() {}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
describe("@Authorize", () => {
|
|
14
|
-
it("should store data", () => {
|
|
15
|
-
class Test {
|
|
16
|
-
@Authorize("local", {
|
|
17
|
-
security: {
|
|
18
|
-
test: ["scope"]
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
test() {}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const store = Store.fromMethod(Test, "test");
|
|
25
|
-
|
|
26
|
-
expect(store.get(PassportMiddleware)).toEqual({
|
|
27
|
-
method: "authorize",
|
|
28
|
-
options: {
|
|
29
|
-
security: {
|
|
30
|
-
test: ["scope"]
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
originalUrl: true,
|
|
34
|
-
protocol: "local"
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
it("should store data (without originalUrl)", () => {
|
|
38
|
-
class Test {
|
|
39
|
-
@Authorize("local", {
|
|
40
|
-
security: {
|
|
41
|
-
test: ["scope"]
|
|
42
|
-
},
|
|
43
|
-
originalUrl: false
|
|
44
|
-
})
|
|
45
|
-
test() {}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const store = Store.fromMethod(Test, "test");
|
|
49
|
-
|
|
50
|
-
expect(store.get(PassportMiddleware)).toEqual({
|
|
51
|
-
method: "authorize",
|
|
52
|
-
options: {
|
|
53
|
-
security: {
|
|
54
|
-
test: ["scope"]
|
|
55
|
-
},
|
|
56
|
-
originalUrl: false
|
|
57
|
-
},
|
|
58
|
-
originalUrl: false,
|
|
59
|
-
protocol: "local"
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
it("should support inheritance", async () => {
|
|
63
|
-
abstract class AbstractAdminController {
|
|
64
|
-
@Get("/allEntries")
|
|
65
|
-
getAllEntries(): Promise<unknown> {
|
|
66
|
-
return Promise.resolve({});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
@Authorize("loginAuthProvider")
|
|
71
|
-
@Controller("/admin")
|
|
72
|
-
class AdminController extends AbstractAdminController {}
|
|
73
|
-
|
|
74
|
-
@Controller("/admin/bucket")
|
|
75
|
-
@UseBefore(AuthoriseBucket)
|
|
76
|
-
class BucketAdminController extends AbstractAdminController {}
|
|
77
|
-
|
|
78
|
-
expect(getSpec(AdminController)).toMatchSnapshot();
|
|
79
|
-
expect(getSpec(BucketAdminController)).toMatchSnapshot();
|
|
80
|
-
expect(Reflect.getOwnPropertyDescriptor(AdminController.prototype, "getAllEntries")).toBeDefined();
|
|
81
|
-
expect(Reflect.getOwnPropertyDescriptor(BucketAdminController.prototype, "getAllEntries")).not.toBeDefined();
|
|
82
|
-
|
|
83
|
-
const adminController = inject(AdminController);
|
|
84
|
-
|
|
85
|
-
expect(await adminController.getAllEntries()).toEqual({});
|
|
86
|
-
|
|
87
|
-
expect(Store.fromMethod(BucketAdminController, "getAllEntries").get(PassportMiddleware)).toEqual(undefined);
|
|
88
|
-
expect(Store.fromMethod(AbstractAdminController, "getAllEntries").get(PassportMiddleware)).toEqual(undefined);
|
|
89
|
-
expect(Store.fromMethod(AdminController, "getAllEntries").get(PassportMiddleware)).toEqual({
|
|
90
|
-
method: "authorize",
|
|
91
|
-
options: {},
|
|
92
|
-
originalUrl: true,
|
|
93
|
-
protocol: "loginAuthProvider"
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {useDecorators} from "@tsed/core";
|
|
2
|
-
import {UseAuth} from "@tsed/platform-middlewares";
|
|
3
|
-
import {Security} from "@tsed/schema";
|
|
4
|
-
import {AuthenticateOptions} from "passport";
|
|
5
|
-
|
|
6
|
-
import {PassportMiddleware} from "../middlewares/PassportMiddleware.js";
|
|
7
|
-
|
|
8
|
-
export interface AuthorizeOptions extends AuthenticateOptions {
|
|
9
|
-
security?: Record<string, string[]>;
|
|
10
|
-
originalUrl?: boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function Authorize(protocol: string | string[] = "*", options: AuthorizeOptions = {}): Function {
|
|
14
|
-
return useDecorators(
|
|
15
|
-
UseAuth(PassportMiddleware, {
|
|
16
|
-
protocol,
|
|
17
|
-
method: "authorize",
|
|
18
|
-
originalUrl: options.originalUrl === undefined ? true : options.originalUrl,
|
|
19
|
-
options
|
|
20
|
-
}),
|
|
21
|
-
...Object.entries(options.security || {}).map(([key, value]) => {
|
|
22
|
-
return Security(key, ...value);
|
|
23
|
-
})
|
|
24
|
-
);
|
|
25
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {StoreSet, useDecorators} from "@tsed/core";
|
|
2
|
-
import {Configuration, Injectable} from "@tsed/di";
|
|
3
|
-
|
|
4
|
-
import {PROVIDER_TYPE_PROTOCOL} from "../contants/constants.js";
|
|
5
|
-
import {ProtocolOptions} from "../interfaces/ProtocolOptions.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Declare a new Protocol base on a Passport Strategy
|
|
9
|
-
*
|
|
10
|
-
* @decorator
|
|
11
|
-
* @class
|
|
12
|
-
*/
|
|
13
|
-
export function Protocol<T = any>(options: ProtocolOptionsDecorator<T>) {
|
|
14
|
-
return useDecorators(
|
|
15
|
-
Injectable({
|
|
16
|
-
type: PROVIDER_TYPE_PROTOCOL
|
|
17
|
-
}),
|
|
18
|
-
StoreSet("protocol", options),
|
|
19
|
-
Configuration({
|
|
20
|
-
passport: {
|
|
21
|
-
protocols: {
|
|
22
|
-
[options.name]: options
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
})
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type ProtocolOptionsDecorator<T = any> = {name: string} & Partial<ProtocolOptions<T>>;
|
package/src/domain/UserInfo.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by @tsed/barrels.
|
|
3
|
-
*/
|
|
4
|
-
export * from "./contants/constants.js";
|
|
5
|
-
export * from "./decorators/args.js";
|
|
6
|
-
export * from "./decorators/authenticate.js";
|
|
7
|
-
export * from "./decorators/authorize.js";
|
|
8
|
-
export * from "./decorators/protocol.js";
|
|
9
|
-
export * from "./domain/UserInfo.js";
|
|
10
|
-
export * from "./errors/PassportException.js";
|
|
11
|
-
export * from "./errors/PassportMessage.js";
|
|
12
|
-
export * from "./interfaces/BeforeInstall.js";
|
|
13
|
-
export * from "./interfaces/interfaces.js";
|
|
14
|
-
export * from "./interfaces/OnInstall.js";
|
|
15
|
-
export * from "./interfaces/OnVerify.js";
|
|
16
|
-
export * from "./interfaces/ProtocolMethods.js";
|
|
17
|
-
export * from "./interfaces/ProtocolOptions.js";
|
|
18
|
-
export * from "./middlewares/PassportMiddleware.js";
|
|
19
|
-
export * from "./PassportModule.js";
|
|
20
|
-
export * from "./services/PassportSerializerService.js";
|
|
21
|
-
export * from "./services/ProtocolsService.js";
|
|
22
|
-
export * from "./utils/getProtocolsFromRequest.js";
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {Type} from "@tsed/core";
|
|
2
|
-
import {PassportStatic} from "passport";
|
|
3
|
-
|
|
4
|
-
import {ProtocolOptions} from "./ProtocolOptions.js";
|
|
5
|
-
|
|
6
|
-
declare global {
|
|
7
|
-
namespace TsED {
|
|
8
|
-
interface Configuration {
|
|
9
|
-
passport: {
|
|
10
|
-
userProperty?: string;
|
|
11
|
-
pauseStream?: string;
|
|
12
|
-
disableSession?: boolean;
|
|
13
|
-
userInfoModel?: Type<any> | false;
|
|
14
|
-
module?: PassportStatic;
|
|
15
|
-
protocols?: {
|
|
16
|
-
[protocolName: string]: Partial<ProtocolOptions>;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
import {Unauthorized} from "@tsed/exceptions";
|
|
2
|
-
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
-
import Passport from "passport";
|
|
4
|
-
|
|
5
|
-
import {PassportException} from "../errors/PassportException.js";
|
|
6
|
-
import {PassportMiddleware} from "./PassportMiddleware.js";
|
|
7
|
-
|
|
8
|
-
function createContextFixture(options = {}) {
|
|
9
|
-
return PlatformTest.createRequestContext({
|
|
10
|
-
event: {
|
|
11
|
-
request: PlatformTest.createRequest(options)
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
describe("PassportMiddleware", () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
PlatformTest.create();
|
|
19
|
-
vi.spyOn(Passport, "authenticate").mockImplementation(() => (req: any, res: any, next: any) => {
|
|
20
|
-
return next();
|
|
21
|
-
});
|
|
22
|
-
vi.spyOn(Passport, "authorize").mockImplementation(() => (req: any, res: any, next: any) => {
|
|
23
|
-
return next();
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
PlatformTest.reset();
|
|
28
|
-
});
|
|
29
|
-
it("should call passport with local", async () => {
|
|
30
|
-
// GIVEN
|
|
31
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
32
|
-
const context = createContextFixture();
|
|
33
|
-
|
|
34
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local"]);
|
|
35
|
-
|
|
36
|
-
context.endpoint = {
|
|
37
|
-
store: {
|
|
38
|
-
get: vi.fn().mockReturnValue({
|
|
39
|
-
options: {},
|
|
40
|
-
protocol: "local",
|
|
41
|
-
method: "authenticate"
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
} as any;
|
|
45
|
-
|
|
46
|
-
// WHEN
|
|
47
|
-
await middleware.use(context);
|
|
48
|
-
|
|
49
|
-
// THEN
|
|
50
|
-
expect(Passport.authenticate).toHaveBeenCalledWith("local", {failWithError: true});
|
|
51
|
-
});
|
|
52
|
-
it("should skip auth when user is authenticated", async () => {
|
|
53
|
-
// GIVEN
|
|
54
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
55
|
-
const context = createContextFixture();
|
|
56
|
-
|
|
57
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local"]);
|
|
58
|
-
|
|
59
|
-
context.getRequest().user = {};
|
|
60
|
-
context.getRequest().isAuthenticated = () => {
|
|
61
|
-
return true;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
context.endpoint = {
|
|
65
|
-
store: {
|
|
66
|
-
get: vi.fn().mockReturnValue({
|
|
67
|
-
options: {},
|
|
68
|
-
protocol: "local",
|
|
69
|
-
method: "authenticate"
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
} as any;
|
|
73
|
-
|
|
74
|
-
// WHEN
|
|
75
|
-
await middleware.use(context);
|
|
76
|
-
|
|
77
|
-
// THEN
|
|
78
|
-
return expect(Passport.authenticate).not.toHaveBeenCalled();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("should call passport with defaults protocols", async () => {
|
|
82
|
-
// GIVEN
|
|
83
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
84
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
85
|
-
|
|
86
|
-
const context = createContextFixture();
|
|
87
|
-
|
|
88
|
-
context.endpoint = {
|
|
89
|
-
store: {
|
|
90
|
-
get: vi.fn().mockReturnValue({
|
|
91
|
-
options: {},
|
|
92
|
-
protocol: "*",
|
|
93
|
-
method: "authenticate"
|
|
94
|
-
})
|
|
95
|
-
}
|
|
96
|
-
} as any;
|
|
97
|
-
|
|
98
|
-
// WHEN
|
|
99
|
-
await middleware.use(context);
|
|
100
|
-
|
|
101
|
-
// THEN
|
|
102
|
-
expect(Passport.authenticate).toHaveBeenCalledWith(["local", "basic"], {failWithError: true});
|
|
103
|
-
});
|
|
104
|
-
it("should call passport with :protocol", async () => {
|
|
105
|
-
// GIVEN
|
|
106
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
107
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
108
|
-
|
|
109
|
-
const context = createContextFixture({
|
|
110
|
-
url: "/",
|
|
111
|
-
originalUrl: "/rest",
|
|
112
|
-
query: {
|
|
113
|
-
protocol: "basic"
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
context.endpoint = {
|
|
118
|
-
store: {
|
|
119
|
-
get: vi.fn().mockReturnValue({
|
|
120
|
-
options: {},
|
|
121
|
-
protocol: ":protocol",
|
|
122
|
-
method: "authenticate"
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
} as any;
|
|
126
|
-
|
|
127
|
-
// WHEN
|
|
128
|
-
await middleware.use(context);
|
|
129
|
-
|
|
130
|
-
// THEN
|
|
131
|
-
expect(context.getRequest().url).toBe(context.getRequest().originalUrl);
|
|
132
|
-
expect(Passport.authenticate).toHaveBeenCalledWith("basic", {failWithError: true});
|
|
133
|
-
});
|
|
134
|
-
it("should call passport with authorize", async () => {
|
|
135
|
-
// GIVEN
|
|
136
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
137
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
138
|
-
|
|
139
|
-
const context = createContextFixture({
|
|
140
|
-
url: "/",
|
|
141
|
-
originalUrl: "/rest",
|
|
142
|
-
query: {
|
|
143
|
-
protocol: "basic"
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
context.endpoint = {
|
|
148
|
-
store: {
|
|
149
|
-
get: vi.fn().mockReturnValue({
|
|
150
|
-
options: {},
|
|
151
|
-
protocol: ":protocol",
|
|
152
|
-
method: "authorize"
|
|
153
|
-
})
|
|
154
|
-
}
|
|
155
|
-
} as any;
|
|
156
|
-
|
|
157
|
-
// WHEN
|
|
158
|
-
await middleware.use(context);
|
|
159
|
-
|
|
160
|
-
// THEN
|
|
161
|
-
expect(context.getRequest().url).toBe(context.getRequest().originalUrl);
|
|
162
|
-
expect(Passport.authorize).toHaveBeenCalledWith("basic", {failWithError: true});
|
|
163
|
-
});
|
|
164
|
-
it("should throw errors", async () => {
|
|
165
|
-
// GIVEN
|
|
166
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
167
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
168
|
-
|
|
169
|
-
const context = createContextFixture();
|
|
170
|
-
context.endpoint = {
|
|
171
|
-
store: {
|
|
172
|
-
get: vi.fn().mockReturnValue({
|
|
173
|
-
options: {},
|
|
174
|
-
protocol: ":protocol",
|
|
175
|
-
method: "authenticate"
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
} as any;
|
|
179
|
-
|
|
180
|
-
// WHEN
|
|
181
|
-
let actualError;
|
|
182
|
-
try {
|
|
183
|
-
await middleware.use(context);
|
|
184
|
-
} catch (er) {
|
|
185
|
-
actualError = er;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// THEN
|
|
189
|
-
expect(actualError).toBeInstanceOf(Unauthorized);
|
|
190
|
-
});
|
|
191
|
-
it("should throw errors from passport (AuthenticationError)", async () => {
|
|
192
|
-
// GIVEN
|
|
193
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
194
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
195
|
-
|
|
196
|
-
(Passport.authenticate as any).mockImplementation(() => (req: any, res: any, next: any) => {
|
|
197
|
-
const error: any = {message: "message"};
|
|
198
|
-
error.name = "AuthenticationError";
|
|
199
|
-
error.status = 400;
|
|
200
|
-
|
|
201
|
-
return next(error);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
const context = createContextFixture();
|
|
205
|
-
|
|
206
|
-
context.endpoint = {
|
|
207
|
-
store: {
|
|
208
|
-
get: vi.fn().mockReturnValue({
|
|
209
|
-
options: {},
|
|
210
|
-
protocol: "*",
|
|
211
|
-
method: "authenticate"
|
|
212
|
-
})
|
|
213
|
-
}
|
|
214
|
-
} as any;
|
|
215
|
-
|
|
216
|
-
// WHEN
|
|
217
|
-
let actualError;
|
|
218
|
-
try {
|
|
219
|
-
await middleware.use(context);
|
|
220
|
-
} catch (er) {
|
|
221
|
-
actualError = er;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// THEN
|
|
225
|
-
expect(actualError).toBeInstanceOf(PassportException);
|
|
226
|
-
expect(actualError.message).toBe("message");
|
|
227
|
-
expect(actualError.status).toBe(400);
|
|
228
|
-
});
|
|
229
|
-
it("should throw errors from passport (AnyError)", async () => {
|
|
230
|
-
// GIVEN
|
|
231
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
232
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
233
|
-
|
|
234
|
-
(Passport.authenticate as any).mockImplementation(() => (req: any, res: any, next: any) => {
|
|
235
|
-
const error: any = {message: "message"};
|
|
236
|
-
error.name = "Any";
|
|
237
|
-
|
|
238
|
-
return next(error);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
const context = createContextFixture();
|
|
242
|
-
|
|
243
|
-
context.endpoint = {
|
|
244
|
-
store: {
|
|
245
|
-
get: vi.fn().mockReturnValue({
|
|
246
|
-
options: {},
|
|
247
|
-
protocol: "*",
|
|
248
|
-
method: "authenticate"
|
|
249
|
-
})
|
|
250
|
-
}
|
|
251
|
-
} as any;
|
|
252
|
-
|
|
253
|
-
// WHEN
|
|
254
|
-
let actualError;
|
|
255
|
-
try {
|
|
256
|
-
await middleware.use(context);
|
|
257
|
-
} catch (er) {
|
|
258
|
-
actualError = er;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// THEN
|
|
262
|
-
expect(actualError).toBeInstanceOf(PassportException);
|
|
263
|
-
expect(actualError.message).toBe("message");
|
|
264
|
-
expect(actualError.status).toBe(500);
|
|
265
|
-
});
|
|
266
|
-
it("should throw errors from passport (Error)", async () => {
|
|
267
|
-
// GIVEN
|
|
268
|
-
const middleware = await PlatformTest.invoke<PassportMiddleware>(PassportMiddleware);
|
|
269
|
-
vi.spyOn(middleware.protocolsService, "getProtocolsNames").mockReturnValue(["local", "basic"]);
|
|
270
|
-
|
|
271
|
-
(Passport.authenticate as any).mockImplementation(() => (req: any, res: any, next: any) => {
|
|
272
|
-
const error: any = new Error("message");
|
|
273
|
-
|
|
274
|
-
return next(error);
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
const context = createContextFixture();
|
|
278
|
-
|
|
279
|
-
context.endpoint = {
|
|
280
|
-
store: {
|
|
281
|
-
get: vi.fn().mockReturnValue({
|
|
282
|
-
options: {},
|
|
283
|
-
protocol: "*",
|
|
284
|
-
method: "authenticate"
|
|
285
|
-
})
|
|
286
|
-
}
|
|
287
|
-
} as any;
|
|
288
|
-
|
|
289
|
-
// WHEN
|
|
290
|
-
let actualError;
|
|
291
|
-
try {
|
|
292
|
-
await middleware.use(context);
|
|
293
|
-
} catch (er) {
|
|
294
|
-
actualError = er;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// THEN
|
|
298
|
-
expect(actualError).toBeInstanceOf(Error);
|
|
299
|
-
expect(actualError.message).toBe("message");
|
|
300
|
-
});
|
|
301
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import {Inject} from "@tsed/di";
|
|
2
|
-
import {Middleware} from "@tsed/platform-middlewares";
|
|
3
|
-
import {Context} from "@tsed/platform-params";
|
|
4
|
-
|
|
5
|
-
import {ProtocolsService} from "../services/ProtocolsService.js";
|
|
6
|
-
import {getProtocolsFromRequest} from "../utils/getProtocolsFromRequest.js";
|
|
7
|
-
|
|
8
|
-
@Middleware()
|
|
9
|
-
export class PassportMiddleware {
|
|
10
|
-
@Inject(ProtocolsService)
|
|
11
|
-
protocolsService: ProtocolsService;
|
|
12
|
-
|
|
13
|
-
shouldSkip(ctx: Context) {
|
|
14
|
-
const request = ctx.getRequest();
|
|
15
|
-
return request.user && request.isAuthenticated();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async use(@Context() ctx: Context) {
|
|
19
|
-
const endpoint = ctx.endpoint;
|
|
20
|
-
const request = ctx.getRequest();
|
|
21
|
-
|
|
22
|
-
if (this.shouldSkip(ctx)) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const {options, protocol, method, originalUrl = true} = endpoint.store.get(PassportMiddleware);
|
|
26
|
-
const protocols = getProtocolsFromRequest(request, protocol, this.protocolsService.getProtocolsNames());
|
|
27
|
-
|
|
28
|
-
if (originalUrl) {
|
|
29
|
-
request.url = request.originalUrl;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
await (method === "authenticate"
|
|
33
|
-
? this.protocolsService.authenticate(protocols, options, ctx)
|
|
34
|
-
: this.protocolsService.authorize(protocols, options, ctx));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
2
|
-
|
|
3
|
-
import {PassportSerializerService, UserInfo} from "../index.js";
|
|
4
|
-
|
|
5
|
-
describe("PassportSerializerService", () => {
|
|
6
|
-
beforeEach(() => PlatformTest.create());
|
|
7
|
-
afterEach(PlatformTest.reset);
|
|
8
|
-
|
|
9
|
-
it(
|
|
10
|
-
"should serialize model",
|
|
11
|
-
PlatformTest.inject([PassportSerializerService], async (service: PassportSerializerService) => {
|
|
12
|
-
const userInfo = new UserInfo();
|
|
13
|
-
|
|
14
|
-
userInfo.id = "id";
|
|
15
|
-
userInfo.email = "email@email.fr";
|
|
16
|
-
userInfo.password = "password";
|
|
17
|
-
|
|
18
|
-
const result = await new Promise((resolve) => service.serialize(userInfo, (...args: any[]) => resolve(args)));
|
|
19
|
-
|
|
20
|
-
expect(result).toEqual([null, '{"id":"id","email":"email@email.fr"}']);
|
|
21
|
-
})
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
it(
|
|
25
|
-
"should deserialize model",
|
|
26
|
-
PlatformTest.inject([PassportSerializerService], async (service: PassportSerializerService) => {
|
|
27
|
-
const result = await new Promise((resolve) =>
|
|
28
|
-
service.deserialize('{"id":"id","email":"email@email.fr"}', (...args: any[]) => resolve(args))
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
expect(result).toEqual([
|
|
32
|
-
null,
|
|
33
|
-
{
|
|
34
|
-
email: "email@email.fr",
|
|
35
|
-
id: "id"
|
|
36
|
-
}
|
|
37
|
-
]);
|
|
38
|
-
})
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
it(
|
|
42
|
-
"should catch error when deserializing model",
|
|
43
|
-
PlatformTest.inject([PassportSerializerService], async (service: PassportSerializerService) => {
|
|
44
|
-
const result: any = await new Promise((resolve) =>
|
|
45
|
-
service.deserialize('{"id":"id","email":"email@email.fr}', (...args: any[]) => resolve(args))
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
expect(result[0].message).toEqual("Unterminated string in JSON at position 35");
|
|
49
|
-
})
|
|
50
|
-
);
|
|
51
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {Type} from "@tsed/core";
|
|
2
|
-
import {Constant, Service} from "@tsed/di";
|
|
3
|
-
import {deserialize, serialize} from "@tsed/json-mapper";
|
|
4
|
-
|
|
5
|
-
import {UserInfo} from "../domain/UserInfo.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @ignore
|
|
9
|
-
*/
|
|
10
|
-
@Service()
|
|
11
|
-
export class PassportSerializerService {
|
|
12
|
-
@Constant("passport.userInfoModel", UserInfo)
|
|
13
|
-
private model: Type<UserInfo> | boolean;
|
|
14
|
-
|
|
15
|
-
serialize(user: UserInfo, done: any) {
|
|
16
|
-
try {
|
|
17
|
-
const obj = serialize(user, {type: this.model});
|
|
18
|
-
|
|
19
|
-
// remove password from serialized object
|
|
20
|
-
obj.password = undefined;
|
|
21
|
-
|
|
22
|
-
done(null, JSON.stringify(obj));
|
|
23
|
-
} catch (er) {
|
|
24
|
-
// istanbul ignore next
|
|
25
|
-
done(er);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
deserialize(obj: any, done: any) {
|
|
30
|
-
try {
|
|
31
|
-
const user = deserialize(JSON.parse(obj), {type: this.model});
|
|
32
|
-
|
|
33
|
-
done(null, user);
|
|
34
|
-
} catch (er) {
|
|
35
|
-
done(er);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|