Search in sources :

Example 1 with IntLogger

use of com.blackducksoftware.integration.log.IntLogger in project hub-alert by blackducksoftware.

the class LoginHandler method authenticateUser.

public ResponseEntity<String> authenticateUser(final HttpServletRequest request, final HttpServletResponse response, final LoginRestModel loginRestModel) {
    final IntLogger logger = new PrintStreamIntLogger(System.out, LogLevel.INFO);
    try {
        if (loginActions.authenticateUser(loginRestModel, logger)) {
            final CsrfToken token = csrfTokenRepository.generateToken(request);
            csrfTokenRepository.saveToken(token, request, response);
            response.setHeader(token.getHeaderName(), token.getToken());
            return createResponse(HttpStatus.OK, "{\"message\":\"Success\"}");
        }
        return createResponse(HttpStatus.UNAUTHORIZED, "User not administrator");
    } catch (final IntegrationRestException e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.valueOf(e.getHttpStatusCode()), e.getHttpStatusMessage() + " : " + e.getMessage());
    } catch (final AlertFieldException e) {
        logger.error(e.getMessage(), e);
        final ResponseBodyBuilder responseBodyBuilder = new ResponseBodyBuilder(0L, e.getMessage());
        responseBodyBuilder.putErrors(e.getFieldErrors());
        final String responseBody = responseBodyBuilder.build();
        return createResponse(HttpStatus.BAD_REQUEST, responseBody);
    } catch (final Exception e) {
        logger.error(e.getMessage(), e);
        return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) IntLogger(com.blackducksoftware.integration.log.IntLogger) PrintStreamIntLogger(com.blackducksoftware.integration.log.PrintStreamIntLogger) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) CsrfToken(org.springframework.security.web.csrf.CsrfToken) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) IntegrationRestException(com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 2 with IntLogger

use of com.blackducksoftware.integration.log.IntLogger 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)

Aggregations

AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)2 IntLogger (com.blackducksoftware.integration.log.IntLogger)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)1 LoginRestModel (com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel)1 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)1 RoleAssignmentView (com.blackducksoftware.integration.hub.api.generated.view.RoleAssignmentView)1 HubServerConfig (com.blackducksoftware.integration.hub.configuration.HubServerConfig)1 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)1 RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)1 IntegrationRestException (com.blackducksoftware.integration.hub.rest.exception.IntegrationRestException)1 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)1 UserGroupService (com.blackducksoftware.integration.hub.service.UserGroupService)1 PrintStreamIntLogger (com.blackducksoftware.integration.log.PrintStreamIntLogger)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 Arrays (java.util.Arrays)1