[14295] | 1 | package org.greenstone.gsdl3.service;
|
---|
| 2 |
|
---|
[25722] | 3 | import java.io.File;
|
---|
| 4 | import java.io.Serializable;
|
---|
[25318] | 5 | import java.math.BigInteger;
|
---|
[25722] | 6 | import java.sql.SQLException;
|
---|
[25258] | 7 | import java.util.ArrayList;
|
---|
[25124] | 8 | import java.util.HashMap;
|
---|
[25258] | 9 | import java.util.UUID;
|
---|
[14295] | 10 | import java.util.Vector;
|
---|
| 11 | import java.util.regex.Pattern;
|
---|
| 12 |
|
---|
[25258] | 13 | import net.tanesha.recaptcha.ReCaptchaImpl;
|
---|
| 14 | import net.tanesha.recaptcha.ReCaptchaResponse;
|
---|
| 15 |
|
---|
[26425] | 16 | import org.apache.commons.codec.digest.DigestUtils;
|
---|
[25722] | 17 | import org.greenstone.gsdl3.util.DerbyWrapper;
|
---|
| 18 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 19 | import org.greenstone.gsdl3.util.UserQueryResult;
|
---|
| 20 | import org.greenstone.gsdl3.util.UserTermInfo;
|
---|
[28120] | 21 | import org.greenstone.util.GlobalProperties;
|
---|
[25722] | 22 | import org.w3c.dom.Element;
|
---|
| 23 | import org.w3c.dom.NodeList;
|
---|
| 24 |
|
---|
[24978] | 25 | public class Authentication extends ServiceRack
|
---|
| 26 | {
|
---|
[25852] | 27 | //Some useful constants
|
---|
| 28 | protected static final int USERNAME_MIN_LENGTH = 2;
|
---|
| 29 | protected static final int USERNAME_MAX_LENGTH = 30;
|
---|
| 30 | protected static final int PASSWORD_MIN_LENGTH = 3;
|
---|
| 31 | protected static final int PASSWORD_MAX_LENGTH = 64;
|
---|
[26425] | 32 |
|
---|
[25258] | 33 | //Error codes
|
---|
| 34 | protected static final int NO_ERROR = 0;
|
---|
| 35 | protected static final int ERROR_REQUEST_HAS_NO_PARAM_LIST = -1;
|
---|
| 36 | protected static final int ERROR_NOT_LOGGED_IN = -2;
|
---|
| 37 | protected static final int ERROR_ADMIN_NOT_LOGGED_IN = -3;
|
---|
| 38 | protected static final int ERROR_COULD_NOT_GET_USER_INFO = -4;
|
---|
| 39 | protected static final int ERROR_USERNAME_NOT_SPECIFIED = -5;
|
---|
| 40 | protected static final int ERROR_REQUESTED_USER_NOT_FOUND = -6;
|
---|
| 41 | protected static final int ERROR_SQL_EXCEPTION = -7;
|
---|
| 42 | protected static final int ERROR_INVALID_USERNAME = -8;
|
---|
[25852] | 43 | protected static final int ERROR_PASSWORD_NOT_ENTERED = -9;
|
---|
| 44 | protected static final int ERROR_PASSWORD_TOO_SHORT = -10;
|
---|
| 45 | protected static final int ERROR_PASSWORD_TOO_LONG = -11;
|
---|
| 46 | protected static final int ERROR_PASSWORD_USES_ILLEGAL_CHARACTERS = -12;
|
---|
| 47 | protected static final int ERROR_INCORRECT_PASSWORD = -13;
|
---|
| 48 | protected static final int ERROR_USER_ALREADY_EXISTS = -14;
|
---|
| 49 | protected static final int ERROR_ADDING_USER = -15;
|
---|
| 50 | protected static final int ERROR_REMOVING_USER = -16;
|
---|
| 51 | protected static final int ERROR_CAPTCHA_DOES_NOT_MATCH = -17;
|
---|
| 52 | protected static final int ERROR_CAPTCHA_MISSING = -18;
|
---|
| 53 | protected static final int ERROR_NOT_AUTHORISED = -19;
|
---|
[25258] | 54 |
|
---|
| 55 | protected static final HashMap<Integer, String> _errorMessageMap;
|
---|
| 56 | static
|
---|
| 57 | {
|
---|
| 58 | //Corresponding error messages
|
---|
| 59 | HashMap<Integer, String> errorMessageMap = new HashMap<Integer, String>();
|
---|
| 60 | errorMessageMap.put(ERROR_REQUEST_HAS_NO_PARAM_LIST, "The list of parameters for this request was empty.");
|
---|
| 61 | errorMessageMap.put(ERROR_NOT_LOGGED_IN, "You must be logged in to access this page.");
|
---|
| 62 | errorMessageMap.put(ERROR_ADMIN_NOT_LOGGED_IN, "You must be logged in as an administrator to access this page.");
|
---|
| 63 | errorMessageMap.put(ERROR_COULD_NOT_GET_USER_INFO, "There was a error getting the user information.");
|
---|
| 64 | errorMessageMap.put(ERROR_USERNAME_NOT_SPECIFIED, "No username was specified.");
|
---|
| 65 | errorMessageMap.put(ERROR_REQUESTED_USER_NOT_FOUND, "The requested user was not found in the database.");
|
---|
| 66 | errorMessageMap.put(ERROR_SQL_EXCEPTION, "There was an SQL exception while accessing the database.");
|
---|
| 67 | errorMessageMap.put(ERROR_INVALID_USERNAME, "The username specified was invalid.");
|
---|
[25852] | 68 | errorMessageMap.put(ERROR_PASSWORD_NOT_ENTERED, "No password was entered.");
|
---|
| 69 | errorMessageMap.put(ERROR_PASSWORD_TOO_SHORT, "The password you entered was too short (minimum of 3 characters).");
|
---|
| 70 | errorMessageMap.put(ERROR_PASSWORD_TOO_LONG, "The password you entered was too long (maximum of 64 characters).");
|
---|
| 71 | errorMessageMap.put(ERROR_PASSWORD_USES_ILLEGAL_CHARACTERS, "The password you entered contains illegal characters.");
|
---|
[25258] | 72 | errorMessageMap.put(ERROR_INCORRECT_PASSWORD, "The password specified was incorrect.");
|
---|
| 73 | errorMessageMap.put(ERROR_USER_ALREADY_EXISTS, "This user already exists and therefore cannot be added.");
|
---|
| 74 | errorMessageMap.put(ERROR_ADDING_USER, "There was an error adding this user to the database.");
|
---|
| 75 | errorMessageMap.put(ERROR_REMOVING_USER, "There was an error removing this user from the database.");
|
---|
| 76 | errorMessageMap.put(ERROR_CAPTCHA_DOES_NOT_MATCH, "The words you entered did not match the image, please try again.");
|
---|
| 77 | errorMessageMap.put(ERROR_CAPTCHA_MISSING, "The information from the captcha is missing.");
|
---|
| 78 | errorMessageMap.put(ERROR_NOT_AUTHORISED, "You are not authorised to access this page.");
|
---|
| 79 |
|
---|
| 80 | _errorMessageMap = errorMessageMap;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //Admin-required operations
|
---|
| 84 | protected static final String LIST_USERS = "ListUsers";
|
---|
| 85 | protected static final String PERFORM_ADD = "PerformAdd";
|
---|
| 86 | protected static final String PERFORM_EDIT = "PerformEdit";
|
---|
| 87 | protected static final String ADD_USER = "AddUser";
|
---|
| 88 | protected static final String EDIT_USER = "EditUser";
|
---|
| 89 | protected static final String PERFORM_DELETE_USER = "PerformDeleteUser";
|
---|
| 90 |
|
---|
| 91 | protected static final ArrayList<String> _adminOpList;
|
---|
| 92 | static
|
---|
| 93 | {
|
---|
| 94 | ArrayList<String> opList = new ArrayList<String>();
|
---|
| 95 | opList.add(LIST_USERS);
|
---|
| 96 | opList.add(PERFORM_ADD);
|
---|
| 97 | opList.add(PERFORM_EDIT);
|
---|
| 98 | opList.add(EDIT_USER);
|
---|
| 99 | opList.add(PERFORM_DELETE_USER);
|
---|
| 100 |
|
---|
| 101 | _adminOpList = opList;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | //User-required operations
|
---|
| 105 | protected static final String ACCOUNT_SETTINGS = "AccountSettings";
|
---|
| 106 | protected static final String PERFORM_ACCOUNT_EDIT = "PerformAccEdit";
|
---|
| 107 | protected static final String PERFORM_RESET_PASSWORD = "PerformResetPassword";
|
---|
[26233] | 108 | protected static final String PERFORM_CHANGE_PASSWORD = "PerformChangePassword";
|
---|
| 109 | protected static final String PERFORM_RETRIEVE_PASSWORD = "PerformRetrievePassword";
|
---|
[25258] | 110 | protected static final ArrayList<String> _userOpList;
|
---|
| 111 | static
|
---|
| 112 | {
|
---|
| 113 | ArrayList<String> opList = new ArrayList<String>();
|
---|
| 114 | opList.add(ACCOUNT_SETTINGS);
|
---|
| 115 | opList.add(PERFORM_ACCOUNT_EDIT);
|
---|
| 116 | opList.add(PERFORM_RESET_PASSWORD);
|
---|
| 117 | opList.addAll(_adminOpList);
|
---|
| 118 | _userOpList = opList;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | //Other operations
|
---|
| 122 | protected static final String REGISTER = "Register";
|
---|
| 123 | protected static final String PERFORM_REGISTER = "PerformRegister";
|
---|
| 124 | protected static final String LOGIN = "Login";
|
---|
| 125 |
|
---|
[17452] | 126 | //the services on offer
|
---|
[24978] | 127 | protected static final String AUTHENTICATION_SERVICE = "Authentication";
|
---|
[25124] | 128 | protected static final String GET_USER_INFORMATION_SERVICE = "GetUserInformation";
|
---|
[28233] | 129 | protected static final String CHANGE_USER_EDIT_MODE_SERVICE = "ChangeUserEditMode";
|
---|
[14295] | 130 |
|
---|
[27075] | 131 | protected static boolean _derbyWrapperDoneForcedShutdown = false;
|
---|
[25258] | 132 |
|
---|
[25311] | 133 | protected String _recaptchaPrivateKey = null;
|
---|
| 134 | protected String _recaptchaPublicKey = null;
|
---|
| 135 |
|
---|
[17452] | 136 | /** constructor */
|
---|
| 137 | public Authentication()
|
---|
[24978] | 138 | {
|
---|
| 139 | }
|
---|
[14295] | 140 |
|
---|
[27075] | 141 | public void cleanUp()
|
---|
| 142 | {
|
---|
| 143 | super.cleanUp();
|
---|
[26912] | 144 |
|
---|
[27075] | 145 | if (!_derbyWrapperDoneForcedShutdown)
|
---|
| 146 | {
|
---|
[26912] | 147 |
|
---|
[27075] | 148 | // This boolean is used to ensure we always shutdown the derby server, even if it is never
|
---|
| 149 | // used by the Authentication server. This is because the Tomcat greenstone3.xml
|
---|
| 150 | // config file also specifies a connection to the database, which can result in the
|
---|
| 151 | // server being initialized when the servlet is first accessed. Note also,
|
---|
| 152 | // Authentication is a ServiceRack, meaning cleanUp() is called for each service
|
---|
| 153 | // supported, however we only need to shutdown the Derby server once. Again
|
---|
| 154 | // this boolean variable helps achieve this.
|
---|
[26912] | 155 |
|
---|
[27075] | 156 | logger.info("Authentication Service performing forced shutdown of Derby Server ...");
|
---|
[26912] | 157 |
|
---|
[27075] | 158 | DerbyWrapper.shutdownDatabaseServer();
|
---|
| 159 | _derbyWrapperDoneForcedShutdown = true;
|
---|
| 160 | }
|
---|
[26912] | 161 | }
|
---|
| 162 |
|
---|
[24978] | 163 | public boolean configure(Element info, Element extra_info)
|
---|
[17452] | 164 | {
|
---|
| 165 | logger.info("Configuring Authentication...");
|
---|
| 166 | this.config_info = info;
|
---|
[14295] | 167 |
|
---|
[17452] | 168 | // set up Authentication service info - for now just has name and type
|
---|
[24978] | 169 | Element authentication_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 170 | authentication_service.setAttribute(GSXML.TYPE_ATT, "authen");
|
---|
[17452] | 171 | authentication_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
|
---|
| 172 | this.short_service_info.appendChild(authentication_service);
|
---|
[14295] | 173 |
|
---|
[25124] | 174 | Element getUserInformation_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 175 | getUserInformation_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 176 | getUserInformation_service.setAttribute(GSXML.NAME_ATT, GET_USER_INFORMATION_SERVICE);
|
---|
| 177 | this.short_service_info.appendChild(getUserInformation_service);
|
---|
| 178 |
|
---|
[28233] | 179 | Element changeEditMode_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 180 | changeEditMode_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 181 | changeEditMode_service.setAttribute(GSXML.NAME_ATT, CHANGE_USER_EDIT_MODE_SERVICE);
|
---|
| 182 | this.short_service_info.appendChild(changeEditMode_service);
|
---|
| 183 |
|
---|
[28218] | 184 | DerbyWrapper.createDatabaseIfNeeded();
|
---|
[28233] | 185 |
|
---|
[25311] | 186 | NodeList recaptchaElems = info.getElementsByTagName("recaptcha");
|
---|
| 187 | for (int i = 0; i < recaptchaElems.getLength(); i++)
|
---|
| 188 | {
|
---|
| 189 | Element currentElem = (Element) recaptchaElems.item(i);
|
---|
| 190 | if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("public_key"))
|
---|
| 191 | {
|
---|
| 192 | if (currentElem.getAttribute(GSXML.VALUE_ATT) != null)
|
---|
| 193 | {
|
---|
| 194 | _recaptchaPublicKey = currentElem.getAttribute(GSXML.VALUE_ATT);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | else if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("private_key"))
|
---|
| 198 | {
|
---|
| 199 | if (currentElem.getAttribute(GSXML.VALUE_ATT) != null)
|
---|
| 200 | {
|
---|
| 201 | _recaptchaPrivateKey = currentElem.getAttribute(GSXML.VALUE_ATT);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[17452] | 206 | return true;
|
---|
| 207 | }
|
---|
[14295] | 208 |
|
---|
[24978] | 209 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
[17452] | 210 | {
|
---|
| 211 |
|
---|
[24978] | 212 | Element authen_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[17452] | 213 |
|
---|
[24978] | 214 | if (service_id.equals(AUTHENTICATION_SERVICE))
|
---|
| 215 | {
|
---|
| 216 | authen_service.setAttribute(GSXML.TYPE_ATT, "authen");
|
---|
[17452] | 217 | authen_service.setAttribute(GSXML.NAME_ATT, AUTHENTICATION_SERVICE);
|
---|
[24978] | 218 | }
|
---|
[25124] | 219 | else if (service_id.equals(GET_USER_INFORMATION_SERVICE))
|
---|
| 220 | {
|
---|
| 221 | authen_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 222 | authen_service.setAttribute(GSXML.NAME_ATT, GET_USER_INFORMATION_SERVICE);
|
---|
| 223 | }
|
---|
[28233] | 224 | else if (service_id.equals(CHANGE_USER_EDIT_MODE_SERVICE))
|
---|
| 225 | {
|
---|
| 226 | authen_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
| 227 | authen_service.setAttribute(GSXML.NAME_ATT, CHANGE_USER_EDIT_MODE_SERVICE);
|
---|
| 228 | }
|
---|
[24978] | 229 | else
|
---|
| 230 | {
|
---|
[17452] | 231 | return null;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[25124] | 234 | if (service_id.equals(AUTHENTICATION_SERVICE) && (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER)))
|
---|
[24978] | 235 | {
|
---|
| 236 | authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(service_id, lang)));
|
---|
[17452] | 237 | authen_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(service_id, lang)));
|
---|
| 238 | }
|
---|
[24978] | 239 | return authen_service;
|
---|
[14295] | 240 | }
|
---|
[17452] | 241 |
|
---|
[24978] | 242 | protected String getServiceName(String service_id, String lang)
|
---|
| 243 | {
|
---|
| 244 | return getTextString(service_id + ".name", lang);
|
---|
[14295] | 245 | }
|
---|
| 246 |
|
---|
[24978] | 247 | protected String getServiceSubmit(String service_id, String lang)
|
---|
| 248 | {
|
---|
| 249 | return getTextString(service_id + ".submit", lang);
|
---|
[17452] | 250 | }
|
---|
[14295] | 251 |
|
---|
[24978] | 252 | protected String getServiceDescription(String service_id, String lang)
|
---|
| 253 | {
|
---|
| 254 | return getTextString(service_id + ".description", lang);
|
---|
[17452] | 255 | }
|
---|
[14295] | 256 |
|
---|
[28233] | 257 | protected Element processChangeUserEditMode(Element request)
|
---|
| 258 | {
|
---|
| 259 | // Create a new (empty) result message
|
---|
| 260 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 261 |
|
---|
| 262 | result.setAttribute(GSXML.FROM_ATT, CHANGE_USER_EDIT_MODE_SERVICE);
|
---|
| 263 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 264 |
|
---|
| 265 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 266 | if (paramList == null)
|
---|
| 267 | {
|
---|
| 268 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
|
---|
| 269 | return result;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | HashMap<String, Serializable> params = GSXML.extractParams(paramList, true);
|
---|
| 273 |
|
---|
| 274 | String username = (String) params.get("username");
|
---|
| 275 | String editMode = (String) params.get("enabled");
|
---|
| 276 |
|
---|
| 277 | if (!editMode.toLowerCase().equals("true") && !editMode.toLowerCase().equals("false"))
|
---|
| 278 | {
|
---|
| 279 | editMode = "false";
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | DerbyWrapper dw = openDatabase();
|
---|
| 283 | dw.addUserData(username, "USER_EDIT_ENABLED", editMode);
|
---|
| 284 | dw.closeDatabase();
|
---|
| 285 |
|
---|
| 286 | return result;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[25124] | 289 | protected Element processGetUserInformation(Element request)
|
---|
| 290 | {
|
---|
| 291 | // Create a new (empty) result message
|
---|
| 292 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 293 |
|
---|
| 294 | result.setAttribute(GSXML.FROM_ATT, GET_USER_INFORMATION_SERVICE);
|
---|
| 295 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 296 |
|
---|
| 297 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 298 | if (paramList == null)
|
---|
| 299 | {
|
---|
[25258] | 300 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
|
---|
| 301 | return result;
|
---|
[25124] | 302 | }
|
---|
| 303 |
|
---|
[25635] | 304 | HashMap<String, Serializable> params = GSXML.extractParams(paramList, true);
|
---|
[25124] | 305 |
|
---|
| 306 | String username = (String) params.get("username");
|
---|
| 307 |
|
---|
| 308 | if (username == null)
|
---|
| 309 | {
|
---|
[25258] | 310 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_USERNAME_NOT_SPECIFIED));
|
---|
[25124] | 311 | return result;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[28210] | 314 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
[25124] | 315 |
|
---|
[28233] | 316 | UserQueryResult userQueryResult = derbyWrapper.findUser(username);
|
---|
| 317 | String editEnabled = derbyWrapper.getUserData(username, "USER_EDIT_ENABLED");
|
---|
[27075] | 318 |
|
---|
[28233] | 319 | Vector<UserTermInfo> terms = userQueryResult.getUserTerms();
|
---|
[25124] | 320 |
|
---|
[28233] | 321 | if (terms.size() == 0)
|
---|
| 322 | {
|
---|
| 323 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_REQUESTED_USER_NOT_FOUND));
|
---|
| 324 | return result;
|
---|
| 325 | }
|
---|
[25124] | 326 |
|
---|
[28233] | 327 | UserTermInfo userInfo = terms.get(0);
|
---|
| 328 | Element userInfoList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 329 | result.appendChild(userInfoList);
|
---|
[25124] | 330 |
|
---|
[28233] | 331 | Element usernameField = GSXML.createParameter(this.doc, "username", userInfo.username);
|
---|
| 332 | Element passwordField = GSXML.createParameter(this.doc, "password", userInfo.password);
|
---|
| 333 | Element groupsField = GSXML.createParameter(this.doc, "groups", userInfo.groups);
|
---|
| 334 | Element accountStatusField = GSXML.createParameter(this.doc, "accountstatus", userInfo.accountstatus);
|
---|
| 335 | Element commentField = GSXML.createParameter(this.doc, "comment", userInfo.comment);
|
---|
[25124] | 336 |
|
---|
[28233] | 337 | if (editEnabled != null)
|
---|
[25124] | 338 | {
|
---|
[28233] | 339 | Element editEnabledElem = GSXML.createParameter(this.doc, "editEnabled", editEnabled);
|
---|
| 340 | userInfoList.appendChild(editEnabledElem);
|
---|
[25124] | 341 | }
|
---|
| 342 |
|
---|
[28233] | 343 | userInfoList.appendChild(usernameField);
|
---|
| 344 | userInfoList.appendChild(passwordField);
|
---|
| 345 | userInfoList.appendChild(groupsField);
|
---|
| 346 | userInfoList.appendChild(accountStatusField);
|
---|
| 347 | userInfoList.appendChild(commentField);
|
---|
| 348 |
|
---|
[28210] | 349 | derbyWrapper.closeDatabase();
|
---|
[26912] | 350 |
|
---|
[25124] | 351 | return result;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[25258] | 354 | protected Element processAuthentication(Element request)
|
---|
[24978] | 355 | {
|
---|
[25258] | 356 | checkAdminUserExists();
|
---|
[14295] | 357 |
|
---|
[17452] | 358 | // Create a new (empty) result message
|
---|
| 359 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 360 | result.setAttribute(GSXML.FROM_ATT, AUTHENTICATION_SERVICE);
|
---|
| 361 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[14295] | 362 |
|
---|
[25258] | 363 | // Create an Authentication node put into the result
|
---|
| 364 | Element authenNode = this.doc.createElement(GSXML.AUTHEN_NODE_ELEM);
|
---|
| 365 | result.appendChild(authenNode);
|
---|
| 366 | result.appendChild(getCollectList(this.site_home + File.separatorChar + "collect"));
|
---|
| 367 |
|
---|
| 368 | // Create a service node added into the Authentication node
|
---|
| 369 | Element serviceNode = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 370 | authenNode.appendChild(serviceNode);
|
---|
| 371 |
|
---|
[17452] | 372 | // Get the parameters of the request
|
---|
[24978] | 373 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 374 | if (param_list == null)
|
---|
| 375 | {
|
---|
[25258] | 376 | serviceNode.setAttribute("operation", LOGIN);
|
---|
| 377 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
|
---|
[24978] | 378 | return result; // Return the empty result
|
---|
[17452] | 379 | }
|
---|
[25635] | 380 | HashMap<String, Serializable> paramMap = GSXML.extractParams(param_list, false);
|
---|
[25258] | 381 | String op = (String) paramMap.get("authpage");
|
---|
| 382 | serviceNode.setAttribute("operation", op);
|
---|
[14295] | 383 |
|
---|
[25258] | 384 | String username = null;
|
---|
| 385 | String groups = null;
|
---|
[14295] | 386 |
|
---|
[25258] | 387 | Element userInformation = (Element) GSXML.getChildByTagName(request, GSXML.USER_INFORMATION_ELEM);
|
---|
| 388 | if (userInformation == null && _userOpList.contains(op))
|
---|
| 389 | {
|
---|
| 390 | serviceNode.setAttribute("operation", LOGIN);
|
---|
| 391 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_NOT_LOGGED_IN));
|
---|
| 392 | return result;
|
---|
| 393 | }
|
---|
[14295] | 394 |
|
---|
[25258] | 395 | if (userInformation != null)
|
---|
| 396 | {
|
---|
| 397 | username = userInformation.getAttribute(GSXML.USERNAME_ATT);
|
---|
| 398 | groups = userInformation.getAttribute(GSXML.GROUPS_ATT);
|
---|
| 399 | }
|
---|
[17452] | 400 |
|
---|
[25258] | 401 | if (username == null && _userOpList.contains(op))
|
---|
| 402 | {
|
---|
| 403 | serviceNode.setAttribute("operation", LOGIN);
|
---|
| 404 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_NOT_LOGGED_IN));
|
---|
| 405 | return result;
|
---|
| 406 | }
|
---|
[24978] | 407 |
|
---|
[25258] | 408 | if (_adminOpList.contains(op) && (groups == null || !groups.matches(".*\\badministrator\\b.*")))
|
---|
[24978] | 409 | {
|
---|
[25258] | 410 | serviceNode.setAttribute("operation", LOGIN);
|
---|
| 411 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_ADMIN_NOT_LOGGED_IN));
|
---|
| 412 | return result;
|
---|
| 413 | }
|
---|
[17452] | 414 |
|
---|
[25258] | 415 | if (op.equals(LIST_USERS))
|
---|
| 416 | {
|
---|
| 417 | int error = addUserInformationToNode(null, serviceNode);
|
---|
| 418 | if (error != NO_ERROR)
|
---|
[24978] | 419 | {
|
---|
[25258] | 420 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[24978] | 421 | }
|
---|
[25258] | 422 | }
|
---|
| 423 | else if (op.equals(PERFORM_ADD))
|
---|
| 424 | {
|
---|
| 425 | String newUsername = (String) paramMap.get("username");
|
---|
| 426 | String newPassword = (String) paramMap.get("password");
|
---|
| 427 | String newGroups = (String) paramMap.get("groups");
|
---|
| 428 | String newStatus = (String) paramMap.get("status");
|
---|
| 429 | String newComment = (String) paramMap.get("comment");
|
---|
| 430 | String newEmail = (String) paramMap.get("email");
|
---|
[25722] | 431 |
|
---|
[25311] | 432 | //Check the given user name
|
---|
| 433 | int error;
|
---|
| 434 | if ((error = checkUsername(newUsername)) != NO_ERROR)
|
---|
| 435 | {
|
---|
| 436 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 437 | return result;
|
---|
| 438 | }
|
---|
[25258] | 439 |
|
---|
[25311] | 440 | //Check the given password
|
---|
| 441 | if ((error = checkPassword(newPassword)) != NO_ERROR)
|
---|
| 442 | {
|
---|
| 443 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 444 | return result;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | newPassword = hashPassword(newPassword);
|
---|
| 448 |
|
---|
| 449 | error = addUser(newUsername, newPassword, newGroups, newStatus, newComment, newEmail);
|
---|
[25258] | 450 | if (error != NO_ERROR)
|
---|
[24978] | 451 | {
|
---|
[25258] | 452 | serviceNode.setAttribute("operation", ADD_USER);
|
---|
| 453 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[24978] | 454 | }
|
---|
[25258] | 455 | else
|
---|
[24978] | 456 | {
|
---|
[25258] | 457 | addUserInformationToNode(null, serviceNode);
|
---|
| 458 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
[24978] | 459 | }
|
---|
[25258] | 460 | }
|
---|
| 461 | else if (op.equals(PERFORM_REGISTER))
|
---|
| 462 | {
|
---|
| 463 | String newUsername = (String) paramMap.get("username");
|
---|
| 464 | String newPassword = (String) paramMap.get("password");
|
---|
| 465 | String newEmail = (String) paramMap.get("email");
|
---|
[25722] | 466 |
|
---|
[25311] | 467 | //Check the given user name
|
---|
| 468 | int error;
|
---|
| 469 | if ((error = checkUsername(newUsername)) != NO_ERROR)
|
---|
[24978] | 470 | {
|
---|
[25311] | 471 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[25258] | 472 | return result;
|
---|
[24978] | 473 | }
|
---|
[25258] | 474 |
|
---|
[25311] | 475 | //Check the given password
|
---|
| 476 | if ((error = checkPassword(newPassword)) != NO_ERROR)
|
---|
[24978] | 477 | {
|
---|
[25311] | 478 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[25258] | 479 | return result;
|
---|
[24978] | 480 | }
|
---|
[25722] | 481 |
|
---|
[25311] | 482 | newPassword = hashPassword(newPassword);
|
---|
[25258] | 483 |
|
---|
[25725] | 484 | if (_recaptchaPrivateKey != null && _recaptchaPrivateKey.length() > 0)
|
---|
[25311] | 485 | {
|
---|
| 486 | ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
|
---|
| 487 | reCaptcha.setPrivateKey(_recaptchaPrivateKey);
|
---|
[25722] | 488 |
|
---|
[25725] | 489 | try
|
---|
[25311] | 490 | {
|
---|
[25734] | 491 | //If this line throws an exception then we'll assume the user has a firewall that is too restrictive
|
---|
| 492 | //(or that they're not connected to the Internet) to allow access to google services.
|
---|
| 493 | //In this situation we won't use the recaptcha test.
|
---|
[25725] | 494 | reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), "", "");
|
---|
[25722] | 495 |
|
---|
[25725] | 496 | String challenge = (String) paramMap.get("recaptcha_challenge_field");
|
---|
| 497 | String uResponse = (String) paramMap.get("recaptcha_response_field");
|
---|
[25722] | 498 |
|
---|
[25725] | 499 | if (challenge == null || uResponse == null)
|
---|
| 500 | {
|
---|
| 501 | serviceNode.setAttribute("operation", REGISTER);
|
---|
| 502 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_CAPTCHA_MISSING));
|
---|
| 503 | return result;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), challenge, uResponse);
|
---|
| 507 |
|
---|
| 508 | if (!reCaptchaResponse.isValid())
|
---|
| 509 | {
|
---|
| 510 | serviceNode.setAttribute("operation", REGISTER);
|
---|
| 511 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_CAPTCHA_DOES_NOT_MATCH));
|
---|
| 512 | return result;
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 | catch (Exception ex)
|
---|
[25311] | 516 | {
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | error = addUser(newUsername, newPassword, "", "true", "", newEmail);
|
---|
[25258] | 521 | if (error != NO_ERROR)
|
---|
[24978] | 522 | {
|
---|
[25258] | 523 | serviceNode.setAttribute("operation", REGISTER);
|
---|
| 524 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[24978] | 525 | }
|
---|
[25258] | 526 | }
|
---|
| 527 | else if (op.equals(PERFORM_EDIT))
|
---|
| 528 | {
|
---|
| 529 | String previousUsername = (String) paramMap.get("prevUsername");
|
---|
| 530 | String newUsername = (String) paramMap.get("newUsername");
|
---|
| 531 | String newPassword = (String) paramMap.get("password");
|
---|
| 532 | String newGroups = (String) paramMap.get("groups");
|
---|
| 533 | String newStatus = (String) paramMap.get("status");
|
---|
| 534 | String newComment = (String) paramMap.get("comment");
|
---|
[25852] | 535 | String newEmail = (String) paramMap.get("newEmail");
|
---|
[25258] | 536 |
|
---|
[25311] | 537 | //Check the given user name
|
---|
| 538 | int error;
|
---|
| 539 | if ((error = checkUsername(newUsername)) != NO_ERROR)
|
---|
| 540 | {
|
---|
| 541 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 542 | return result;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[25258] | 545 | if (newPassword == null)
|
---|
[24978] | 546 | {
|
---|
[25258] | 547 | newPassword = retrieveDataForUser(previousUsername, "password");
|
---|
[24978] | 548 | }
|
---|
[25311] | 549 | else
|
---|
| 550 | {
|
---|
| 551 | //Check the given password
|
---|
| 552 | if ((error = checkPassword(newPassword)) != NO_ERROR)
|
---|
| 553 | {
|
---|
| 554 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 555 | return result;
|
---|
| 556 | }
|
---|
[25722] | 557 |
|
---|
[25311] | 558 | newPassword = hashPassword(newPassword);
|
---|
| 559 | }
|
---|
[25722] | 560 |
|
---|
[25311] | 561 | error = removeUser(previousUsername);
|
---|
[25258] | 562 | if (error != NO_ERROR)
|
---|
[24978] | 563 | {
|
---|
[25258] | 564 | if (error == ERROR_USERNAME_NOT_SPECIFIED)
|
---|
| 565 | {
|
---|
| 566 | addUserInformationToNode(null, serviceNode);
|
---|
| 567 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
| 568 | }
|
---|
| 569 | else
|
---|
| 570 | {
|
---|
| 571 | serviceNode.setAttribute("operation", EDIT_USER);
|
---|
| 572 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 573 | }
|
---|
| 574 | return result;
|
---|
[24978] | 575 | }
|
---|
[25311] | 576 |
|
---|
[25258] | 577 | error = addUser(newUsername, newPassword, newGroups, newStatus, newComment, newEmail);
|
---|
| 578 | if (error != NO_ERROR)
|
---|
[24978] | 579 | {
|
---|
[25258] | 580 | serviceNode.setAttribute("operation", EDIT_USER);
|
---|
| 581 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[24978] | 582 | }
|
---|
[25258] | 583 | else
|
---|
[24978] | 584 | {
|
---|
[25258] | 585 | addUserInformationToNode(null, serviceNode);
|
---|
| 586 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
[24978] | 587 | }
|
---|
[25258] | 588 | }
|
---|
| 589 | else if (op.equals(PERFORM_ACCOUNT_EDIT))
|
---|
| 590 | {
|
---|
| 591 | String previousUsername = (String) paramMap.get("prevUsername");
|
---|
| 592 | String newUsername = (String) paramMap.get("newUsername");
|
---|
| 593 | String oldPassword = (String) paramMap.get("oldPassword");
|
---|
| 594 | String newPassword = (String) paramMap.get("newPassword");
|
---|
| 595 | String newEmail = (String) paramMap.get("newEmail");
|
---|
| 596 |
|
---|
| 597 | //Make sure the user name does not already exist
|
---|
| 598 | if (!previousUsername.equals(newUsername) && checkUserExists(newUsername))
|
---|
[24978] | 599 | {
|
---|
[25258] | 600 | addUserInformationToNode(previousUsername, serviceNode);
|
---|
| 601 | serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
|
---|
| 602 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_USER_ALREADY_EXISTS));
|
---|
| 603 | return result;
|
---|
[24978] | 604 | }
|
---|
[17452] | 605 |
|
---|
[25258] | 606 | String prevPassword = retrieveDataForUser(previousUsername, "password");
|
---|
[14295] | 607 |
|
---|
[25258] | 608 | if (newPassword != null)
|
---|
| 609 | {
|
---|
| 610 | oldPassword = hashPassword(oldPassword);
|
---|
[17452] | 611 |
|
---|
[25258] | 612 | if (oldPassword == null || !oldPassword.equals(prevPassword))
|
---|
[24978] | 613 | {
|
---|
[25258] | 614 | addUserInformationToNode(previousUsername, serviceNode);
|
---|
| 615 | serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
|
---|
[25352] | 616 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_INCORRECT_PASSWORD), "Incorrect Password");
|
---|
[17452] | 617 | return result;
|
---|
| 618 | }
|
---|
[25722] | 619 |
|
---|
[25311] | 620 | //Check the given password
|
---|
| 621 | int error;
|
---|
| 622 | if ((error = checkPassword(newPassword)) != NO_ERROR)
|
---|
| 623 | {
|
---|
| 624 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 625 | return result;
|
---|
| 626 | }
|
---|
[25722] | 627 |
|
---|
[25311] | 628 | newPassword = hashPassword(newPassword);
|
---|
[17452] | 629 | }
|
---|
[25258] | 630 | else
|
---|
| 631 | {
|
---|
| 632 | newPassword = prevPassword;
|
---|
| 633 | }
|
---|
[25722] | 634 |
|
---|
[25311] | 635 | //Check the given user name
|
---|
| 636 | int error;
|
---|
| 637 | if ((error = checkUsername(newUsername)) != NO_ERROR)
|
---|
| 638 | {
|
---|
| 639 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 640 | return result;
|
---|
| 641 | }
|
---|
[25722] | 642 |
|
---|
[25258] | 643 | String prevGroups = retrieveDataForUser(previousUsername, "groups");
|
---|
| 644 | String prevStatus = retrieveDataForUser(previousUsername, "status");
|
---|
| 645 | String prevComment = retrieveDataForUser(previousUsername, "comment");
|
---|
| 646 |
|
---|
[25311] | 647 | error = removeUser(previousUsername);
|
---|
[25258] | 648 | if (error != NO_ERROR)
|
---|
[24978] | 649 | {
|
---|
[25258] | 650 | if (error == ERROR_USERNAME_NOT_SPECIFIED)
|
---|
[24978] | 651 | {
|
---|
[25258] | 652 | addUserInformationToNode(null, serviceNode);
|
---|
| 653 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
[17452] | 654 | }
|
---|
[24978] | 655 | else
|
---|
| 656 | {
|
---|
[25258] | 657 | addUserInformationToNode(previousUsername, serviceNode);
|
---|
| 658 | serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
|
---|
| 659 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[17452] | 660 | }
|
---|
[25258] | 661 | return result;
|
---|
[14295] | 662 | }
|
---|
[17452] | 663 |
|
---|
[25258] | 664 | error = addUser(newUsername, newPassword, prevGroups, prevStatus, prevComment, newEmail);
|
---|
| 665 | if (error != NO_ERROR)
|
---|
[24978] | 666 | {
|
---|
[25258] | 667 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[14295] | 668 | }
|
---|
[25258] | 669 |
|
---|
| 670 | addUserInformationToNode(null, serviceNode);
|
---|
| 671 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
[14295] | 672 | }
|
---|
[26233] | 673 | else if (op.equals(PERFORM_RETRIEVE_PASSWORD))
|
---|
| 674 | {
|
---|
[26425] | 675 |
|
---|
[26233] | 676 | }
|
---|
| 677 | else if (op.equals(PERFORM_CHANGE_PASSWORD))
|
---|
| 678 | {
|
---|
[26272] | 679 | serviceNode.setAttribute("operation", PERFORM_CHANGE_PASSWORD);
|
---|
[26233] | 680 | String user_name = (String) paramMap.get("username");
|
---|
| 681 | String oldPassword = (String) paramMap.get("oldPassword");
|
---|
| 682 | String newPassword = (String) paramMap.get("newPassword");
|
---|
[26425] | 683 | if (user_name == null || oldPassword == null || newPassword == null)
|
---|
[26233] | 684 | {
|
---|
| 685 | GSXML.addError(this.doc, result, _errorMessageMap.get("missing compulsory parameters: username, oldPassword, or newPassword"));
|
---|
| 686 | return result;
|
---|
| 687 | }
|
---|
[26425] | 688 |
|
---|
| 689 | String prevPassword = retrieveDataForUser(user_name, "password");
|
---|
[26272] | 690 | if (!hashPassword(oldPassword).equals(prevPassword))
|
---|
[26233] | 691 | {
|
---|
| 692 | addUserInformationToNode(user_name, serviceNode);
|
---|
| 693 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_INCORRECT_PASSWORD), "Incorrect Password");
|
---|
| 694 | return result;
|
---|
| 695 | }
|
---|
[26425] | 696 |
|
---|
[26233] | 697 | //Check the given password
|
---|
| 698 | int error;
|
---|
| 699 | if ((error = checkPassword(newPassword)) != NO_ERROR)
|
---|
| 700 | {
|
---|
| 701 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 702 | return result;
|
---|
| 703 | }
|
---|
[26425] | 704 |
|
---|
[28210] | 705 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
[26272] | 706 | String chpa_groups = retrieveDataForUser(user_name, "groups");
|
---|
| 707 | String chpa_comment = "password_changed_by_user";
|
---|
[28210] | 708 | String info = derbyWrapper.modifyUserInfo(user_name, hashPassword(newPassword), chpa_groups, null, chpa_comment, null);
|
---|
| 709 | derbyWrapper.closeDatabase();
|
---|
[26425] | 710 | if (info != "succeed")
|
---|
| 711 | {//see DerbyWrapper.modifyUserInfo
|
---|
[26272] | 712 | GSXML.addError(this.doc, result, _errorMessageMap.get(info));
|
---|
[26233] | 713 | return result;
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
[25258] | 716 | else if (op.equals(EDIT_USER))
|
---|
[24978] | 717 | {
|
---|
[25258] | 718 | String editUsername = (String) paramMap.get("username");
|
---|
| 719 | int error = addUserInformationToNode(editUsername, serviceNode);
|
---|
| 720 | if (error != NO_ERROR)
|
---|
[24978] | 721 | {
|
---|
[25258] | 722 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
| 725 | else if (op.equals(ACCOUNT_SETTINGS))
|
---|
| 726 | {
|
---|
| 727 | String editUsername = (String) paramMap.get("username");
|
---|
[25311] | 728 |
|
---|
| 729 | if (editUsername == null)
|
---|
[25258] | 730 | {
|
---|
| 731 | serviceNode.setAttribute("operation", "");
|
---|
| 732 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_USERNAME_NOT_SPECIFIED));
|
---|
[17452] | 733 | return result;
|
---|
| 734 | }
|
---|
[25311] | 735 |
|
---|
| 736 | if (!editUsername.equals(username))
|
---|
[24978] | 737 | {
|
---|
[25258] | 738 | serviceNode.setAttribute("operation", LOGIN);
|
---|
| 739 | GSXML.addError(this.doc, result, _errorMessageMap.get(ERROR_NOT_AUTHORISED));
|
---|
[17452] | 740 | return result;
|
---|
| 741 | }
|
---|
[25258] | 742 | int error = addUserInformationToNode(editUsername, serviceNode);
|
---|
| 743 | if (error != NO_ERROR)
|
---|
[24978] | 744 | {
|
---|
[25258] | 745 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[17452] | 746 | }
|
---|
[14295] | 747 | }
|
---|
[25258] | 748 | else if (op.equals(PERFORM_RESET_PASSWORD))
|
---|
[24978] | 749 | {
|
---|
[25258] | 750 | String passwordResetUser = (String) paramMap.get("username");
|
---|
[25311] | 751 |
|
---|
[25258] | 752 | String newPassword = UUID.randomUUID().toString();
|
---|
| 753 | newPassword = newPassword.substring(0, newPassword.indexOf("-"));
|
---|
[25311] | 754 |
|
---|
[25258] | 755 | String email = retrieveDataForUser(passwordResetUser, "email");
|
---|
| 756 | String from = "[email protected]";
|
---|
| 757 | String host = request.getAttribute("remoteAddress");
|
---|
[25311] | 758 |
|
---|
[25270] | 759 | //TODO: FINISH THIS
|
---|
[25258] | 760 | }
|
---|
[25311] | 761 | else if (op.equals(REGISTER))
|
---|
| 762 | {
|
---|
[25725] | 763 | if (_recaptchaPrivateKey != null && _recaptchaPrivateKey.length() > 0)
|
---|
[25311] | 764 | {
|
---|
[25725] | 765 | try
|
---|
| 766 | {
|
---|
| 767 | ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
|
---|
| 768 | reCaptcha.setPrivateKey(_recaptchaPrivateKey);
|
---|
| 769 | reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), "", "");
|
---|
| 770 | }
|
---|
| 771 | catch (Exception ex)
|
---|
| 772 | {
|
---|
| 773 | return result;
|
---|
| 774 | }
|
---|
[25722] | 775 | }
|
---|
| 776 |
|
---|
| 777 | if (_recaptchaPublicKey != null && _recaptchaPrivateKey != null)
|
---|
| 778 | {
|
---|
[25311] | 779 | Element recaptchaElem = this.doc.createElement("recaptcha");
|
---|
| 780 | recaptchaElem.setAttribute("publicKey", _recaptchaPublicKey);
|
---|
| 781 | recaptchaElem.setAttribute("privateKey", _recaptchaPrivateKey);
|
---|
| 782 | result.appendChild(recaptchaElem);
|
---|
| 783 | }
|
---|
| 784 | }
|
---|
[25258] | 785 | else if (op.equals(PERFORM_DELETE_USER))
|
---|
| 786 | {
|
---|
| 787 | String usernameToDelete = (String) paramMap.get("username");
|
---|
| 788 | int error = removeUser(usernameToDelete);
|
---|
| 789 | if (error != NO_ERROR)
|
---|
[24978] | 790 | {
|
---|
[25258] | 791 | GSXML.addError(this.doc, result, _errorMessageMap.get(error));
|
---|
[17452] | 792 | }
|
---|
[25258] | 793 | addUserInformationToNode(null, serviceNode);
|
---|
| 794 | serviceNode.setAttribute("operation", LIST_USERS);
|
---|
| 795 | }
|
---|
[17452] | 796 |
|
---|
[25258] | 797 | return result;
|
---|
| 798 | }
|
---|
[25722] | 799 |
|
---|
[25311] | 800 | public int checkUsernameAndPassword(String username, String password)
|
---|
| 801 | {
|
---|
| 802 | int uResult = checkUsername(username);
|
---|
| 803 | int pResult = checkPassword(password);
|
---|
[25722] | 804 |
|
---|
[25311] | 805 | return (uResult != NO_ERROR ? uResult : (pResult != NO_ERROR ? pResult : NO_ERROR));
|
---|
| 806 | }
|
---|
[25722] | 807 |
|
---|
[25311] | 808 | public int checkUsername(String username)
|
---|
| 809 | {
|
---|
| 810 | //Check the given user name
|
---|
[25852] | 811 | if ((username == null) || (username.length() < USERNAME_MIN_LENGTH) || (username.length() > USERNAME_MAX_LENGTH) || (!(Pattern.matches("[a-zA-Z0-9//_//.]+", username))))
|
---|
[25311] | 812 | {
|
---|
| 813 | return ERROR_INVALID_USERNAME;
|
---|
| 814 | }
|
---|
| 815 | return NO_ERROR;
|
---|
| 816 | }
|
---|
[25722] | 817 |
|
---|
[25311] | 818 | public int checkPassword(String password)
|
---|
| 819 | {
|
---|
| 820 | //Check the given password
|
---|
[25852] | 821 | if (password == null)
|
---|
[25311] | 822 | {
|
---|
[25852] | 823 | return ERROR_PASSWORD_NOT_ENTERED;
|
---|
[25311] | 824 | }
|
---|
[25852] | 825 | else if (password.length() < PASSWORD_MIN_LENGTH)
|
---|
| 826 | {
|
---|
| 827 | return ERROR_PASSWORD_TOO_SHORT;
|
---|
| 828 | }
|
---|
| 829 | else if (password.length() > PASSWORD_MAX_LENGTH)
|
---|
| 830 | {
|
---|
| 831 | return ERROR_PASSWORD_TOO_LONG;
|
---|
| 832 | }
|
---|
| 833 | else if (!(Pattern.matches("[\\p{ASCII}]+", password)))
|
---|
| 834 | {
|
---|
| 835 | return ERROR_PASSWORD_USES_ILLEGAL_CHARACTERS;
|
---|
| 836 | }
|
---|
[25311] | 837 | return NO_ERROR;
|
---|
| 838 | }
|
---|
[17452] | 839 |
|
---|
[25258] | 840 | public static String hashPassword(String password)
|
---|
| 841 | {
|
---|
[26425] | 842 | return DigestUtils.sha1Hex(password);
|
---|
[25258] | 843 | }
|
---|
[14295] | 844 |
|
---|
[25722] | 845 | // This method can also be used for printing out the password in hex (in case
|
---|
| 846 | // the password used the UTF-8 Charset), or the hex values in any unicode string.
|
---|
| 847 | // From http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java
|
---|
| 848 | public static String toHex(String arg)
|
---|
| 849 | {
|
---|
| 850 | try
|
---|
| 851 | {
|
---|
| 852 | return String.format("%x", new BigInteger(arg.getBytes("US-ASCII"))); // set to same charset as used by hashPassword
|
---|
| 853 | }
|
---|
| 854 | catch (Exception e)
|
---|
| 855 | { // UnsupportedEncodingException
|
---|
| 856 | e.printStackTrace();
|
---|
| 857 | }
|
---|
| 858 | return "Unable to print";
|
---|
| 859 | }
|
---|
[25318] | 860 |
|
---|
[25258] | 861 | private void checkAdminUserExists()
|
---|
| 862 | {
|
---|
[28210] | 863 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
| 864 | UserQueryResult userQueryResult = derbyWrapper.findUser(null, null);
|
---|
| 865 | derbyWrapper.closeDatabase();
|
---|
[17452] | 866 |
|
---|
[25258] | 867 | if (userQueryResult != null)
|
---|
| 868 | {
|
---|
| 869 | Vector userInfo = userQueryResult.users;
|
---|
[17452] | 870 |
|
---|
[25258] | 871 | boolean adminFound = false;
|
---|
| 872 | for (int i = 0; i < userQueryResult.getSize(); i++)
|
---|
[24978] | 873 | {
|
---|
[25258] | 874 | if (((UserTermInfo) userInfo.get(i)).groups != null && ((UserTermInfo) userInfo.get(i)).groups.matches(".*\\badministrator\\b.*"))
|
---|
[24978] | 875 | {
|
---|
[25258] | 876 | adminFound = true;
|
---|
[17452] | 877 | }
|
---|
| 878 | }
|
---|
| 879 |
|
---|
[25258] | 880 | if (!adminFound)
|
---|
[24978] | 881 | {
|
---|
[25258] | 882 | addUser("admin", "admin", "administrator", "true", "Change the password for this account as soon as possible", "");
|
---|
[17452] | 883 | }
|
---|
[14295] | 884 | }
|
---|
[25258] | 885 | }
|
---|
| 886 |
|
---|
[28210] | 887 | private DerbyWrapper openDatabase()
|
---|
[25258] | 888 | {
|
---|
[28210] | 889 | // check the usersDb database, if it isn't existing, check the etc dir, create the etc dir if it isn't existing, then create the user database and add a "admin" user
|
---|
| 890 | String usersDB_dir = GlobalProperties.getGSDL3Home() + File.separatorChar + "etc" + File.separatorChar + "usersDB";
|
---|
| 891 | DerbyWrapper derbyWrapper = new DerbyWrapper(usersDB_dir);
|
---|
| 892 | return derbyWrapper;
|
---|
[25258] | 893 | }
|
---|
[14295] | 894 |
|
---|
[25258] | 895 | private int addUserInformationToNode(String username, Element serviceNode)
|
---|
[27075] | 896 | {
|
---|
[28210] | 897 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
| 898 | UserQueryResult userQueryResult = derbyWrapper.findUser(username, null);
|
---|
| 899 | derbyWrapper.closeDatabase();
|
---|
[17452] | 900 |
|
---|
[25258] | 901 | if (userQueryResult != null)
|
---|
| 902 | {
|
---|
| 903 | Element user_node = getUserNode(userQueryResult);
|
---|
| 904 | serviceNode.appendChild(user_node);
|
---|
| 905 | return NO_ERROR;
|
---|
| 906 | }
|
---|
[17452] | 907 |
|
---|
[25258] | 908 | return ERROR_COULD_NOT_GET_USER_INFO;
|
---|
| 909 | }
|
---|
[17452] | 910 |
|
---|
[25258] | 911 | private int removeUser(String username)
|
---|
| 912 | {
|
---|
| 913 | if (username == null)
|
---|
| 914 | {
|
---|
| 915 | return ERROR_USERNAME_NOT_SPECIFIED;
|
---|
| 916 | }
|
---|
[17452] | 917 |
|
---|
[28210] | 918 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
| 919 | boolean success = derbyWrapper.deleteUser(username);
|
---|
| 920 | derbyWrapper.closeDatabase();
|
---|
[25258] | 921 |
|
---|
| 922 | if (success)
|
---|
| 923 | {
|
---|
[27075] | 924 | return NO_ERROR;
|
---|
[25258] | 925 | }
|
---|
| 926 |
|
---|
| 927 | return ERROR_REMOVING_USER;
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | private int addUser(String newUsername, String newPassword, String newGroups, String newStatus, String newComment, String newEmail)
|
---|
| 931 | {
|
---|
| 932 | newGroups = newGroups.replaceAll(" ", "");
|
---|
| 933 |
|
---|
| 934 | //Check if the user already exists
|
---|
[28210] | 935 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
| 936 | UserQueryResult userQueryResult = derbyWrapper.findUser(newUsername, null);
|
---|
[26912] | 937 |
|
---|
[25258] | 938 | if (userQueryResult != null)
|
---|
| 939 | {
|
---|
[28210] | 940 | derbyWrapper.closeDatabase();
|
---|
[25258] | 941 | return ERROR_USER_ALREADY_EXISTS;
|
---|
| 942 | }
|
---|
| 943 | else
|
---|
| 944 | {
|
---|
[28210] | 945 | boolean success = derbyWrapper.addUser(newUsername, newPassword, newGroups, newStatus, newComment, newEmail);
|
---|
| 946 | derbyWrapper.closeDatabase();
|
---|
| 947 |
|
---|
[25258] | 948 | if (!success)
|
---|
[24978] | 949 | {
|
---|
[25258] | 950 | return ERROR_ADDING_USER;
|
---|
[17452] | 951 | }
|
---|
[25258] | 952 | }
|
---|
[26912] | 953 |
|
---|
[25258] | 954 | return NO_ERROR;
|
---|
| 955 | }
|
---|
[17452] | 956 |
|
---|
[25258] | 957 | private boolean checkUserExists(String username)
|
---|
| 958 | {
|
---|
[27075] | 959 | boolean check_status = false;
|
---|
[25258] | 960 |
|
---|
[28210] | 961 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
[25258] | 962 | try
|
---|
| 963 | {
|
---|
[28210] | 964 | UserQueryResult result = derbyWrapper.findUser(username);
|
---|
[25258] | 965 |
|
---|
| 966 | if (result != null)
|
---|
[24978] | 967 | {
|
---|
[26912] | 968 | check_status = true;
|
---|
[24978] | 969 | }
|
---|
[25258] | 970 |
|
---|
[14295] | 971 | }
|
---|
[25258] | 972 | catch (Exception ex)
|
---|
| 973 | {
|
---|
[27075] | 974 | // some error occurred accessing the database
|
---|
| 975 | ex.printStackTrace();
|
---|
[25258] | 976 | }
|
---|
[28210] | 977 | derbyWrapper.closeDatabase();
|
---|
[26912] | 978 |
|
---|
| 979 | return check_status;
|
---|
[25258] | 980 | }
|
---|
[17452] | 981 |
|
---|
[25258] | 982 | private String retrieveDataForUser(String username, String dataType)
|
---|
| 983 | {
|
---|
[27075] | 984 | openDatabase();
|
---|
[25258] | 985 |
|
---|
[26912] | 986 | String data = null;
|
---|
[25258] | 987 |
|
---|
| 988 | try
|
---|
| 989 | {
|
---|
[28210] | 990 | DerbyWrapper derbyWrapper = openDatabase();
|
---|
| 991 | UserQueryResult result = derbyWrapper.findUser(username);
|
---|
| 992 | derbyWrapper.closeDatabase();
|
---|
[25258] | 993 | Vector userInfo = result.users;
|
---|
| 994 |
|
---|
| 995 | for (int i = 0; i < result.getSize(); i++)
|
---|
[24978] | 996 | {
|
---|
[25258] | 997 | if (dataType.equals("password"))
|
---|
[24978] | 998 | {
|
---|
[26912] | 999 | data = ((UserTermInfo) userInfo.get(i)).password;
|
---|
| 1000 | break;
|
---|
[24978] | 1001 | }
|
---|
[25258] | 1002 | else if (dataType.equals("groups"))
|
---|
[24978] | 1003 | {
|
---|
[26912] | 1004 | data = ((UserTermInfo) userInfo.get(i)).groups;
|
---|
| 1005 | break;
|
---|
[17452] | 1006 | }
|
---|
[25258] | 1007 | else if (dataType.equals("status"))
|
---|
| 1008 | {
|
---|
[26912] | 1009 | data = ((UserTermInfo) userInfo.get(i)).accountstatus;
|
---|
| 1010 | break;
|
---|
[25258] | 1011 | }
|
---|
| 1012 | else if (dataType.equals("comment"))
|
---|
| 1013 | {
|
---|
[26912] | 1014 | data = ((UserTermInfo) userInfo.get(i)).comment;
|
---|
| 1015 | break;
|
---|
[25258] | 1016 | }
|
---|
| 1017 | else if (dataType.equals("email"))
|
---|
| 1018 | {
|
---|
[26912] | 1019 | data = ((UserTermInfo) userInfo.get(i)).email;
|
---|
| 1020 | break;
|
---|
[25258] | 1021 | }
|
---|
[24978] | 1022 | }
|
---|
[14295] | 1023 | }
|
---|
[25258] | 1024 | catch (Exception ex)
|
---|
| 1025 | {
|
---|
| 1026 | ex.printStackTrace();
|
---|
| 1027 | }
|
---|
[14295] | 1028 |
|
---|
[26912] | 1029 | return data;
|
---|
[14295] | 1030 | }
|
---|
| 1031 |
|
---|
[24978] | 1032 | private Element getUserNode(UserQueryResult userQueryResult)
|
---|
| 1033 | {
|
---|
| 1034 | Element user_list_node = this.doc.createElement(GSXML.USER_NODE_ELEM + "List");
|
---|
[14295] | 1035 |
|
---|
[25258] | 1036 | Vector userInfo = userQueryResult.users;
|
---|
[14295] | 1037 |
|
---|
[24978] | 1038 | for (int i = 0; i < userQueryResult.getSize(); i++)
|
---|
| 1039 | {
|
---|
| 1040 | Element user_node = this.doc.createElement(GSXML.USER_NODE_ELEM);
|
---|
[25258] | 1041 | String username = ((UserTermInfo) userInfo.get(i)).username;
|
---|
| 1042 | String groups = ((UserTermInfo) userInfo.get(i)).groups;
|
---|
| 1043 | String accountstatus = ((UserTermInfo) userInfo.get(i)).accountstatus;
|
---|
| 1044 | String comment = ((UserTermInfo) userInfo.get(i)).comment;
|
---|
| 1045 | String email = ((UserTermInfo) userInfo.get(i)).email;
|
---|
| 1046 | user_node.setAttribute("username", username);
|
---|
| 1047 | user_node.setAttribute("groups", groups);
|
---|
| 1048 | user_node.setAttribute("status", accountstatus);
|
---|
| 1049 | user_node.setAttribute("comment", comment);
|
---|
| 1050 | user_node.setAttribute("email", email);
|
---|
[14295] | 1051 |
|
---|
[17452] | 1052 | user_list_node.appendChild(user_node);
|
---|
[14295] | 1053 | }
|
---|
[24978] | 1054 | return user_list_node;
|
---|
[14295] | 1055 | }
|
---|
| 1056 |
|
---|
[24978] | 1057 | private Element getCollectList(String collect)
|
---|
| 1058 | {
|
---|
| 1059 | Element collect_list_node = this.doc.createElement(GSXML.COLLECTION_ELEM + "List");
|
---|
| 1060 | File[] collect_dir = (new File(collect)).listFiles();
|
---|
| 1061 | if (collect_dir != null && collect_dir.length > 0)
|
---|
| 1062 | {
|
---|
| 1063 | for (int i = 0; i < collect_dir.length; i++)
|
---|
| 1064 | {
|
---|
| 1065 | if (collect_dir[i].isDirectory() && (!collect_dir[i].getName().startsWith(".svn")))
|
---|
| 1066 | {
|
---|
[17452] | 1067 | Element collect_node = this.doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
[24978] | 1068 | collect_node.setAttribute("name", collect_dir[i].getName());
|
---|
[17452] | 1069 | collect_list_node.appendChild(collect_node);
|
---|
| 1070 | }
|
---|
| 1071 | }
|
---|
[14402] | 1072 | }
|
---|
[17452] | 1073 | return collect_list_node;
|
---|
[14402] | 1074 | }
|
---|
[26206] | 1075 |
|
---|
[26425] | 1076 | // main() method - calls hashPassword() on any String argument, printing this to stdout
|
---|
| 1077 | // This main() is invoked by gliserver.pl perl code to encrypt passwords identically to Java code.
|
---|
| 1078 | public static void main(String[] args)
|
---|
| 1079 | {
|
---|
| 1080 | if (args.length < 1)
|
---|
| 1081 | {
|
---|
| 1082 | System.err.println("Usage: Authentication <string to encrypt>");
|
---|
| 1083 | System.exit(-1);
|
---|
| 1084 | }
|
---|
| 1085 | // just hash the first argument
|
---|
| 1086 | String hash = Authentication.hashPassword(args[0]);
|
---|
| 1087 | System.out.println(hash);
|
---|
[26206] | 1088 | }
|
---|
[14295] | 1089 | }
|
---|