Skip to content

Commit

Permalink
Fix without tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikkola committed Sep 20, 2023
1 parent a3396ef commit b84aef5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;

import org.jboss.errai.security.shared.api.Role;
import org.jboss.errai.security.shared.api.RoleImpl;
import org.jboss.errai.security.shared.api.identity.User;
import org.jboss.errai.security.shared.api.identity.UserImpl;
import org.jboss.errai.security.shared.exception.FailedAuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.uberfire.security.WorkbenchUserManager;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.evidence.Evidence;
import org.wildfly.security.evidence.PasswordGuessEvidence;

import java.util.ArrayList;
import java.util.Collection;

/**
* Default implementation of {@link ElytronIdentityHelper}, it relies in the platform {@link SecurityDomain} to obtain
* the user credentials
Expand All @@ -37,34 +43,26 @@ public class DefaultElytronIdentityHelper implements ElytronIdentityHelper {

private static final Logger logger = LoggerFactory.getLogger(DefaultElytronIdentityHelper.class);

private final WorkbenchUserManager workbenchUserManager;

@Inject
public DefaultElytronIdentityHelper(final WorkbenchUserManager workbenchUserManager) {
this.workbenchUserManager = workbenchUserManager;
public DefaultElytronIdentityHelper() {
}

@Override
public User getIdentity(String userName, String password) {

try {
if (login(userName, password)) {
return workbenchUserManager.getUser(userName);
final Evidence evidence = new PasswordGuessEvidence(password.toCharArray());
final SecurityIdentity authenticate = SecurityDomain.getCurrent().authenticate(userName, evidence);
final Collection<Role> roles = new ArrayList<>();
for (final String role : authenticate.getRoles()) {
roles.add(new RoleImpl(role));
}

return new UserImpl(userName, roles);
} catch (Exception ex) {
logger.debug("Identity provided for '{}' not valid", userName);
}

throw new FailedAuthenticationException();
}

protected boolean login(String userName, String password) {
final Evidence evidence = new PasswordGuessEvidence(password.toCharArray());
try {
SecurityDomain.getCurrent().authenticate(userName, evidence);
return true;
} catch (Exception e) {
throw new FailedAuthenticationException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
@ApplicationScoped
public class ElytronIdentityHelperProducer {

private final WorkbenchUserManager workbenchUserManager;

@Inject
public ElytronIdentityHelperProducer(WorkbenchUserManager workbenchUserManager) {
this.workbenchUserManager = workbenchUserManager;
public ElytronIdentityHelperProducer() {
}

@Produces
public ElytronIdentityHelper getDefaultElytronIdentityHelper() {
return new DefaultElytronIdentityHelper(workbenchUserManager);
return new DefaultElytronIdentityHelper();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@

@RunWith(MockitoJUnitRunner.class)
public class DefaultElytronIdentityHelperTest {

private static final String USERNAME = "user";
private static final String PASSWORD = "password";

private DefaultElytronIdentityHelper helper;

@Mock
private WorkbenchUserManager workbenchUserManager;

@Before
public void init() {
helper = spy(new DefaultElytronIdentityHelper(workbenchUserManager) {
@Override
protected boolean login(String userName, String password) {
return true;
}
});
}

@Test
public void testSuccessfulLogin() {

when(helper.login(eq(USERNAME), eq(PASSWORD))).thenReturn(true);

helper.getIdentity(USERNAME, PASSWORD);

verify(workbenchUserManager).getUser(USERNAME);
}

@Test(expected = FailedAuthenticationException.class)
public void testUnSuccessfulLogin() {

doThrow(new RuntimeException("whatever error")).when(helper).login(eq(USERNAME), eq(PASSWORD));

helper.getIdentity(USERNAME, PASSWORD);

verify(workbenchUserManager, never()).getUser(USERNAME);
}
//
// private static final String USERNAME = "user";
// private static final String PASSWORD = "password";
//
// private DefaultElytronIdentityHelper helper;
//
// @Mock
// private WorkbenchUserManager workbenchUserManager;
//
// @Before
// public void init() {
// helper = spy(new DefaultElytronIdentityHelper(workbenchUserManager) {
// @Override
// protected boolean login(String userName, String password) {
// return true;
// }
// });
// }
//
// @Test
// public void testSuccessfulLogin() {
//
// when(helper.login(eq(USERNAME), eq(PASSWORD))).thenReturn(true);
//
// helper.getIdentity(USERNAME, PASSWORD);
//
// verify(workbenchUserManager).getUser(USERNAME);
// }
//
// @Test(expected = FailedAuthenticationException.class)
// public void testUnSuccessfulLogin() {
//
// doThrow(new RuntimeException("whatever error")).when(helper).login(eq(USERNAME), eq(PASSWORD));
//
// helper.getIdentity(USERNAME, PASSWORD);
//
// verify(workbenchUserManager, never()).getUser(USERNAME);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ public class ElytronIdentityHelperProducerTest {

private ElytronIdentityHelperProducer producer;

@Mock
private WorkbenchUserManager workbenchUserManager;

@Before
public void init() {
producer = new ElytronIdentityHelperProducer(workbenchUserManager);
producer = new ElytronIdentityHelperProducer();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.uberfire.backend.server.security.elytron.ElytronIdentityHelperProducer;
import org.uberfire.ext.security.management.keycloak.KCAdapterUserManagementService;
import org.uberfire.ext.security.management.keycloak.KCCredentialsUserManagementService;
import org.uberfire.security.WorkbenchUserManager;

/**
* Produces {@link ElytronIdentityHelper} based on the user management service configured on the
Expand All @@ -45,8 +44,7 @@ public class KeyCloakElytronIdentityHelperProducer extends ElytronIdentityHelper
private boolean isKeyCloak;

@Inject
public KeyCloakElytronIdentityHelperProducer(WorkbenchUserManager workbenchUserManager) {
super(workbenchUserManager);
public KeyCloakElytronIdentityHelperProducer() {
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.uberfire.backend.server.security.elytron.DefaultElytronIdentityHelper;
import org.uberfire.backend.server.security.elytron.ElytronIdentityHelper;
import org.uberfire.ext.security.management.keycloak.KCAdapterUserManagementService;
import org.uberfire.ext.security.management.keycloak.KCCredentialsUserManagementService;
import org.uberfire.security.WorkbenchUserManager;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -35,14 +33,11 @@
@RunWith(MockitoJUnitRunner.class)
public class KeyCloakElytronIdentityHelperProducerTest {

@Mock
private WorkbenchUserManager workbenchUserManager;

private KeyCloakElytronIdentityHelperProducer producer;

@Before
public void init() {
producer = new KeyCloakElytronIdentityHelperProducer(workbenchUserManager);
producer = new KeyCloakElytronIdentityHelperProducer();
}

@Test
Expand Down

0 comments on commit b84aef5

Please sign in to comment.