Search in sources :

Example 1 with ProviderNotFoundException

use of org.springframework.security.authentication.ProviderNotFoundException in project ArTEMiS by ls1intum.

the class JiraAuthenticationProvider method getOrCreateUser.

/**
 * Gets or creates the user object for an JIRA user.
 *
 * @param authentication
 * @param skipPasswordCheck     Skip checking the password
 * @return
 */
@Override
public User getOrCreateUser(Authentication authentication, Boolean skipPasswordCheck) {
    String username = authentication.getName().toLowerCase();
    String password = authentication.getCredentials().toString();
    HttpEntity<Principal> entity = new HttpEntity<>(!skipPasswordCheck ? HeaderUtil.createAuthorization(username, password) : HeaderUtil.createAuthorization(JIRA_USER, JIRA_PASSWORD));
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> authenticationResponse = null;
    try {
        authenticationResponse = restTemplate.exchange(JIRA_URL + "/rest/api/2/user?username=" + username + "&expand=groups", HttpMethod.GET, entity, Map.class);
    } catch (HttpStatusCodeException e) {
        if (e.getStatusCode().value() == 401) {
            throw new BadCredentialsException("Wrong credentials");
        } else if (e.getStatusCode().is5xxServerError()) {
            throw new ProviderNotFoundException("Could not authenticate via JIRA");
        }
    }
    if (authenticationResponse != null) {
        Map content = authenticationResponse.getBody();
        User user = userRepository.findOneByLogin((String) content.get("name")).orElseGet(() -> {
            return userService.createUser((String) content.get("name"), "", (String) content.get("displayName"), "", (String) content.get("emailAddress"), null, "en");
        });
        user.setGroups(getGroupStrings((ArrayList) ((Map) content.get("groups")).get("items")));
        user.setAuthorities(buildAuthoritiesFromGroups(getGroupStrings((ArrayList) ((Map) content.get("groups")).get("items"))));
        userRepository.save(user);
        if (!user.getActivated()) {
            userService.activateRegistration(user.getActivationKey());
        }
        Optional<User> matchingUser = userService.getUserWithAuthoritiesByLogin(username);
        if (matchingUser.isPresent()) {
            return matchingUser.get();
        } else {
            throw new UsernameNotFoundException("User " + username + " was not found in the " + "database");
        }
    } else {
        throw new InternalAuthenticationServiceException("JIRA Authentication failed for user " + username);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(de.tum.in.www1.artemis.domain.User) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ProviderNotFoundException(org.springframework.security.authentication.ProviderNotFoundException) RestTemplate(org.springframework.web.client.RestTemplate) Principal(java.security.Principal)

Aggregations

User (de.tum.in.www1.artemis.domain.User)1 Principal (java.security.Principal)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 InternalAuthenticationServiceException (org.springframework.security.authentication.InternalAuthenticationServiceException)1 ProviderNotFoundException (org.springframework.security.authentication.ProviderNotFoundException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)1 RestTemplate (org.springframework.web.client.RestTemplate)1