Search in sources :

Example 1 with LoginRestModel

use of com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel 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;
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Arrays(java.util.Arrays) IntLogger(com.blackducksoftware.integration.log.IntLogger) FieldEnum(com.blackducksoftware.integration.validator.FieldEnum) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory) Map(java.util.Map) GlobalProperties(com.blackducksoftware.integration.hub.alert.config.GlobalProperties) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) UserGroupService(com.blackducksoftware.integration.hub.service.UserGroupService) RoleAssignmentView(com.blackducksoftware.integration.hub.api.generated.view.RoleAssignmentView) ValidationResults(com.blackducksoftware.integration.validator.ValidationResults) Set(java.util.Set) AbstractValidator(com.blackducksoftware.integration.validator.AbstractValidator) List(java.util.List) Component(org.springframework.stereotype.Component) LoginRestModel(com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) Entry(java.util.Map.Entry) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) ValidationResult(com.blackducksoftware.integration.validator.ValidationResult) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HubServerConfig(com.blackducksoftware.integration.hub.configuration.HubServerConfig) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with LoginRestModel

use of com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel 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);
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) LoginRestModel(com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel) MockLoginRestModel(com.blackducksoftware.integration.hub.alert.mock.model.MockLoginRestModel) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) GlobalProperties(com.blackducksoftware.integration.hub.alert.config.GlobalProperties) Slf4jIntLogger(com.blackducksoftware.integration.log.Slf4jIntLogger) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) Test(org.junit.Test) HubConnectionTest(com.blackducksoftware.integration.test.annotation.HubConnectionTest)

Aggregations

GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)2 LoginRestModel (com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel)2 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)2 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 TestGlobalProperties (com.blackducksoftware.integration.hub.alert.TestGlobalProperties)1 AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)1 MockLoginRestModel (com.blackducksoftware.integration.hub.alert.mock.model.MockLoginRestModel)1 RoleAssignmentView (com.blackducksoftware.integration.hub.api.generated.view.RoleAssignmentView)1 HubServerConfig (com.blackducksoftware.integration.hub.configuration.HubServerConfig)1 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)1 UserGroupService (com.blackducksoftware.integration.hub.service.UserGroupService)1 IntLogger (com.blackducksoftware.integration.log.IntLogger)1 Slf4jIntLogger (com.blackducksoftware.integration.log.Slf4jIntLogger)1 HubConnectionTest (com.blackducksoftware.integration.test.annotation.HubConnectionTest)1 AbstractValidator (com.blackducksoftware.integration.validator.AbstractValidator)1 FieldEnum (com.blackducksoftware.integration.validator.FieldEnum)1 ValidationResult (com.blackducksoftware.integration.validator.ValidationResult)1 ValidationResults (com.blackducksoftware.integration.validator.ValidationResults)1 ArrayList (java.util.ArrayList)1