Skip to content

Commit

Permalink
Merge pull request #4085 from Coduz/fix-credentialNotEditableFromConsole
Browse files Browse the repository at this point in the history
🐛 [Console GWT] Fixed Credential Edit dialog does not letting update the selected Credential
  • Loading branch information
Coduz authored Jul 22, 2024
2 parents 70cbdd3 + 7694759 commit 6659768
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public class CredentialEditDialog extends CredentialAddDialog {

public CredentialEditDialog(GwtSession currentSession, GwtCredential selectedCredential, String selectedUserId, String selectedUserName) {
super(currentSession, selectedUserId, selectedUserName);

this.selectedCredential = selectedCredential;
}

@Override
public void submit() {
// TODO read enabled and expire date
selectedCredential.setCredentialKey(password.getValue());
selectedCredential.setExpirationDate(expirationDate.getValue());
selectedCredential.setCredentialStatus(credentialStatus.getValue().getValue().toString());
selectedCredential.setOptlock(optlock.getValue().intValue());

GWT_CREDENTIAL_SERVICE.update(xsrfToken, selectedCredential, new AsyncCallback<GwtCredential>() {

@Override
Expand Down Expand Up @@ -99,16 +99,26 @@ private void loadCredential() {
@Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
password.setVisible(false);
confirmPassword.setVisible(false);
passwordTooltip.setVisible(false);
credentialFormPanel.remove(credentialType);
credentialTypeLabel.setVisible(true);

password.hide();
password.disable();

confirmPassword.hide();
confirmPassword.disable();

passwordTooltip.hide();

credentialType.hide();
credentialType.disable();

credentialTypeLabel.show();
credentialTypeLabel.setValue(selectedCredential.getCredentialType());

if (selectedCredential.getLockoutReset() != null && selectedCredential.getLockoutReset().after(new Date())) {
lockedUntil.setText(MSGS.dialogEditLockedUntil(DateUtils.formatDateTime(selectedCredential.getLockoutReset())));
credentialFormPanel.add(lockedUntil);
}

DialogUtils.resizeDialog(this, 400, 230);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.eclipse.kapua.KapuaException;
Expand All @@ -32,8 +31,6 @@
import org.eclipse.kapua.app.console.module.authentication.shared.util.GwtKapuaAuthenticationModelConverter;
import org.eclipse.kapua.app.console.module.authentication.shared.util.KapuaGwtAuthenticationModelConverter;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.commons.util.CommonsValidationRegex;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.authentication.AuthenticationService;
Expand All @@ -45,9 +42,7 @@
import org.eclipse.kapua.service.authentication.credential.CredentialListResult;
import org.eclipse.kapua.service.authentication.credential.CredentialQuery;
import org.eclipse.kapua.service.authentication.credential.CredentialService;
import org.eclipse.kapua.service.authentication.exception.PasswordLengthException;
import org.eclipse.kapua.service.authentication.shiro.utils.AuthenticationUtils;
import org.eclipse.kapua.service.authentication.shiro.utils.CryptAlgorithm;
import org.eclipse.kapua.service.authentication.user.PasswordChangeRequest;
import org.eclipse.kapua.service.authentication.user.PasswordResetRequest;
import org.eclipse.kapua.service.authentication.user.UserCredentialsFactory;
Expand Down Expand Up @@ -177,72 +172,23 @@ public GwtCredential update(GwtXSRFToken gwtXsrfToken, GwtCredential gwtCredenti
// Checking XSRF token
checkXSRFToken(gwtXsrfToken);

fixPasswordValidationBypass(gwtCredential);
// Do update
GwtCredential gwtCredentialUpdated = null;
try {
KapuaId scopeId = GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getScopeId());
KapuaId credentialId = GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getId());
Credential credential = GwtKapuaAuthenticationModelConverter.convertCredential(gwtCredential);

// Update
if (StringUtils.isNotEmpty(StringUtils.strip(gwtCredential.getCredentialKey()))) {
String encryptedPass = AUTHENTICATION_UTILS.cryptCredential(CryptAlgorithm.BCRYPT, gwtCredential.getCredentialKey());
gwtCredential.setCredentialKey(encryptedPass);
} else {
Credential currentCredential = CREDENTIAL_SERVICE.find(scopeId, credentialId);
gwtCredential.setCredentialKey(currentCredential.getCredentialKey());
}
Credential credentialUpdated = CREDENTIAL_SERVICE.update(GwtKapuaAuthenticationModelConverter.convertCredential(gwtCredential));
User user = USER_SERVICE.find(credentialUpdated.getScopeId(), credentialUpdated.getUserId());
Credential credentialUpdated = CREDENTIAL_SERVICE.update(credential);

// Convert
gwtCredentialUpdated = KapuaGwtAuthenticationModelConverter.convertCredential(credentialUpdated, user);

} catch (Throwable t) {
throw KapuaExceptionHandler.buildExceptionFromError(t);
}
// Return result
return gwtCredentialUpdated;
}


/**
* Validate password, this check should be moved to
* CredentialServiceImpl. There, this check already exist,
* but it's useless since it's done on the already encrypted password
*
* @param gwtCredential
* @throws GwtKapuaException
*/
private void fixPasswordValidationBypass(GwtCredential gwtCredential)
throws GwtKapuaException {
Credential credential =
GwtKapuaAuthenticationModelConverter.convertCredential(
gwtCredential);
try {
// Validate Password length
int minPasswordLength = CREDENTIAL_SERVICE.getMinimumPasswordLength(
credential.getScopeId());
if (gwtCredential.getCredentialKey().length() < minPasswordLength ||
gwtCredential.getCredentialKey().length() >
SYSTEM_MAXIMUM_PASSWORD_LENGTH) {
throw new PasswordLengthException(
minPasswordLength, SYSTEM_MAXIMUM_PASSWORD_LENGTH);
}

// Validate Password regex
ArgumentValidator.match(
gwtCredential.getCredentialKey(),
CommonsValidationRegex.PASSWORD_REGEXP,
"credential.credentialKey"
);
User user = USER_SERVICE.find(credentialUpdated.getScopeId(), credentialUpdated.getUserId());

// Return result
return KapuaGwtAuthenticationModelConverter.convertCredential(credentialUpdated, user);
} catch (Throwable t) {
throw KapuaExceptionHandler.buildExceptionFromError(t);
}
}


@Override
public void changePassword(GwtXSRFToken gwtXsrfToken, String oldPassword, final String newPassword, String mfaCode, String stringUserId, String stringScopeId) throws GwtKapuaException {
String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ public static CredentialCreator convertCredentialCreator(GwtCredentialCreator gw
public static Credential convertCredential(GwtCredential gwtCredential) {

// Convert scopeId
KapuaId scopeId = GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getScopeId());
Credential credential = CREDENTIAL_FACTORY.newEntity(scopeId);
Credential credential = CREDENTIAL_FACTORY.newEntity(GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getScopeId()));

GwtKapuaCommonsModelConverter.convertUpdatableEntity(gwtCredential, credential);
if (gwtCredential.getId() != null && !gwtCredential.getId().trim().isEmpty()) {
credential.setId(GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getId()));
}

credential.setId(GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getId()));
credential.setUserId(GwtKapuaCommonsModelConverter.convertKapuaId(gwtCredential.getUserId()));
credential.setCredentialType(convertCredentialType(gwtCredential.getCredentialTypeEnum()));
credential.setCredentialKey(gwtCredential.getCredentialKey());
Expand All @@ -138,6 +137,7 @@ public static Credential convertCredential(GwtCredential gwtCredential) {
credential.setFirstLoginFailure(gwtCredential.getFirstLoginFailure());
credential.setLoginFailuresReset(gwtCredential.getLoginFailuresReset());
credential.setLockoutReset(gwtCredential.getLockoutReset());

// Return converted
return credential;
}
Expand Down

0 comments on commit 6659768

Please sign in to comment.