nestjs-authentication 0.1.8 → 0.4.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/dist/classes/memory.user.d.ts +4 -5
- package/dist/classes/memory.user.d.ts.map +1 -1
- package/dist/classes/memory.user.js +0 -5
- package/dist/classes/memory.user.js.map +1 -1
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/constants.js.map +1 -0
- package/dist/encryptor.d.ts +7 -0
- package/dist/encryptor.d.ts.map +1 -0
- package/dist/encryptor.js +31 -0
- package/dist/encryptor.js.map +1 -0
- package/dist/guard.d.ts +20 -8
- package/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +86 -48
- package/dist/guard.js.map +1 -1
- package/dist/index.d.ts +13 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -13
- package/dist/index.js.map +1 -1
- package/dist/interfaces/auth.user.d.ts +8 -0
- package/dist/interfaces/auth.user.d.ts.map +1 -0
- package/dist/interfaces/{jwt.payload.js → auth.user.js} +1 -1
- package/dist/interfaces/auth.user.js.map +1 -0
- package/dist/interfaces/encrypt.d.ts +2 -2
- package/dist/interfaces/encrypt.d.ts.map +1 -1
- package/dist/interfaces/options.d.ts +4 -2
- package/dist/interfaces/options.d.ts.map +1 -1
- package/dist/interfaces/provider.d.ts +4 -4
- package/dist/interfaces/provider.d.ts.map +1 -1
- package/dist/module.d.ts +15 -5
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +89 -29
- package/dist/module.js.map +1 -1
- package/dist/providers/auth.memory.user.provider.d.ts +15 -0
- package/dist/providers/auth.memory.user.provider.d.ts.map +1 -0
- package/dist/providers/auth.memory.user.provider.js +30 -0
- package/dist/providers/auth.memory.user.provider.js.map +1 -0
- package/dist/providers/auth.provider.d.ts +8 -11
- package/dist/providers/auth.provider.d.ts.map +1 -1
- package/dist/providers/auth.provider.js +10 -33
- package/dist/providers/auth.provider.js.map +1 -1
- package/dist/providers/{memory.provider.d.ts → memory.user.provider.d.ts} +2 -2
- package/dist/providers/memory.user.provider.d.ts.map +1 -0
- package/dist/providers/{memory.provider.js → memory.user.provider.js} +5 -5
- package/dist/providers/memory.user.provider.js.map +1 -0
- package/dist/service.d.ts +5 -10
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +2 -35
- package/dist/service.js.map +1 -1
- package/dist/strategies/emailpassword.d.ts +14 -0
- package/dist/strategies/emailpassword.d.ts.map +1 -0
- package/dist/strategies/{local.js → emailpassword.js} +18 -17
- package/dist/strategies/emailpassword.js.map +1 -0
- package/dist/strategies/jwt.d.ts +22 -0
- package/dist/strategies/jwt.d.ts.map +1 -0
- package/dist/strategies/jwt.js +40 -0
- package/dist/strategies/jwt.js.map +1 -0
- package/dist/user.serializer.d.ts +4 -6
- package/dist/user.serializer.d.ts.map +1 -1
- package/dist/user.serializer.js +5 -14
- package/dist/user.serializer.js.map +1 -1
- package/package.json +50 -47
- package/test/emailpassword.controller.ts +69 -0
- package/test/encryptor.spec.ts +20 -0
- package/test/jwt.controller.ts +38 -0
- package/test/mixed.controller.ts +41 -0
- package/test/module.spec.ts +535 -0
- package/dist/classes/user.d.ts +0 -8
- package/dist/classes/user.d.ts.map +0 -1
- package/dist/classes/user.js +0 -7
- package/dist/classes/user.js.map +0 -1
- package/dist/controller.d.ts +0 -7
- package/dist/controller.d.ts.map +0 -1
- package/dist/controller.js +0 -37
- package/dist/controller.js.map +0 -1
- package/dist/interfaces/emailpassword.d.ts +0 -5
- package/dist/interfaces/emailpassword.d.ts.map +0 -1
- package/dist/interfaces/emailpassword.js +0 -3
- package/dist/interfaces/emailpassword.js.map +0 -1
- package/dist/interfaces/jwt.payload.d.ts +0 -6
- package/dist/interfaces/jwt.payload.d.ts.map +0 -1
- package/dist/interfaces/jwt.payload.js.map +0 -1
- package/dist/interfaces/login.request.d.ts +0 -6
- package/dist/interfaces/login.request.d.ts.map +0 -1
- package/dist/interfaces/login.request.js +0 -3
- package/dist/interfaces/login.request.js.map +0 -1
- package/dist/providers/memory.provider.d.ts.map +0 -1
- package/dist/providers/memory.provider.js.map +0 -1
- package/dist/strategies/local.d.ts +0 -9
- package/dist/strategies/local.d.ts.map +0 -1
- package/dist/strategies/local.js.map +0 -1
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { NestApplication } from '@nestjs/core';
|
|
2
|
+
import { Test } from '@nestjs/testing';
|
|
3
|
+
import { randomBytes } from 'crypto';
|
|
4
|
+
import session from 'express-session';
|
|
5
|
+
import { ExtractJwt } from 'passport-jwt';
|
|
6
|
+
import request from 'supertest';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
AuthMemoryUserProvider,
|
|
10
|
+
AuthModule,
|
|
11
|
+
AuthService,
|
|
12
|
+
EmailPasswordStrategy,
|
|
13
|
+
Encryptor,
|
|
14
|
+
JwtStrategy,
|
|
15
|
+
MemoryUser,
|
|
16
|
+
MemoryUserProvider,
|
|
17
|
+
MODULE_OPTIONS_TOKEN
|
|
18
|
+
} from '../src';
|
|
19
|
+
import { EmailPaswordController } from './emailpassword.controller';
|
|
20
|
+
import { JwtController } from './jwt.controller';
|
|
21
|
+
import { MixedController } from './mixed.controller';
|
|
22
|
+
|
|
23
|
+
class User implements MemoryUser {
|
|
24
|
+
_id: string;
|
|
25
|
+
email: string;
|
|
26
|
+
passwordEncrypted: string;
|
|
27
|
+
namespace?: string;
|
|
28
|
+
authSerialize() {
|
|
29
|
+
return { _id: this._id, namespace: this.namespace ?? 'default' };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const key = randomBytes(32).toString('hex');
|
|
34
|
+
const iv = randomBytes(16).toString('hex');
|
|
35
|
+
|
|
36
|
+
const userTest = new User();
|
|
37
|
+
userTest._id = '1';
|
|
38
|
+
userTest.email = 'userTest@nestjs-authentication.com';
|
|
39
|
+
const userTestPassword = 'password';
|
|
40
|
+
|
|
41
|
+
const userTest2 = new User();
|
|
42
|
+
userTest2._id = '1';
|
|
43
|
+
userTest2.email = 'userTest2@nestjs-authentication.com';
|
|
44
|
+
userTest2.namespace = 'user';
|
|
45
|
+
const userTest2Password = 'password2';
|
|
46
|
+
|
|
47
|
+
const jwtSecret = 'nestjs-authentication';
|
|
48
|
+
|
|
49
|
+
let app: NestApplication;
|
|
50
|
+
|
|
51
|
+
beforeAll(async () => {
|
|
52
|
+
const builder = await Test.createTestingModule({
|
|
53
|
+
imports: [
|
|
54
|
+
AuthModule.forRoot({
|
|
55
|
+
encrypt: { key, iv },
|
|
56
|
+
jwt: { secretOrKey: jwtSecret, jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken() }
|
|
57
|
+
}),
|
|
58
|
+
AuthModule.registerAuthProviders([
|
|
59
|
+
{
|
|
60
|
+
provide: 'AuthMemoryUserProvider_default',
|
|
61
|
+
inject: [Encryptor, AuthService],
|
|
62
|
+
useFactory: (encryptor: Encryptor) => {
|
|
63
|
+
const memoryUserProvider = new MemoryUserProvider<User>();
|
|
64
|
+
memoryUserProvider.registerUser(userTest);
|
|
65
|
+
return new AuthMemoryUserProvider<User>('default', memoryUserProvider, encryptor);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
provide: 'AuthMemoryUserProvider_user',
|
|
70
|
+
inject: [Encryptor, AuthService],
|
|
71
|
+
useFactory: (encryptor: Encryptor) => {
|
|
72
|
+
const memoryUserProvider = new MemoryUserProvider<User>();
|
|
73
|
+
memoryUserProvider.registerUser(userTest2);
|
|
74
|
+
return new AuthMemoryUserProvider<User>('user', memoryUserProvider, encryptor);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
])
|
|
78
|
+
],
|
|
79
|
+
controllers: [EmailPaswordController, JwtController, MixedController]
|
|
80
|
+
}).compile();
|
|
81
|
+
|
|
82
|
+
app = builder.createNestApplication();
|
|
83
|
+
|
|
84
|
+
// init the session
|
|
85
|
+
app.use(
|
|
86
|
+
session({
|
|
87
|
+
secret: 'secret',
|
|
88
|
+
resave: false,
|
|
89
|
+
saveUninitialized: false,
|
|
90
|
+
cookie: {
|
|
91
|
+
maxAge: 100000,
|
|
92
|
+
httpOnly: false
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
await app.init();
|
|
98
|
+
|
|
99
|
+
userTest.passwordEncrypted = app.get(Encryptor).encrypt(userTestPassword);
|
|
100
|
+
userTest2.passwordEncrypted = app.get(Encryptor).encrypt(userTest2Password);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('AuthModule', () => {
|
|
104
|
+
test('The AuthModule must create the options provider', async () => {
|
|
105
|
+
const options = app.get(MODULE_OPTIONS_TOKEN);
|
|
106
|
+
expect(options).toHaveProperty('encrypt.key');
|
|
107
|
+
expect(options).toHaveProperty('encrypt.iv');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('The AuthModule must create the Encryptor', async () => {
|
|
111
|
+
const encryptor = app.get(Encryptor);
|
|
112
|
+
expect(encryptor).toBeInstanceOf(Encryptor);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('The AuthModule must create the AuthService', async () => {
|
|
116
|
+
const authService = app.get(AuthService);
|
|
117
|
+
expect(authService).toBeInstanceOf(AuthService);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('The AuthModule must create the AuthMemoryUserProvider_default', async () => {
|
|
121
|
+
const memoryProvider = app.get('AuthMemoryUserProvider_default');
|
|
122
|
+
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('The AuthModule must create the AuthMemoryUserProvider_user', async () => {
|
|
126
|
+
const memoryProvider = app.get('AuthMemoryUserProvider_user');
|
|
127
|
+
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('The AuthModule must create the EmailPasswordStrategy', async () => {
|
|
131
|
+
const strategy = app.get(EmailPasswordStrategy);
|
|
132
|
+
expect(strategy).toBeInstanceOf(EmailPasswordStrategy);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('The AuthModule must create the JwtStrategy', async () => {
|
|
136
|
+
const strategy = app.get(JwtStrategy);
|
|
137
|
+
expect(strategy).toBeInstanceOf(JwtStrategy);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('AuthService', () => {
|
|
142
|
+
test('The AuthService must have the providers registered', async () => {
|
|
143
|
+
const authService = app.get(AuthService);
|
|
144
|
+
|
|
145
|
+
const memoryProvider = authService.getAuthProvider('default');
|
|
146
|
+
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
147
|
+
|
|
148
|
+
const memoryProvider2 = authService.getAuthProvider('user');
|
|
149
|
+
expect(memoryProvider2).toBeInstanceOf(AuthMemoryUserProvider);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('E2E EmailPasswordStrategy', () => {
|
|
154
|
+
describe('Authenticated', () => {
|
|
155
|
+
test(`GET /emailpassword/authenticated return the user if cookie set, empty if not`, async () => {
|
|
156
|
+
const user1 = request.agent(app.getHttpServer());
|
|
157
|
+
await user1
|
|
158
|
+
.post('/emailpassword/login_check')
|
|
159
|
+
.set('accept', 'application/json')
|
|
160
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
161
|
+
.expect({ success: true });
|
|
162
|
+
await user1
|
|
163
|
+
.get('/emailpassword/authenticated')
|
|
164
|
+
.expect(200)
|
|
165
|
+
.expect((res) => {
|
|
166
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
167
|
+
});
|
|
168
|
+
await request(app.getHttpServer()).get('/emailpassword/authenticated').expect(200);
|
|
169
|
+
});
|
|
170
|
+
test(`GET /emailpassword/authenticated return the good user`, async () => {
|
|
171
|
+
const user1 = request.agent(app.getHttpServer());
|
|
172
|
+
await user1
|
|
173
|
+
.post('/emailpassword/login_check')
|
|
174
|
+
.set('accept', 'application/json')
|
|
175
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
176
|
+
.expect({ success: true });
|
|
177
|
+
await user1
|
|
178
|
+
.get('/emailpassword/authenticated')
|
|
179
|
+
.expect(200)
|
|
180
|
+
.expect((res) => {
|
|
181
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const user2 = request.agent(app.getHttpServer());
|
|
185
|
+
await user2
|
|
186
|
+
.post('/emailpassword/login_check')
|
|
187
|
+
.set('accept', 'application/json')
|
|
188
|
+
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
189
|
+
.expect({ success: true });
|
|
190
|
+
await user2
|
|
191
|
+
.get('/emailpassword/authenticated')
|
|
192
|
+
.expect(200)
|
|
193
|
+
.expect((res) => {
|
|
194
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
195
|
+
});
|
|
196
|
+
await request(app.getHttpServer()).get('/emailpassword/authenticated').expect(200);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe('Protected', () => {
|
|
201
|
+
test(`GET /emailpassword/protected return the user if cookie set, throw 403 if not`, async () => {
|
|
202
|
+
const user1 = request.agent(app.getHttpServer());
|
|
203
|
+
await user1
|
|
204
|
+
.post('/emailpassword/login_check')
|
|
205
|
+
.set('accept', 'application/json')
|
|
206
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
207
|
+
.expect({ success: true });
|
|
208
|
+
await user1
|
|
209
|
+
.get('/emailpassword/protected')
|
|
210
|
+
.expect(200)
|
|
211
|
+
.expect((res) => {
|
|
212
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
213
|
+
});
|
|
214
|
+
await request(app.getHttpServer()).get('/emailpassword/protected').expect(403);
|
|
215
|
+
});
|
|
216
|
+
test(`GET /emailpassword/protected return the good user`, async () => {
|
|
217
|
+
const user1 = request.agent(app.getHttpServer());
|
|
218
|
+
await user1
|
|
219
|
+
.post('/emailpassword/login_check')
|
|
220
|
+
.set('accept', 'application/json')
|
|
221
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
222
|
+
.expect({ success: true });
|
|
223
|
+
await user1
|
|
224
|
+
.get('/emailpassword/protected')
|
|
225
|
+
.expect(200)
|
|
226
|
+
.expect((res) => {
|
|
227
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
228
|
+
});
|
|
229
|
+
const user2 = request.agent(app.getHttpServer());
|
|
230
|
+
await user2
|
|
231
|
+
.post('/emailpassword/login_check')
|
|
232
|
+
.set('accept', 'application/json')
|
|
233
|
+
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
234
|
+
.expect({ success: true });
|
|
235
|
+
await user2
|
|
236
|
+
.get('/emailpassword/protected')
|
|
237
|
+
.expect(200)
|
|
238
|
+
.expect((res) => {
|
|
239
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
240
|
+
});
|
|
241
|
+
await request(app.getHttpServer()).get('/emailpassword/protected').expect(403);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('Session', () => {
|
|
246
|
+
test(`GET /emailpassword/nosession return empty user if cookie set`, async () => {
|
|
247
|
+
const user1 = request.agent(app.getHttpServer());
|
|
248
|
+
await user1
|
|
249
|
+
.post('/emailpassword/login_check')
|
|
250
|
+
.set('accept', 'application/json')
|
|
251
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
252
|
+
.expect({ success: true });
|
|
253
|
+
|
|
254
|
+
await user1
|
|
255
|
+
.get('/emailpassword/session')
|
|
256
|
+
.expect(200)
|
|
257
|
+
.expect((res) => {
|
|
258
|
+
expect(res.body).not.toHaveProperty('user');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
await request(app.getHttpServer())
|
|
262
|
+
.get('/emailpassword/session')
|
|
263
|
+
.expect((res) => expect(res.body).not.toHaveProperty('user'));
|
|
264
|
+
});
|
|
265
|
+
test(`GET /emailpassword/protected return the good user`, async () => {
|
|
266
|
+
const user1 = request.agent(app.getHttpServer());
|
|
267
|
+
await user1
|
|
268
|
+
.post('/emailpassword/login_check')
|
|
269
|
+
.set('accept', 'application/json')
|
|
270
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
271
|
+
.expect({ success: true });
|
|
272
|
+
await user1
|
|
273
|
+
.get('/emailpassword/session')
|
|
274
|
+
.expect(200)
|
|
275
|
+
.expect((res) => {
|
|
276
|
+
expect(res.body).not.toHaveProperty('user');
|
|
277
|
+
});
|
|
278
|
+
const user2 = request.agent(app.getHttpServer());
|
|
279
|
+
await user2
|
|
280
|
+
.post('/emailpassword/login_check')
|
|
281
|
+
.set('accept', 'application/json')
|
|
282
|
+
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
283
|
+
.expect({ success: true });
|
|
284
|
+
await user2
|
|
285
|
+
.get('/emailpassword/session')
|
|
286
|
+
.expect(200)
|
|
287
|
+
.expect((res) => {
|
|
288
|
+
expect(res.body).not.toHaveProperty('user');
|
|
289
|
+
});
|
|
290
|
+
await request(app.getHttpServer()).get('/emailpassword/session').expect(200);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe('Logout', () => {
|
|
295
|
+
test(`GET /emailpassword/logout and logout the user`, async () => {
|
|
296
|
+
const user1 = request.agent(app.getHttpServer());
|
|
297
|
+
await user1
|
|
298
|
+
.post('/emailpassword/login_check')
|
|
299
|
+
.set('accept', 'application/json')
|
|
300
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
301
|
+
.expect({ success: true });
|
|
302
|
+
await user1
|
|
303
|
+
.get('/emailpassword/authenticated')
|
|
304
|
+
.expect(200)
|
|
305
|
+
.expect((res) => {
|
|
306
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
307
|
+
});
|
|
308
|
+
await user1.post('/emailpassword/logout').expect(200);
|
|
309
|
+
|
|
310
|
+
await user1.post('/emailpassword/authenticated').expect((res) => {
|
|
311
|
+
expect(res.body).not.toHaveProperty('user');
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe('E2E JWTStrategy', () => {
|
|
318
|
+
test(`Generate a jwt`, () => {
|
|
319
|
+
const strategy = app.get(JwtStrategy);
|
|
320
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
321
|
+
expect(typeof token).toBe('string');
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
describe('Authenticated', () => {
|
|
325
|
+
test(`GET /jwt/authenticated return the user if token set, empty if not`, async () => {
|
|
326
|
+
const strategy = app.get(JwtStrategy);
|
|
327
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
328
|
+
const user1 = request.agent(app.getHttpServer());
|
|
329
|
+
await user1
|
|
330
|
+
.get('/jwt/authenticated')
|
|
331
|
+
.set('Authorization', `Bearer ${token}`)
|
|
332
|
+
.expect(200)
|
|
333
|
+
.expect((res) => {
|
|
334
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
335
|
+
});
|
|
336
|
+
await user1
|
|
337
|
+
.get('/jwt/authenticated')
|
|
338
|
+
.expect(200)
|
|
339
|
+
.expect((res) => {
|
|
340
|
+
expect(res.body).not.toHaveProperty('user');
|
|
341
|
+
});
|
|
342
|
+
await user1.get('/jwt/authenticated').expect(200);
|
|
343
|
+
await request(app.getHttpServer()).get('/jwt/authenticated').expect(200);
|
|
344
|
+
});
|
|
345
|
+
test(`GET /jwt/authenticated return the good user`, async () => {
|
|
346
|
+
const strategy = app.get(JwtStrategy);
|
|
347
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
348
|
+
const user1 = request.agent(app.getHttpServer());
|
|
349
|
+
await user1
|
|
350
|
+
.get('/jwt/authenticated')
|
|
351
|
+
.set('Authorization', `Bearer ${token}`)
|
|
352
|
+
.expect(200)
|
|
353
|
+
.expect((res) => {
|
|
354
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const token2 = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
358
|
+
await user1
|
|
359
|
+
.get('/jwt/authenticated')
|
|
360
|
+
.set('Authorization', `Bearer ${token2}`)
|
|
361
|
+
.expect(200)
|
|
362
|
+
.expect((res) => {
|
|
363
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
364
|
+
});
|
|
365
|
+
await user1.get('/jwt/authenticated').expect(200);
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
describe('Protected', () => {
|
|
370
|
+
test(`GET /jwt/protected return the user if token set, throw if not`, async () => {
|
|
371
|
+
const strategy = app.get(JwtStrategy);
|
|
372
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
373
|
+
const user1 = request.agent(app.getHttpServer());
|
|
374
|
+
await user1
|
|
375
|
+
.get('/jwt/protected')
|
|
376
|
+
.set('Authorization', `Bearer ${token}`)
|
|
377
|
+
.expect(200)
|
|
378
|
+
.expect((res) => {
|
|
379
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
380
|
+
});
|
|
381
|
+
await user1.get('/jwt/protected').expect(403);
|
|
382
|
+
await request(app.getHttpServer()).get('/jwt/protected').expect(403);
|
|
383
|
+
});
|
|
384
|
+
test(`GET /jwt/protected return the good user`, async () => {
|
|
385
|
+
const strategy = app.get(JwtStrategy);
|
|
386
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
387
|
+
const user1 = request.agent(app.getHttpServer());
|
|
388
|
+
await user1
|
|
389
|
+
.get('/jwt/protected')
|
|
390
|
+
.set('Authorization', `Bearer ${token}`)
|
|
391
|
+
.expect(200)
|
|
392
|
+
.expect((res) => {
|
|
393
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
394
|
+
});
|
|
395
|
+
const token2 = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
396
|
+
await user1
|
|
397
|
+
.get('/jwt/protected')
|
|
398
|
+
.set('Authorization', `Bearer ${token2}`)
|
|
399
|
+
.expect(200)
|
|
400
|
+
.expect((res) => {
|
|
401
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
402
|
+
});
|
|
403
|
+
await user1.get('/jwt/protected').expect(403);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe('Session', () => {
|
|
408
|
+
test(`GET /jwt/session return the user if token set, throw if not`, async () => {
|
|
409
|
+
const strategy = app.get(JwtStrategy);
|
|
410
|
+
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
411
|
+
|
|
412
|
+
// this request must login the user and persist it in the session for next call
|
|
413
|
+
const user1 = request.agent(app.getHttpServer());
|
|
414
|
+
await user1
|
|
415
|
+
.get('/jwt/protected')
|
|
416
|
+
.set('Authorization', `Bearer ${token}`)
|
|
417
|
+
.expect(200)
|
|
418
|
+
.expect((res) => {
|
|
419
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// this request must get the user from session, even if no token is provided
|
|
423
|
+
await user1
|
|
424
|
+
.get('/jwt/session')
|
|
425
|
+
.expect(200)
|
|
426
|
+
.expect((res) => {
|
|
427
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
await request(app.getHttpServer()).get('/jwt/session').expect(403);
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
describe('E2E MixedStrategy (emailpassword and jwt)', () => {
|
|
436
|
+
let user1: request.SuperAgentTest;
|
|
437
|
+
beforeAll(async () => {
|
|
438
|
+
user1 = request.agent(app.getHttpServer());
|
|
439
|
+
await user1
|
|
440
|
+
.post('/emailpassword/login_check')
|
|
441
|
+
.set('accept', 'application/json')
|
|
442
|
+
.send({ email: userTest.email, password: userTestPassword })
|
|
443
|
+
.expect({ success: true });
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
describe('Authenticated', () => {
|
|
447
|
+
test(`GET /mixed/authenticated return the user from session`, async () => {
|
|
448
|
+
await user1
|
|
449
|
+
.get('/mixed/authenticated')
|
|
450
|
+
.expect(200)
|
|
451
|
+
.expect((res) => {
|
|
452
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
test(`GET /mixed/authenticated return the user from jwt token`, async () => {
|
|
456
|
+
const strategy = app.get(JwtStrategy);
|
|
457
|
+
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
458
|
+
await request
|
|
459
|
+
.agent(app.getHttpServer())
|
|
460
|
+
.get('/mixed/authenticated')
|
|
461
|
+
.set('Authorization', `Bearer ${token}`)
|
|
462
|
+
.expect(200)
|
|
463
|
+
.expect((res) => {
|
|
464
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
test(`GET /mixed/authenticated do not return the user (no cookie, no token)`, async () => {
|
|
468
|
+
await request
|
|
469
|
+
.agent(app.getHttpServer())
|
|
470
|
+
.get('/mixed/authenticated')
|
|
471
|
+
.expect(200)
|
|
472
|
+
.expect((res) => {
|
|
473
|
+
expect(res.body).not.toHaveProperty('user');
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
describe('Protected', () => {
|
|
479
|
+
test(`GET /mixed/protected return the user from session`, async () => {
|
|
480
|
+
await user1
|
|
481
|
+
.get('/mixed/protected')
|
|
482
|
+
.expect(200)
|
|
483
|
+
.expect((res) => {
|
|
484
|
+
expect(res.body).toHaveProperty('user', userTest);
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
test(`GET /mixed/protected return the user from jwt token`, async () => {
|
|
488
|
+
const strategy = app.get(JwtStrategy);
|
|
489
|
+
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
490
|
+
await request
|
|
491
|
+
.agent(app.getHttpServer())
|
|
492
|
+
.get('/mixed/authenticated')
|
|
493
|
+
.set('Authorization', `Bearer ${token}`)
|
|
494
|
+
.expect(200)
|
|
495
|
+
.expect((res) => {
|
|
496
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
test(`GET /mixed/protected throw (no cookie, no token)`, async () => {
|
|
500
|
+
await request.agent(app.getHttpServer()).get('/mixed/protected').expect(403);
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe('Session', () => {
|
|
505
|
+
test(`GET /mixed/session do not return the user from session (user is logged in)`, async () => {
|
|
506
|
+
await user1
|
|
507
|
+
.get('/mixed/session')
|
|
508
|
+
.expect(200)
|
|
509
|
+
.expect((res) => {
|
|
510
|
+
expect(res.body).not.toHaveProperty('user');
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
test(`GET /mixed/session return the user from jwt token`, async () => {
|
|
514
|
+
const strategy = app.get(JwtStrategy);
|
|
515
|
+
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
516
|
+
await request
|
|
517
|
+
.agent(app.getHttpServer())
|
|
518
|
+
.get('/mixed/session')
|
|
519
|
+
.set('Authorization', `Bearer ${token}`)
|
|
520
|
+
.expect(200)
|
|
521
|
+
.expect((res) => {
|
|
522
|
+
expect(res.body).toHaveProperty('user', userTest2);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
test(`GET /mixed/session return empty`, async () => {
|
|
526
|
+
await request
|
|
527
|
+
.agent(app.getHttpServer())
|
|
528
|
+
.get('/mixed/session')
|
|
529
|
+
.expect(200)
|
|
530
|
+
.expect((res) => {
|
|
531
|
+
expect(res.body).not.toHaveProperty('user');
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
});
|
package/dist/classes/user.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../src/classes/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,qBAAa,IAAK,YAAW,cAAc;IACvC,GAAG,EAAE,GAAG,CAAC;IACT,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;CAC7B"}
|
package/dist/classes/user.js
DELETED
package/dist/classes/user.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/classes/user.ts"],"names":[],"mappings":";;;AAEA,MAAa,IAAI;CAKhB;AALD,oBAKC"}
|
package/dist/controller.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ILoginRequest } from './interfaces/login.request';
|
|
2
|
-
export declare abstract class AuthController<LoginRequest extends ILoginRequest> {
|
|
3
|
-
loginAction(req: any, namespace?: string, redirect?: string): any;
|
|
4
|
-
loginCheckAction(data: LoginRequest, request: any): any;
|
|
5
|
-
logoutAction(req: any): any;
|
|
6
|
-
}
|
|
7
|
-
//# sourceMappingURL=controller.d.ts.map
|
package/dist/controller.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAO3D,8BAAsB,cAAc,CAAC,YAAY,SAAS,aAAa;IAUnE,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,SAAa,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,GAAG;IAarE,gBAAgB,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,GAAG,GAAG,GAAG;IAgBvD,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;CAU9B"}
|
package/dist/controller.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AuthController = void 0;
|
|
4
|
-
class AuthController {
|
|
5
|
-
loginAction(req, namespace = 'user.ops', redirect) {
|
|
6
|
-
const user = req.user;
|
|
7
|
-
if (typeof user === 'object' && user.namespace !== namespace) {
|
|
8
|
-
req.logout();
|
|
9
|
-
}
|
|
10
|
-
return { data: { namespace, user: req.user, redirect } };
|
|
11
|
-
}
|
|
12
|
-
loginCheckAction(data, request) {
|
|
13
|
-
const response = { success: true };
|
|
14
|
-
if (typeof request.user !== 'object') {
|
|
15
|
-
response.success = false;
|
|
16
|
-
}
|
|
17
|
-
if (request.get('accept') === 'application/json') {
|
|
18
|
-
return response;
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
return request.res.redirect('/auth/login');
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
logoutAction(req) {
|
|
25
|
-
if (req.isAuthenticated() === true) {
|
|
26
|
-
req.logOut();
|
|
27
|
-
}
|
|
28
|
-
if (req.get('accept') === 'application/json') {
|
|
29
|
-
return { ok: true };
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
req.res.redirect('/auth/login');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.AuthController = AuthController;
|
|
37
|
-
//# sourceMappingURL=controller.js.map
|
package/dist/controller.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":";;;AAOA,MAAsB,cAAc;IAUhC,WAAW,CAAC,GAAQ,EAAE,SAAS,GAAG,UAAU,EAAE,QAAiB;QAC3D,MAAM,IAAI,GAAQ,GAAG,CAAC,IAAI,CAAC;QAC3B,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,EAAE;YAC1D,GAAG,CAAC,MAAM,EAAE,CAAC;SAChB;QACD,OAAO,EAAE,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC;IAC7D,CAAC;IAOD,gBAAgB,CAAC,IAAkB,EAAE,OAAY;QAC7C,MAAM,QAAQ,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACnC,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;YAClC,QAAQ,CAAC,OAAO,GAAG,KAAK,CAAC;SAC5B;QACD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,kBAAkB,EAAE;YAC9C,OAAO,QAAQ,CAAC;SACnB;aAAM;YACH,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;SAC9C;IACL,CAAC;IAMD,YAAY,CAAC,GAAQ;QACjB,IAAI,GAAG,CAAC,eAAe,EAAE,KAAK,IAAI,EAAE;YAChC,GAAG,CAAC,MAAM,EAAE,CAAC;SAChB;QACD,IAAI,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,kBAAkB,EAAE;YAC1C,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;SACvB;aAAM;YACH,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;SACnC;IACL,CAAC;CACJ;AAjDD,wCAiDC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"emailpassword.d.ts","sourceRoot":"","sources":["../../src/interfaces/emailpassword.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,iBAAiB,EAAE,MAAM,CAAC;CAC7B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"emailpassword.js","sourceRoot":"","sources":["../../src/interfaces/emailpassword.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.payload.d.ts","sourceRoot":"","sources":["../../src/interfaces/jwt.payload.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,EAAE,CAAC;CACnB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.payload.js","sourceRoot":"","sources":["../../src/interfaces/jwt.payload.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"login.request.d.ts","sourceRoot":"","sources":["../../src/interfaces/login.request.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACrB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"login.request.js","sourceRoot":"","sources":["../../src/interfaces/login.request.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memory.provider.d.ts","sourceRoot":"","sources":["../../src/providers/memory.provider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEvD,qBACa,kBAAkB,CAAC,CAAC,SAAS,UAAU,CAAE,YAAW,aAAa,CAAC,CAAC,CAAC;IAC7E,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA6B;IAE7C,OAAO,CAAC,CAAC,SAAS;SAAG,GAAG,IAAI,MAAM,CAAC,GAAG,GAAG;KAAE,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAYvE,YAAY,CAAC,IAAI,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC;CAI/C"}
|