/** * Builds the response to reset the password of a user * * @param username - Username of the user. * @param credentials - User credentials * @return Response Object */ public static Response buildResetPasswordResponse( String username, PasswordResetWrapper credentials) { try { UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { return Response.status(Response.Status.BAD_REQUEST) .entity( new ErrorResponse.ErrorResponseBuilder() .setMessage("No user found with the username " + username) .build()) .build(); } if (credentials == null || credentials.getNewPassword() == null) { return Response.status(Response.Status.BAD_REQUEST) .entity( new ErrorResponse.ErrorResponseBuilder() .setMessage("Password cannot be empty." + username) .build()) .build(); } if (!validateCredential(credentials.getNewPassword())) { String errorMsg = DeviceMgtAPIUtils.getRealmService() .getBootstrapRealmConfiguration() .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); return Response.status(Response.Status.BAD_REQUEST) .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()) .build(); } userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword()); return Response.status(Response.Status.OK) .entity("UserImpl password by username: "******" was successfully changed.") .build(); } catch (UserStoreException e) { String msg = "Error occurred while updating the credentials of user '" + username + "'"; log.error(msg, e); return Response.serverError() .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()) .build(); } catch (UnsupportedEncodingException e) { String msg = "Could not change the password of the user: "******". The Character Encoding is not supported."; log.error(msg, e); return Response.serverError() .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()) .build(); } }
/** * Builds the response to change the password of a user * * @param credentials - User credentials * @return Response Object */ public static Response buildChangePasswordResponse(OldPasswordResetWrapper credentials) { String username = ""; try { RequestValidationUtil.validateCredentials(credentials); if (!validateCredential(credentials.getNewPassword())) { String errorMsg = DeviceMgtAPIUtils.getRealmService() .getBootstrapRealmConfiguration() .getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG); return Response.status(Response.Status.BAD_REQUEST) .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()) .build(); } UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); // this is the user who initiates the request username = CarbonContext.getThreadLocalCarbonContext().getUsername(); userStoreManager.updateCredential( username, credentials.getNewPassword(), credentials.getOldPassword()); return Response.status(Response.Status.OK) .entity("UserImpl password by username: "******" was successfully changed.") .build(); } catch (UserStoreException e) { log.error(e.getMessage(), e); return Response.serverError() .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()) .build(); } catch (UnsupportedEncodingException e) { String msg = "Could not change the password of the user: "******". The Character Encoding is not supported."; log.error(msg, e); return Response.serverError() .entity(new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()) .build(); } }
private static boolean validateCredential(String password) throws UserStoreException, UnsupportedEncodingException { String passwordValidationRegex = DeviceMgtAPIUtils.getRealmService() .getBootstrapRealmConfiguration() .getUserStoreProperty(PASSWORD_VALIDATION_REGEX_TAG); if (passwordValidationRegex != null) { Pattern pattern = Pattern.compile(passwordValidationRegex); if (pattern.matcher(password).matches()) { return true; } } return false; }