/
ConfigurablePasswordProvider.java
249 lines (220 loc) · 8.85 KB
/
ConfigurablePasswordProvider.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
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
/*
* Copyright (C) 2009-2014 Glencoe Software, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package ome.security.auth;
import java.security.Permissions;
import ome.security.SecuritySystem;
import ome.services.messages.LoginAttemptMessage;
import ome.system.OmeroContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Base class for most {@link PasswordProvider} implementations, providing
* configuration for default behaviors. There is no need for a subclass to
* use this implementation.
*
* @author Josh Moore, josh at glencoesoftware.com
* @see SecuritySystem
* @see Permissions
* @since 4.0
*/
public abstract class ConfigurablePasswordProvider implements PasswordProvider,
PasswordUtility, ApplicationContextAware {
final protected Logger log = LoggerFactory.getLogger(this.getClass());
/**
* Hash implementation to use for encoding passwords to check and changed
* passwords. Default value: MD5 (For the moment, the only supported value!)
*/
protected final String hash;
/**
* Whether or not salting based on the user ID should be attempted.
*/
protected final boolean salt;
/**
* If true, this implementation should return a null on
* {@link #checkPassword(String, String, boolean)} if the user is unknown,
* otherwise a {@link Boolean#FALSE}. Default value: false
*/
protected final boolean ignoreUnknown;
protected final PasswordUtil util;
/**
* Possibly null {@link PasswordUtil} instance which will be used as a
* fallback for password checks if the {@link #util} instance fails.
*/
protected /*final*/ PasswordUtil legacyUtil;
protected OmeroContext ctx;
/**
* Call {@link #ConfigurablePasswordProvider(PasswordUtil, boolean)}
* with "ignoreUnknown" equal to false.
* @param util an instance of the password utility class
*/
public ConfigurablePasswordProvider(PasswordUtil util) {
this(util, false);
}
/**
* Call {@link #ConfigurablePasswordProvider(PasswordUtil, boolean, boolean)}
* with "salt" equal to false.
* @param util an instance of the password utility class
* @param ignoreUnknown if {@link #checkPassword(String, String, boolean)} should
* return {@code null} rather than {@link Boolean#FALSE} for unknown users
*/
public ConfigurablePasswordProvider(PasswordUtil util, boolean ignoreUnknown) {
this(util, ignoreUnknown, false);
}
public ConfigurablePasswordProvider(PasswordUtil util, boolean ignoreUnknown,
boolean salt) {
this.util = util;
this.hash = "MD5";
this.salt = salt;
this.ignoreUnknown = ignoreUnknown;
}
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = (OmeroContext) ctx;
}
public void setLegacyUtil(PasswordUtil legacy) {
this.legacyUtil = legacy;
}
protected Boolean loginAttempt(String user, Boolean success) {
try {
this.ctx.publishMessage(new LoginAttemptMessage(this, user, success));
} catch (Throwable e) {
log.error("LoginAttemptMessage error", e);
}
return success;
}
/**
* Always returns false, override with specific logic.
*/
public boolean hasPassword(String user) {
return false;
}
/**
* If this was constructed with the {@code ignoreUnknown} argument set to
* {@code true}, returns {@code null}, since the base class knows no users.
* Otherwise, returns {@link Boolean#FALSE} specifying that
* authentication should fail.
*/
public Boolean checkPassword(String user, String password, boolean readOnly) {
if (ignoreUnknown) {
return null;
} else {
return Boolean.FALSE;
}
}
/**
* Throws by default.
*/
public void changePassword(String user, String password)
throws PasswordChangeException {
throw new PasswordChangeException(
"Cannot change password with this implementation: "
+ getClass().getName());
}
/**
* Encodes the password as it would be encoded for a check by
* {@link #comparePasswords(String, String)}
*/
public String encodePassword(String newPassword) {
return encodePassword(null, newPassword, false, util);
}
/**
* Encodes the password as it would be encoded for a check by
* {@link #comparePasswords(String, String)} salting the password
* with the given userId if it's provided.
* @param userId a user ID (may be {@code null})
* @param newPassword a password
* @return the encoded password
*/
public String encodeSaltedPassword(Long userId, String newPassword) {
return encodePassword(userId, newPassword, salt, util);
}
protected String encodePassword(Long userId, String newPassword,
boolean salt, PasswordUtil util) {
if (salt) {
return util.prepareSaltedPassword(userId, newPassword);
} else {
return util.preparePassword(newPassword);
}
}
/**
* Compares the password provided by the user (unhashed) against the given
* trusted password. A return value of null, however, is also possible
* with the same semantics as a null from
* {@link #checkPassword(String, String, boolean)}.
*
* For this implementation, if the trusted password is null, return
* {@link Boolean#FALSE}. If the trusted password is empty (only
* whitespace), return {@link Boolean#TRUE}. Otherwise return the result of
* {@link String#equals(Object)}.
*/
public Boolean comparePasswords(String trusted, String provided) {
return comparePasswords(null, trusted, provided);
}
/**
* Compares the password provided by the user (unhashed) against the given
* trusted password. In general, if the trusted password is null, return
* {@link Boolean#FALSE}. If the trusted password is empty (only
* whitespace), return {@link Boolean#TRUE}. Otherwise return the results of
* {@link String#equals(Object)}.
*
* If necessary, falls back to using a legacy password utility class if one was set by {@link #setLegacyUtil(PasswordUtil)}.
* @param userId a user ID
* @param trusted the user's trusted password
* @param provided the provided password
* @return if the provided password matches the trusted password (for which blank matches anything)
*/
public Boolean comparePasswords(Long userId, String trusted, String provided) {
if(comparePasswords(userId, trusted, provided, util)) {
return true;
} else if (legacyUtil != null) {
if (comparePasswords(userId, trusted, provided, legacyUtil)) {
log.error("Matched LEGACY password for Experimenter:{}!", userId);
final String username = util.userName(userId);
if (username != null) {
try {
changePassword(username, provided);
log.info("Upgraded password for Experimenter:{}", userId);
} catch (PasswordChangeException e) {
/* this password provider cannot change the password */
}
}
return true;
}
}
return false;
}
protected boolean comparePasswords(Long userId, String trusted,
String provided, PasswordUtil util) {
if (trusted == null) {
return false;
} else if ("".equals(trusted.trim())) {
return !util.isPasswordRequired(userId);
} else {
boolean salt = (userId != null && this.salt);
String encoded = encodePassword(userId, provided, salt, util);
if (trusted.equals(encoded)) {
return true;
}
encoded = encodePassword(userId, provided, false, util);
return trusted.equals(encoded); // ok unsalted.
}
}
}