-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathwebauthController.test.ts
373 lines (313 loc) · 12.5 KB
/
webauthController.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { strict as assert } from 'node:assert';
import crypto from 'node:crypto';
import { basename } from 'node:path';
import { app, mock } from 'egg-mock/bootstrap';
import { AuthAdapter } from '../../../app/infra/AuthAdapter';
import { CacheAdapter } from '../../../app/common/adapter/CacheAdapter';
import { UserService } from '../../../app/core/service/UserService';
import { UserRepository } from '../../../app/repository/UserRepository';
import { genRSAKeys, encryptRSA } from '../../../app/common/CryptoUtil';
describe('test/port/webauth/webauthController.test.ts', () => {
describe('/-/v1/login', () => {
it('should get authUrl work', async () => {
const res = await app.httpRequest()
.post('/-/v1/login')
.send({
hostname: 'test',
});
assert.equal(res.status, 200);
assert(res.body.loginUrl);
assert(res.body.doneUrl);
const loginSessionId = basename(res.body.loginUrl);
const doneSessionId = basename(res.body.doneUrl);
assert.equal(loginSessionId, doneSessionId);
assert.equal(loginSessionId.length, 36);
});
it('should hostname is optional', async () => {
const res = await app.httpRequest()
.post('/-/v1/login');
assert.equal(res.status, 200);
});
});
describe('GET /-/v1/login/request/session/:sessionId', () => {
let sessionId = '';
const rsaKeys = genRSAKeys();
beforeEach(async () => {
sessionId = crypto.randomUUID();
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, '');
await cacheAdapter.set(`${sessionId}_privateKey`, rsaKeys.privateKey);
});
it('should check sessionId type', async () => {
const res = await app.httpRequest()
.get('/-/v1/login/request/session/123');
assert.equal(res.status, 422);
assert.equal(res.body.error, '[INVALID_PARAM] sessionId: must NOT have fewer than 36 characters');
});
it('should check sessionId exists', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/request/session/${crypto.randomUUID()}`);
assert.equal(res.status, 404);
assert(/Session not found/.test(res.text));
assert.equal(res.headers['content-type'], 'text/html; charset=utf-8');
});
it('should render login.html', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/request/session/${sessionId}`);
assert.equal(res.status, 200);
assert(/<title>Sign in to CNPM<\/title>/.test(res.text));
});
});
describe('POST /-/v1/login/request/session/:sessionId', () => {
let sessionId = '';
const rsaKeys = genRSAKeys();
beforeEach(async () => {
sessionId = crypto.randomUUID();
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, '');
await cacheAdapter.set(`${sessionId}_privateKey`, rsaKeys.privateKey);
});
it('should check sessionId type', async () => {
const res = await app.httpRequest()
.post('/-/v1/login/request/session/123');
assert.equal(res.status, 422);
assert.equal(res.body.error, '[INVALID_PARAM] sessionId: must NOT have fewer than 36 characters');
});
it('should check sessionId exists', async () => {
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${crypto.randomUUID()}`);
assert.equal(res.status, 200);
assert(/Session not found/.test(res.text));
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
});
describe('should verify login request body', () => {
beforeEach(async () => {
const userService = await app.getEggObject(UserService);
await userService.create({
name: 'banana',
email: 'banana@fruits.com',
password: 'flymetothemoon',
ip: 'localhost',
});
});
it('should login success', async () => {
const password = encryptRSA(rsaKeys.publicKey, 'flymetothemoon');
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: 'banana',
password,
},
});
assert.equal(res.status, 200);
assert.equal(res.body.ok, true);
});
it('should check password', async () => {
const password = encryptRSA(rsaKeys.publicKey, 'incorrect_password');
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: 'banana',
password,
},
});
assert.equal(res.status, 200);
assert(/Please check your login name and password/.test(res.body.message));
});
it('should check user params', async () => {
const password = encryptRSA(rsaKeys.publicKey, 'incorrect_password');
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: '',
password,
},
});
assert.equal(res.status, 200);
assert(/Unauthorized, Validation Failed/.test(res.body.message));
});
it('should check authentication user (unbound webauthn) when enableWebauthn', async () => {
mock(app.config.cnpmcore, 'enableWebAuthn', true);
app.mockContext({
hostname: 'localhost',
});
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: 'banana',
},
wanCredentialAuthData: {},
});
assert.equal(res.status, 200);
assert(/Unauthorized, Please check your login name/.test(res.body.message));
});
it('should add user', async () => {
const password = encryptRSA(rsaKeys.publicKey, 'newaccount_password');
const userRepository = await app.getEggObject(UserRepository);
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: 'orange',
password,
},
});
assert.equal(res.status, 200);
assert.equal(res.body.ok, true);
const user = await userRepository.findUserByName('orange');
assert(user);
});
it('only support admin when not allowPublicRegistration', async () => {
mock(app.config.cnpmcore, 'allowPublicRegistration', false);
const password = encryptRSA(rsaKeys.publicKey, 'newaccount_password');
const res = await app.httpRequest()
.post(`/-/v1/login/request/session/${sessionId}`)
.send({
accData: {
username: 'orange',
password,
},
});
assert.equal(res.status, 200);
assert(/Public registration is not allowed/.test(res.body.message));
});
});
});
describe('/-/v1/login/request/prepare/:sessionId', () => {
let sessionId = '';
const rsaKeys = genRSAKeys();
beforeEach(async () => {
sessionId = crypto.randomUUID();
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, '');
await cacheAdapter.set(`${sessionId}_privateKey`, rsaKeys.privateKey);
const userService = await app.getEggObject(UserService);
const user = await userService.create({
name: 'banana',
email: 'banana@fruits.com',
password: 'flymetothemoon',
ip: 'localhost',
});
await userService.createWebauthnCredential(user.user.userId, {
credentialId: 'mock_credential_id',
publicKey: 'mock_public_key',
});
});
it('should check sessionId type', async () => {
const res = await app.httpRequest()
.get('/-/v1/login/request/prepare/123?name=banana');
assert.equal(res.status, 422);
assert.equal(res.body.error, '[INVALID_PARAM] sessionId: must NOT have fewer than 36 characters');
});
it('should check sessionId exists', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/request/prepare/${crypto.randomUUID()}?name=banana`);
assert.equal(res.status, 200);
assert(/Session not found/.test(res.text));
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
});
it('should get prepare with authentication options', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/request/prepare/${sessionId}?name=banana`);
assert.equal(res.status, 200);
assert(typeof res.body.wanCredentialAuthOption === 'object');
});
it('should get prepare with registration options', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/request/prepare/${sessionId}?name=apple`);
assert.equal(res.status, 200);
assert(typeof res.body.wanCredentialRegiOption === 'object');
});
});
describe('/-/v1/login/sso/:sessionId', () => {
let sessionId = '';
beforeEach(async () => {
sessionId = crypto.randomUUID();
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, '');
mock(AuthAdapter.prototype, 'ensureCurrentUser', async () => {
return {
name: 'banana',
email: 'banana@fruits.com',
};
});
});
it('should sso login work', async () => {
const res = await app.httpRequest()
.post(`/-/v1/login/sso/${sessionId}`);
assert.equal(res.status, 200);
});
it('should check sessionId exists', async () => {
const res = await app.httpRequest()
.post('/-/v1/login/sso/banana');
assert.equal(res.status, 422);
assert.equal(res.body.error, '[INVALID_PARAM] sessionId: must NOT have fewer than 36 characters');
});
it('should ensure sessionId valid', async () => {
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.delete(sessionId);
const res = await app.httpRequest()
.post(`/-/v1/login/sso/${sessionId}`);
assert.equal(res.status, 403);
assert.equal(res.body.error, '[FORBIDDEN] invalid sessionId');
});
it('should error when invalid userinfo', async () => {
mock(AuthAdapter.prototype, 'ensureCurrentUser', async () => {
return null;
});
const res = await app.httpRequest()
.post(`/-/v1/login/sso/${sessionId}`);
assert.equal(res.status, 403);
assert.equal(res.body.error, '[FORBIDDEN] invalid user info');
});
});
describe('/-/v1/login/request/success', () => {
it('should work', async () => {
const res = await app.httpRequest()
.get('/-/v1/login/request/success');
assert.equal(res.status, 200);
assert.equal(res.headers['content-type'], 'text/html; charset=utf-8');
assert(/Authorization Successful/.test(res.text));
});
});
describe('/-/v1/login/done/session/:sessionId', () => {
let sessionId = '';
beforeEach(async () => {
sessionId = crypto.randomUUID();
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, '');
});
it('should check sessionId type', async () => {
const res = await app.httpRequest()
.get('/-/v1/login/done/session/123');
assert.equal(res.status, 422);
assert.equal(res.body.error, '[INVALID_PARAM] sessionId: must NOT have fewer than 36 characters');
});
it('should check sessionId exists', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/done/session/${crypto.randomUUID()}`);
assert.equal(res.status, 404);
assert.equal(res.body.error, '[NOT_FOUND] session not found');
});
it('should re-validate sessionId', async () => {
const res = await app.httpRequest()
.get(`/-/v1/login/done/session/${sessionId}`);
assert.equal(res.status, 202);
assert.equal(res.body.message, 'processing');
assert.equal(res.headers['retry-after'], '1');
});
it('should check sessionId exists', async () => {
const cacheAdapter = await app.getEggObject(CacheAdapter);
await cacheAdapter.set(sessionId, 'banana');
const res = await app.httpRequest()
.get(`/-/v1/login/done/session/${sessionId}`);
assert.equal(res.status, 200);
assert.equal(res.body.token, 'banana');
assert.equal(await cacheAdapter.get(sessionId), null);
});
});
});