use of com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder in project hub-alert by blackducksoftware.
the class LoginActions method authenticateUser.
public boolean authenticateUser(final LoginRestModel loginRestModel, final IntLogger logger) throws IntegrationException {
final HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
serverConfigBuilder.setLogger(logger);
serverConfigBuilder.setHubUrl(globalProperties.getHubUrl());
serverConfigBuilder.setTimeout(HubServerConfigBuilder.DEFAULT_TIMEOUT_SECONDS);
if (globalProperties.getHubTrustCertificate() != null) {
serverConfigBuilder.setAlwaysTrustServerCertificate(globalProperties.getHubTrustCertificate());
}
serverConfigBuilder.setProxyHost(globalProperties.getHubProxyHost());
serverConfigBuilder.setProxyPort(globalProperties.getHubProxyPort());
serverConfigBuilder.setProxyUsername(globalProperties.getHubProxyUsername());
serverConfigBuilder.setProxyPassword(globalProperties.getHubProxyPassword());
serverConfigBuilder.setPassword(loginRestModel.getHubPassword());
serverConfigBuilder.setUsername(loginRestModel.getHubUsername());
try {
validateHubConfiguration(serverConfigBuilder);
final RestConnection restConnection = createRestConnection(serverConfigBuilder);
restConnection.connect();
logger.info("Connected");
final boolean isValidLoginUser = isUserRoleValid(loginRestModel.getHubUsername(), restConnection);
if (isValidLoginUser) {
final Authentication authentication = new UsernamePasswordAuthenticationToken(loginRestModel.getHubUsername(), loginRestModel.getHubPassword(), Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN")));
SecurityContextHolder.getContext().setAuthentication(authentication);
return authentication.isAuthenticated();
}
} catch (final AlertFieldException afex) {
logger.error("Error establishing connection", afex);
final Map<String, String> fieldErrorMap = afex.getFieldErrors();
fieldErrorMap.keySet().forEach(key -> {
final String value = fieldErrorMap.get(key);
logger.error(String.format("Field Error %s - %s", key, value));
});
logger.info("User not authenticated");
return false;
} catch (final IntegrationException ex) {
logger.error("Error establishing connection", ex);
logger.info("User not authenticated");
return false;
}
logger.info("User role not authenticated");
return false;
}
use of com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder in project hub-alert by blackducksoftware.
the class LoginActionsTestIT method testIsUserValidFailIT.
@Test
public void testIsUserValidFailIT() throws IntegrationException, IOException {
final LoginRestModel loginRestModel = mockLoginRestModel.createRestModel();
final GlobalProperties globalProperties = new TestGlobalProperties();
final HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
serverConfigBuilder.setLogger(new Slf4jIntLogger(logger));
serverConfigBuilder.setHubUrl(globalProperties.getHubUrl());
serverConfigBuilder.setAlwaysTrustServerCertificate(globalProperties.getHubTrustCertificate());
serverConfigBuilder.setTimeout(globalProperties.getHubTimeout());
serverConfigBuilder.setPassword(loginRestModel.getHubPassword());
serverConfigBuilder.setUsername(loginRestModel.getHubUsername());
final LoginActions loginActions = new LoginActions(globalProperties);
final RestConnection restConnection = loginActions.createRestConnection(serverConfigBuilder);
final boolean roleValid = loginActions.isUserRoleValid("broken", restConnection);
assertFalse(roleValid);
}
Aggregations