-
Notifications
You must be signed in to change notification settings - Fork 1
/
HtPasswdFile.java
142 lines (126 loc) · 4.72 KB
/
HtPasswdFile.java
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
/**
* The MIT License
*
* Copyright (c) 2014, Kestutis Kupciunas (aka kesha)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.htpasswd;
import java.util.HashMap;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
/**
* htpasswd file parser and password checker. Supported hash types:
* <ul>
* <li> MD5 ($apr1$)
* <li> SHA1 ({SHA})
* <li> CRYPT (unix crypt)
* </ul>
* BCRYPT has is not supported yet, because jbcrypt library uses $2a$ salt
* revision, while apache htpasswd generates passwords using $2y$ salt revision
*
* @author kesha (Kestutis Kupciunas)
*/
public class HtPasswdFile extends HtFile {
protected HashMap<String, String> entries = new HashMap<String, String>();
@Override
public void put(String key, String value) {
if (key.isEmpty() || value.isEmpty()) {
return;
}
entries.put(key, value);
}
@Override
public void clear() {
entries.clear();
}
/**
* Returns the hashed password entry for the given user.
*
* @param user user name
* @return hashed password entry for the given user (or <code>null</code>
* if given user does not exist)
*/
public String getPassword(String user) {
return entries.get(user);
}
/**
* Validates the hashed password of the given user against plain text password.
*
* @param user user name from htpasswd file
* @param password plain text password to validate
* @return <code>true</code> if password matches, <code>false</code> - otherwise
*/
public boolean isPasswordValid(String user, String password) {
String hashed = this.getPassword(user);
if (hashed == null)
return false;
Algorithm algo = getPasswordAlgorithm(hashed);
switch (algo) {
case MD5:
return validateMd5Password(hashed, password);
case SHA:
return validateShaPassword(hashed, password);
case CRYPT:
return validateCryptPassword(hashed, password);
case BCRYPT:
// Bcrypt currently unsupported, as jbcrypt uses $2a$ salt revision
// and htpasswd utility hashes passwords with $2y$ salt revision
//return BCrypt.checkpw(password, hashed);
default:
throw new IllegalStateException("Unsupported password format: " + algo);
}
}
private static boolean validateMd5Password(String hashed, String plain) {
String result = Md5Crypt.apr1Crypt(plain, hashed);
return hashed.equals(result);
}
private static boolean validateShaPassword(String hashed, String plain) {
String result = Base64.encodeBase64String(DigestUtils.sha1(plain));
String hashOnly = hashed.substring(5); // skip "{SHA1}"
return hashOnly.equals(result);
}
private static boolean validateCryptPassword(String hashed, String plain) {
String result = Crypt.crypt(plain, hashed);
return hashed.equals(result);
}
protected enum Algorithm {
MD5,
SHA,
CRYPT,
BCRYPT,
UNKNOWN
}
protected static Algorithm getPasswordAlgorithm(String passwd) {
if (passwd == null)
return Algorithm.UNKNOWN;
if (passwd.startsWith("$apr1$")) {
return Algorithm.MD5;
}
if (passwd.startsWith("{SHA}")) {
return Algorithm.SHA;
}
if (passwd.startsWith("$2a$") || passwd.startsWith("$2x$") || passwd.startsWith("$2y$")) {
return Algorithm.BCRYPT;
}
return Algorithm.CRYPT;
}
}