RoleUserInterceptor.java

1
package edu.ucsb.cs156.organic.interceptors;
2
3
import javax.servlet.http.HttpServletRequest;
4
import javax.servlet.http.HttpServletResponse;
5
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.stereotype.Component;
8
import org.springframework.web.servlet.HandlerInterceptor;
9
import org.springframework.web.servlet.ModelAndView;
10
11
import edu.ucsb.cs156.organic.repositories.UserRepository;
12
import lombok.extern.slf4j.Slf4j;
13
14
import org.springframework.security.core.authority.SimpleGrantedAuthority;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.security.core.Authentication;
18
import org.springframework.security.core.GrantedAuthority;
19
import org.springframework.security.core.context.SecurityContext;
20
import org.springframework.security.core.context.SecurityContextHolder;
21
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
22
import org.springframework.security.oauth2.core.user.OAuth2User;
23
24
import java.util.Optional;
25
import java.util.HashSet;
26
import java.util.Set;
27
import java.util.Collection;
28
import edu.ucsb.cs156.organic.entities.User;
29
30
31
@Component
32
@Slf4j
33
public class RoleUserInterceptor implements HandlerInterceptor {
34
35
   @Autowired
36
   UserRepository userRepository;
37
38
   @Override
39
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
40
        // Update user's security context on server each time the user makes HTTP request to the backend
41
        // If user has admin status in database we will keep ROLE_ADMIN in security context
42
        // Otherwise interceptor will remove ROLE_ADMIN before the incoming request is processed by backend API
43
        SecurityContext securityContext = SecurityContextHolder.getContext();
44
        Authentication authentication = securityContext.getAuthentication();
45 1 1. preHandle : negated conditional → KILLED
        if (authentication instanceof OAuth2AuthenticationToken ) {
46
            OAuth2User oAuthUser = ((OAuth2AuthenticationToken) authentication).getPrincipal();
47
            Integer githubID = oAuthUser.getAttribute("id");
48
            log.info("interceptor githubId={}", githubID);
49
            Optional<User> optionalUser = userRepository.findByGithubId(githubID);
50 1 1. preHandle : negated conditional → KILLED
            if (optionalUser.isPresent()){
51
                User user = optionalUser.get();
52
53
                Set<GrantedAuthority> newAuthorities = new HashSet<>();
54
                Collection<? extends GrantedAuthority> currentAuthorities = authentication.getAuthorities();
55
                currentAuthorities.stream()
56 2 1. lambda$preHandle$0 : negated conditional → KILLED
2. lambda$preHandle$0 : replaced boolean return with true for edu/ucsb/cs156/organic/interceptors/RoleUserInterceptor::lambda$preHandle$0 → KILLED
                .filter(authority -> !authority.getAuthority().equals("ROLE_ADMIN")
57 1 1. lambda$preHandle$0 : negated conditional → KILLED
                && !authority.getAuthority().equals("ROLE_INSTRUCTOR"))
58 1 1. preHandle : removed call to java/util/stream/Stream::forEach → KILLED
                .forEach(authority -> {
59
                    newAuthorities.add(authority);
60
                });
61
62 1 1. preHandle : negated conditional → KILLED
                if (user.isAdmin() ){
63
                    newAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
64
                }
65
66 1 1. preHandle : negated conditional → KILLED
                if (user.isInstructor()){
67
                    newAuthorities.add(new SimpleGrantedAuthority("ROLE_INSTRUCTOR"));
68
                }
69
70
                Authentication newAuth = new OAuth2AuthenticationToken(oAuthUser, newAuthorities,(((OAuth2AuthenticationToken)authentication).getAuthorizedClientRegistrationId()));
71 1 1. preHandle : removed call to org/springframework/security/core/context/SecurityContext::setAuthentication → KILLED
                SecurityContextHolder.getContext().setAuthentication(newAuth);
72
            }
73
        }
74
75 1 1. preHandle : replaced boolean return with false for edu/ucsb/cs156/organic/interceptors/RoleUserInterceptor::preHandle → KILLED
      return true;
76
   }
77
78
}

Mutations

45

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.controllers.SystemInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SystemInfoControllerTests]/[method:systemInfo__logged_out()]
negated conditional → KILLED

50

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:user_not_present_in_db_and_no_role_update_by_interceptor()]
negated conditional → KILLED

56

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
negated conditional → KILLED

2.2
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
replaced boolean return with true for edu/ucsb/cs156/organic/interceptors/RoleUserInterceptor::lambda$preHandle$0 → KILLED

57

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
negated conditional → KILLED

58

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
removed call to java/util/stream/Stream::forEach → KILLED

62

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
negated conditional → KILLED

66

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
negated conditional → KILLED

71

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.interceptors.RoleUserInterceptorTests]/[method:interceptor_removes_admin_role_when_admin_field_in_db_is_false()]
removed call to org/springframework/security/core/context/SecurityContext::setAuthentication → KILLED

75

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.organic.controllers.SystemInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.organic.controllers.SystemInfoControllerTests]/[method:systemInfo__logged_out()]
replaced boolean return with false for edu/ucsb/cs156/organic/interceptors/RoleUserInterceptor::preHandle → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3