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());
}
}
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;
}
Aggregations