|
|
@ -21,11 +21,9 @@ package org.wso2.carbon.device.mgt.jaxrs.util;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreManager;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreManager;
|
|
|
|
|
|
|
|
|
|
|
@ -44,6 +42,7 @@ public class CredentialManagementResponseBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Builds the response to change the password of a user
|
|
|
|
* Builds the response to change the password of a user
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param username - Username of the user.
|
|
|
|
* @param username - Username of the user.
|
|
|
|
* @param credentials - User credentials
|
|
|
|
* @param credentials - User credentials
|
|
|
|
* @return Response Object
|
|
|
|
* @return Response Object
|
|
|
@ -52,17 +51,17 @@ public class CredentialManagementResponseBuilder {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
|
|
|
|
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
|
|
|
|
if (!userStoreManager.isExistingUser(username)) {
|
|
|
|
if (!userStoreManager.isExistingUser(username)) {
|
|
|
|
throw new InputValidationException(
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username "
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username '"
|
|
|
|
+ username).build());
|
|
|
|
+ username + "'").build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RequestValidationUtil.validateCredentials(credentials);
|
|
|
|
RequestValidationUtil.validateCredentials(credentials);
|
|
|
|
|
|
|
|
|
|
|
|
if (!validateCredential(credentials.getNewPassword())) {
|
|
|
|
if (!validateCredential(credentials.getNewPassword())) {
|
|
|
|
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
|
|
|
|
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
|
|
|
|
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
|
|
|
|
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
|
|
|
|
throw new InputValidationException(
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userStoreManager.updateCredential(username, credentials.getNewPassword(),
|
|
|
|
userStoreManager.updateCredential(username, credentials.getNewPassword(),
|
|
|
|
credentials.getOldPassword());
|
|
|
|
credentials.getOldPassword());
|
|
|
@ -70,19 +69,20 @@ public class CredentialManagementResponseBuilder {
|
|
|
|
username + " was successfully changed.").build();
|
|
|
|
username + " was successfully changed.").build();
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(e.getMessage()).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(e.getMessage()).build()).build();
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
String msg = "Could not change the password of the user: " + username +
|
|
|
|
String msg = "Could not change the password of the user: " + username +
|
|
|
|
". The Character Encoding is not supported.";
|
|
|
|
". The Character Encoding is not supported.";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Builds the response to reset the password of a user
|
|
|
|
* Builds the response to reset the password of a user
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param username - Username of the user.
|
|
|
|
* @param username - Username of the user.
|
|
|
|
* @param credentials - User credentials
|
|
|
|
* @param credentials - User credentials
|
|
|
|
* @return Response Object
|
|
|
|
* @return Response Object
|
|
|
@ -91,35 +91,35 @@ public class CredentialManagementResponseBuilder {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
|
|
|
|
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
|
|
|
|
if (!userStoreManager.isExistingUser(username)) {
|
|
|
|
if (!userStoreManager.isExistingUser(username)) {
|
|
|
|
throw new InputValidationException(
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("No user found with the username "
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("No user found with the username "
|
|
|
|
+ username).build());
|
|
|
|
+ username).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (credentials == null || credentials.getNewPassword() == null) {
|
|
|
|
if (credentials == null || credentials.getNewPassword() == null) {
|
|
|
|
throw new InputValidationException(
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Password cannot be empty."
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage("Password cannot be empty."
|
|
|
|
+ username).build());
|
|
|
|
+ username).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validateCredential(credentials.getNewPassword())) {
|
|
|
|
if (!validateCredential(credentials.getNewPassword())) {
|
|
|
|
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
|
|
|
|
String errorMsg = DeviceMgtAPIUtils.getRealmService().getBootstrapRealmConfiguration()
|
|
|
|
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
|
|
|
|
.getUserStoreProperty(PASSWORD_VALIDATION_ERROR_MSG_TAG);
|
|
|
|
throw new InputValidationException(
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMsg).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMsg).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword());
|
|
|
|
userStoreManager.updateCredentialByAdmin(username, credentials.getNewPassword());
|
|
|
|
return Response.status(Response.Status.OK).entity("UserImpl password by username: " +
|
|
|
|
return Response.status(Response.Status.OK).entity("UserImpl password by username: " +
|
|
|
|
username + " was successfully changed.").build();
|
|
|
|
username + " was successfully changed.").build();
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
String msg = "ErrorResponse occurred while updating the credentials of user '" + username + "'";
|
|
|
|
String msg = "Error occurred while updating the credentials of user '" + username + "'";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
String msg = "Could not change the password of the user: " + username +
|
|
|
|
String msg = "Could not change the password of the user: " + username +
|
|
|
|
". The Character Encoding is not supported.";
|
|
|
|
". The Character Encoding is not supported.";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
throw new UnexpectedServerErrorException(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|