forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassport-auth-proxy.js
More file actions
279 lines (235 loc) · 8.11 KB
/
passport-auth-proxy.js
File metadata and controls
279 lines (235 loc) · 8.11 KB
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
const assert = require('assert');
const request = require('supertest');
const TestUtil = require('../utils');
describe('auth/passport-proxy-auth', function () {
it('auto sign up creates user w/default role', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyDefaultRole: 'editor',
authProxyHeaders: 'email:X-WEBAUTH-EMAIL',
});
await utils.init();
const { body } = await request(utils.app)
.get('/api/users')
.expect(200);
const user = body[0];
assert.equal(user.role, 'editor');
});
it('Does not start session when used via middleware', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyDefaultRole: 'editor',
authProxyHeaders: 'email:X-WEBAUTH-EMAIL',
});
await utils.init();
const agent = request.agent(utils.app);
// This api call passively authenticates, auto-creates the user and returns api result
await agent
.get('/api/users')
.expect(200);
// This call should respond, but with no current user
const r2 = await agent.get('/api/app').expect(200);
assert(!r2.body.currentUser);
// This call requiring auth should respond 401
await agent.get('/api/users').expect(401);
});
it('401 has expected errors object', async function () {
// Don't care about what is actually tested here
// other than to ensure the body has errors object
// Rest of 401s can be trusted to be expected shape
const utils = new TestUtil({
authProxyEnabled: true,
authProxyHeaders: 'email:X-WEBAUTH-EMAIL',
});
await utils.init();
const res = await request(utils.app)
.get('/api/users')
.expect(401);
assert(res.body.title);
});
it('401 if auto sign up and missing default role', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders: 'email:X-WEBAUTH-EMAIL',
});
await utils.init();
await request(utils.app)
.get('/api/users')
.expect(401);
});
it('401 if auto sign up and missing email', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders:
'id:X-WEBAUTH-ID name:X-WEBAUTH-NAME role:X-WEBAUTH-ROLE',
});
await utils.init();
await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-ID', 'test001')
.set('X-WEBAUTH-NAME', 'Test user')
.set('X-WEBAUTH-ROLE', 'admin')
.expect(401);
});
it('401 if missing id and email', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders:
'id:X-WEBAUTH-ID email:X-WEBAUTH-EMAIL name:X-WEBAUTH-NAME role:X-WEBAUTH-ROLE',
});
await utils.init();
await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-NAME', 'Test user')
.set('X-WEBAUTH-ROLE', 'admin')
.expect(401);
});
it('401 if user does not exist and auto sign up turned off', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: false,
authProxyHeaders:
'id:X-WEBAUTH-ID email:X-WEBAUTH-EMAIL name:X-WEBAUTH-NAME role:X-WEBAUTH-ROLE',
});
await utils.init();
await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-ID', 'test001')
.set('X-WEBAUTH-NAME', 'Test user')
.set('X-WEBAUTH-ROLE', 'admin')
.expect(401);
});
it('401 if proxy auth turned off', async function () {
const utils = new TestUtil({
authProxyEnabled: false,
});
await utils.init();
await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-ID', 'test001')
.set('X-WEBAUTH-NAME', 'Test user')
.set('X-WEBAUTH-ROLE', 'admin')
.expect(401);
});
it('All fields used for user without default role', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders:
'id:X-WEBAUTH-ID email:X-WEBAUTH-EMAIL name:X-WEBAUTH-NAME role:X-WEBAUTH-ROLE data.customField:X-WEBAUTH-CUSTOM-FIELD',
});
await utils.init();
const { body } = await request(utils.app)
.get('/api/users/test001')
.set('X-WEBAUTH-ID', 'test001')
.set('X-WEBAUTH-NAME', 'Test user')
.set('X-WEBAUTH-ROLE', 'admin')
.set('X-WEBAUTH-CUSTOM-FIELD', 'custom data value')
.expect(200);
const user = body;
assert.equal(user.role, 'admin');
assert.equal(user.name, 'Test user');
assert.equal(user.id, 'test001');
assert.equal(user.data.customField, 'custom data value');
});
it('Matches existing user via email', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyHeaders: 'email:X-WEBAUTH-EMAIL',
});
await utils.init(true);
const { body } = await request(utils.app)
.get('/api/users')
.expect(200);
assert.equal(user.role, 'admin');
});
it('Matches existing user via id', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyHeaders: 'id:X-WEBAUTH-ID',
});
await utils.init(true);
const id = utils.users.admin.id;
const { body } = await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-ID', id)
.expect(200);
const user = body.find((user) => user.id === id);
assert.equal(user.role, 'admin');
});
it('Disabled user cannot log in (auth proxy)', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyHeaders: 'id:X-WEBAUTH-ID',
});
await utils.init(true);
const id = utils.users.editor.id;
await request(utils.app)
.get('/api/app')
.set('X-WEBAUTH-ID', id)
.expect(200);
await utils.models.users.update(id, { disabled: true });
await request(utils.app)
.get('/api/app')
.set('X-WEBAUTH-ID', id)
.expect(401);
});
it('Updates existing user if changes', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders: 'email:X-WEBAUTH-EMAIL name:X-WEBAUTH-NAME',
});
await utils.init(true);
const { body } = await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-NAME', 'New admin name')
.expect(200);
assert.equal(user.name, 'New admin name');
});
it('User not updated if no changes', async function () {
const utils = new TestUtil({
authProxyEnabled: true,
authProxyAutoSignUp: true,
authProxyHeaders: 'email:X-WEBAUTH-EMAIL name:X-WEBAUTH-NAME',
});
await utils.init(true);
const response1 = await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-NAME', 'New admin name')
.expect(200);
const user1 = response1.body.find(
);
assert.equal(user1.name, 'New admin name');
await new Promise((resolve) => setTimeout(resolve, 100));
const response2 = await request(utils.app)
.get('/api/users')
.set('X-WEBAUTH-NAME', 'New admin name')
.expect(200);
const user2 = response2.body.find(
);
assert.equal(user1.updatedAt, user2.updatedAt);
});
});