forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLdapAuth.spec.js
212 lines (198 loc) · 7.53 KB
/
LdapAuth.spec.js
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
const ldap = require('../lib/Adapters/Auth/ldap');
const mockLdapServer = require('./support/MockLdapServer');
const fs = require('fs');
const port = 12345;
const sslport = 12346;
describe('Ldap Auth', () => {
it('Should fail with missing options', done => {
ldap
.validateAuthData({ id: 'testuser', password: 'testpw' })
.then(done.fail)
.catch(err => {
jequal(err.message, 'LDAP auth configuration missing');
done();
});
});
it('Should return a resolved promise when validating the app id', done => {
ldap.validateAppId().then(done).catch(done.fail);
});
it('Should succeed with right credentials', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example');
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
};
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
server.close(done);
});
it('Should succeed with right credentials when LDAPS is used and certifcate is not checked', async done => {
const server = await mockLdapServer(sslport, 'uid=testuser, o=example', false, true);
const options = {
suffix: 'o=example',
url: `ldaps://localhost:${sslport}`,
dn: 'uid={{id}}, o=example',
tlsOptions: { rejectUnauthorized: false },
};
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
server.close(done);
});
it('Should succeed when LDAPS is used and the presented certificate is the expected certificate', async done => {
const server = await mockLdapServer(sslport, 'uid=testuser, o=example', false, true);
const options = {
suffix: 'o=example',
url: `ldaps://localhost:${sslport}`,
dn: 'uid={{id}}, o=example',
tlsOptions: {
ca: fs.readFileSync(__dirname + '/support/cert/cert.pem'),
rejectUnauthorized: true,
},
};
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
server.close(done);
});
it('Should fail when LDAPS is used and the presented certificate is not the expected certificate', async done => {
const server = await mockLdapServer(sslport, 'uid=testuser, o=example', false, true);
const options = {
suffix: 'o=example',
url: `ldaps://localhost:${sslport}`,
dn: 'uid={{id}}, o=example',
tlsOptions: {
ca: fs.readFileSync(__dirname + '/support/cert/anothercert.pem'),
rejectUnauthorized: true,
},
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAPS: Certificate mismatch');
}
server.close(done);
});
it('Should fail when LDAPS is used certifcate matches but credentials are wrong', async done => {
const server = await mockLdapServer(sslport, 'uid=testuser, o=example', false, true);
const options = {
suffix: 'o=example',
url: `ldaps://localhost:${sslport}`,
dn: 'uid={{id}}, o=example',
tlsOptions: {
ca: fs.readFileSync(__dirname + '/support/cert/cert.pem'),
rejectUnauthorized: true,
},
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'wrong!' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAP: Wrong username or password');
}
server.close(done);
});
it('Should fail with wrong credentials', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example');
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'wrong!' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAP: Wrong username or password');
}
server.close(done);
});
it('Should succeed if user is in given group', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example');
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
groupCn: 'powerusers',
groupFilter: '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))',
};
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
server.close(done);
});
it('Should fail if user is not in given group', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example');
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
groupCn: 'groupTheUserIsNotIn',
groupFilter: '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))',
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAP: User not in group');
}
server.close(done);
});
it('Should fail if the LDAP server does not allow searching inside the provided suffix', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example');
const options = {
suffix: 'o=invalid',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
groupCn: 'powerusers',
groupFilter: '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))',
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAP group search failed');
}
server.close(done);
});
it('Should fail if the LDAP server encounters an error while searching', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example', true);
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
groupCn: 'powerusers',
groupFilter: '(&(uniqueMember=uid={{id}}, o=example)(objectClass=groupOfUniqueNames))',
};
try {
await ldap.validateAuthData({ id: 'testuser', password: 'secret' }, options);
fail();
} catch (err) {
expect(err.message).toBe('LDAP group search failed');
}
server.close(done);
});
it('Should delete the password from authData after validation', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example', true);
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
};
const authData = { id: 'testuser', password: 'secret' };
await ldap.validateAuthData(authData, options);
expect(authData).toEqual({ id: 'testuser' });
server.close(done);
});
it('Should not save the password in the user record after authentication', async done => {
const server = await mockLdapServer(port, 'uid=testuser, o=example', true);
const options = {
suffix: 'o=example',
url: `ldap://localhost:${port}`,
dn: 'uid={{id}}, o=example',
};
await reconfigureServer({ auth: { ldap: options } });
const authData = { authData: { id: 'testuser', password: 'secret' } };
const returnedUser = await Parse.User.logInWith('ldap', authData);
const query = new Parse.Query('User');
const user = await query.equalTo('objectId', returnedUser.id).first({ useMasterKey: true });
expect(user.get('authData')).toEqual({ ldap: { id: 'testuser' } });
expect(user.get('authData').ldap.password).toBeUndefined();
server.close(done);
});
});