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
|
/*
* Copyright 2013 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.tests;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.gitblit.IStoredSettings;
import com.gitblit.auth.HtpasswdAuthProvider;
import com.gitblit.manager.RuntimeManager;
import com.gitblit.manager.UserManager;
import com.gitblit.models.UserModel;
import com.gitblit.tests.mock.MemorySettings;
/**
* Test the Htpasswd user service.
*
*/
public class HtpasswdAuthenticationTest extends GitblitUnitTest {
private static final String RESOURCE_DIR = "src/test/resources/htpasswd/";
private static final String KEY_SUPPORT_PLAINTEXT_PWD = "realm.htpasswd.supportPlaintextPasswords";
private static final int NUM_USERS_HTPASSWD = 10;
private static final MemorySettings MS = new MemorySettings(new HashMap<String, Object>());
private HtpasswdAuthProvider htpasswd;
private MemorySettings getSettings(String userfile, String groupfile, Boolean overrideLA)
{
MS.put("realm.userService", RESOURCE_DIR + "users.conf");
MS.put("realm.htpasswd.userfile", (userfile == null) ? (RESOURCE_DIR + "htpasswd") : userfile);
MS.put("realm.htpasswd.groupfile", (groupfile == null) ? (RESOURCE_DIR + "htgroup") : groupfile);
MS.put("realm.htpasswd.overrideLocalAuthentication", (overrideLA == null) ? "false" : overrideLA.toString());
// Default to keep test the same on all platforms.
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
return MS;
}
private MemorySettings getSettings()
{
return getSettings(null, null, null);
}
private void setupUS()
{
htpasswd = newHtpasswdAuthentication(getSettings());
}
private HtpasswdAuthProvider newHtpasswdAuthentication(IStoredSettings settings) {
RuntimeManager runtime = new RuntimeManager(settings, GitBlitSuite.BASEFOLDER).start();
UserManager users = new UserManager(runtime).start();
HtpasswdAuthProvider htpasswd = new HtpasswdAuthProvider();
htpasswd.setup(runtime, users);
return htpasswd;
}
private void copyInFiles() throws IOException
{
File dir = new File(RESOURCE_DIR);
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String file) {
return file.endsWith(".in");
}
};
for (File inf : dir.listFiles(filter)) {
File dest = new File(inf.getParent(), inf.getName().substring(0, inf.getName().length() - 3));
FileUtils.copyFile(inf, dest);
}
}
private void deleteGeneratedFiles()
{
File dir = new File(RESOURCE_DIR);
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String file) {
return !(file.endsWith(".in"));
}
};
for (File file : dir.listFiles(filter)) {
file.delete();
}
}
@Before
public void setup() throws IOException
{
copyInFiles();
setupUS();
}
@After
public void tearDown()
{
deleteGeneratedFiles();
}
@Test
public void testSetup() throws IOException
{
assertEquals(NUM_USERS_HTPASSWD, htpasswd.getNumberHtpasswdUsers());
}
@Test
public void testAuthenticate()
{
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
UserModel user = htpasswd.authenticate("user1", "pass1".toCharArray());
assertNotNull(user);
assertEquals("user1", user.username);
user = htpasswd.authenticate("user2", "pass2".toCharArray());
assertNotNull(user);
assertEquals("user2", user.username);
// Test different encryptions
user = htpasswd.authenticate("plain", "passWord".toCharArray());
assertNotNull(user);
assertEquals("plain", user.username);
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
user = htpasswd.authenticate("crypt", "password".toCharArray());
assertNotNull(user);
assertEquals("crypt", user.username);
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNotNull(user);
assertEquals("md5", user.username);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNotNull(user);
assertEquals("sha", user.username);
// Test leading and trailing whitespace
user = htpasswd.authenticate("trailing", "whitespace".toCharArray());
assertNotNull(user);
assertEquals("trailing", user.username);
user = htpasswd.authenticate("tabbed", "frontAndBack".toCharArray());
assertNotNull(user);
assertEquals("tabbed", user.username);
user = htpasswd.authenticate("leading", "whitespace".toCharArray());
assertNotNull(user);
assertEquals("leading", user.username);
}
@Test
public void testAttributes()
{
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
UserModel user = htpasswd.authenticate("user1", "pass1".toCharArray());
assertNotNull(user);
assertEquals("El Capitan", user.displayName);
assertEquals("cheffe@example.com", user.emailAddress);
assertTrue(user.canAdmin);
user = htpasswd.authenticate("user2", "pass2".toCharArray());
assertNotNull(user);
assertEquals("User Two", user.displayName);
assertTrue(user.canCreate);
assertTrue(user.canFork);
}
@Test
public void testAuthenticateDenied()
{
UserModel user = null;
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
user = htpasswd.authenticate("user1", "".toCharArray());
assertNull("User 'user1' falsely authenticated.", user);
user = htpasswd.authenticate("user1", "pass2".toCharArray());
assertNull("User 'user1' falsely authenticated.", user);
user = htpasswd.authenticate("user2", "lalala".toCharArray());
assertNull("User 'user2' falsely authenticated.", user);
user = htpasswd.authenticate("user3", "disabled".toCharArray());
assertNull("User 'user3' falsely authenticated.", user);
user = htpasswd.authenticate("user4", "disabled".toCharArray());
assertNull("User 'user4' falsely authenticated.", user);
user = htpasswd.authenticate("plain", "text".toCharArray());
assertNull("User 'plain' falsely authenticated.", user);
user = htpasswd.authenticate("plain", "password".toCharArray());
assertNull("User 'plain' falsely authenticated.", user);
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
user = htpasswd.authenticate("crypt", "".toCharArray());
assertNull("User 'cyrpt' falsely authenticated.", user);
user = htpasswd.authenticate("crypt", "passwd".toCharArray());
assertNull("User 'crypt' falsely authenticated.", user);
user = htpasswd.authenticate("md5", "".toCharArray());
assertNull("User 'md5' falsely authenticated.", user);
user = htpasswd.authenticate("md5", "pwd".toCharArray());
assertNull("User 'md5' falsely authenticated.", user);
user = htpasswd.authenticate("sha", "".toCharArray());
assertNull("User 'sha' falsely authenticated.", user);
user = htpasswd.authenticate("sha", "letmein".toCharArray());
assertNull("User 'sha' falsely authenticated.", user);
user = htpasswd.authenticate(" tabbed", "frontAndBack".toCharArray());
assertNull("User 'tabbed' falsely authenticated.", user);
user = htpasswd.authenticate(" leading", "whitespace".toCharArray());
assertNull("User 'leading' falsely authenticated.", user);
}
@Test
public void testCleartextIntrusion()
{
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
assertNull(htpasswd.authenticate("md5", "$apr1$qAGGNfli$sAn14mn.WKId/3EQS7KSX0".toCharArray()));
assertNull(htpasswd.authenticate("sha", "{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g=".toCharArray()));
assertNull(htpasswd.authenticate("user1", "#externalAccount".toCharArray()));
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
assertNull(htpasswd.authenticate("md5", "$apr1$qAGGNfli$sAn14mn.WKId/3EQS7KSX0".toCharArray()));
assertNull(htpasswd.authenticate("sha", "{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g=".toCharArray()));
assertNull(htpasswd.authenticate("user1", "#externalAccount".toCharArray()));
}
@Test
public void testCryptVsPlaintext()
{
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "false");
assertNull(htpasswd.authenticate("crypt", "6TmlbxqZ2kBIA".toCharArray()));
assertNotNull(htpasswd.authenticate("crypt", "password".toCharArray()));
MS.put(KEY_SUPPORT_PLAINTEXT_PWD, "true");
assertNotNull(htpasswd.authenticate("crypt", "6TmlbxqZ2kBIA".toCharArray()));
assertNull(htpasswd.authenticate("crypt", "password".toCharArray()));
}
@Test
public void testChangeHtpasswdFile()
{
UserModel user;
// User default set up.
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNotNull(user);
assertEquals("md5", user.username);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNotNull(user);
assertEquals("sha", user.username);
user = htpasswd.authenticate("blueone", "GoBlue!".toCharArray());
assertNull(user);
user = htpasswd.authenticate("bluetwo", "YayBlue!".toCharArray());
assertNull(user);
// Switch to different htpasswd file.
getSettings(RESOURCE_DIR + "htpasswd-user", null, null);
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNull(user);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNull(user);
user = htpasswd.authenticate("blueone", "GoBlue!".toCharArray());
assertNotNull(user);
assertEquals("blueone", user.username);
user = htpasswd.authenticate("bluetwo", "YayBlue!".toCharArray());
assertNotNull(user);
assertEquals("bluetwo", user.username);
}
@Test
public void testChangeHtpasswdFileNotExisting()
{
UserModel user;
// User default set up.
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNotNull(user);
assertEquals("md5", user.username);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNotNull(user);
assertEquals("sha", user.username);
user = htpasswd.authenticate("blueone", "GoBlue!".toCharArray());
assertNull(user);
user = htpasswd.authenticate("bluetwo", "YayBlue!".toCharArray());
assertNull(user);
// Switch to different htpasswd file that doesn't exist.
// Currently we stop working with old users upon this change.
getSettings(RESOURCE_DIR + "no-such-file", null, null);
user = htpasswd.authenticate("md5", "password".toCharArray());
assertNull(user);
user = htpasswd.authenticate("sha", "password".toCharArray());
assertNull(user);
user = htpasswd.authenticate("blueone", "GoBlue!".toCharArray());
assertNull(user);
user = htpasswd.authenticate("bluetwo", "YayBlue!".toCharArray());
assertNull(user);
}
}
|