ApiKeyInterceptor.java

1
package edu.ucsb.cs156.frontiers.interceptors;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import edu.ucsb.cs156.frontiers.config.AllowApiKeyAccess;
5
import jakarta.servlet.http.HttpServletRequest;
6
import jakarta.servlet.http.HttpServletResponse;
7
import java.io.IOException;
8
import java.util.Map;
9
import lombok.extern.slf4j.Slf4j;
10
import org.springframework.boot.web.servlet.error.ErrorController;
11
import org.springframework.core.annotation.AnnotatedElementUtils;
12
import org.springframework.security.core.Authentication;
13
import org.springframework.security.core.context.SecurityContextHolder;
14
import org.springframework.web.method.HandlerMethod;
15
import org.springframework.web.servlet.HandlerInterceptor;
16
17
@Slf4j
18
public class ApiKeyInterceptor implements HandlerInterceptor {
19
20
  private ObjectMapper mapper;
21
22
  public ApiKeyInterceptor(ObjectMapper mapper) {
23
    this.mapper = mapper;
24
  }
25
26
  @Override
27
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
28
      throws IOException {
29
30
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
31 3 1. lambda$preHandle$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::lambda$preHandle$0 → KILLED
2. lambda$preHandle$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::lambda$preHandle$0 → KILLED
3. preHandle : negated conditional → KILLED
    if (auth.getAuthorities().stream().noneMatch(a -> a.getAuthority().equals("ROLE_API_KEY"))) {
32 1 1. preHandle : replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED
      return true;
33
    }
34 1 1. preHandle : negated conditional → KILLED
    if (!(handler instanceof HandlerMethod commonHandler)) {
35 1 1. preHandle : replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED
      return true;
36 1 1. preHandle : negated conditional → KILLED
    } else if (ErrorController.class.isAssignableFrom(commonHandler.getBeanType())) {
37 1 1. preHandle : replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED
      return true;
38
    }
39
    if (AnnotatedElementUtils.findAllMergedAnnotations(
40
            commonHandler.getMethod(), AllowApiKeyAccess.class)
41 1 1. preHandle : negated conditional → KILLED
        .isEmpty()) {
42
      Map<String, String> responseMessage =
43
          Map.of(
44
              "message",
45
              "Attempting to gain access to an API endpoint that does not allow an API key");
46 1 1. preHandle : removed call to jakarta/servlet/http/HttpServletResponse::setStatus → KILLED
      response.setStatus(HttpServletResponse.SC_FORBIDDEN);
47 1 1. preHandle : removed call to java/io/PrintWriter::write → KILLED
      response.getWriter().write(mapper.writeValueAsString(responseMessage));
48 1 1. preHandle : replaced boolean return with true for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED
      return false;
49
    }
50 1 1. preHandle : replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED
    return true;
51
  }
52
}

Mutations

31

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.frontiers.controllers.WebhookControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.WebhookControllerTests]/[method:webhookWithoutSignature_returnsUnauthorized()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::lambda$preHandle$0 → KILLED

2.2
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_blocks_api_key_request_to_unannotated_endpoint()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::lambda$preHandle$0 → KILLED

3.3
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.controllers.WebhookControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.WebhookControllerTests]/[method:webhookWithoutSignature_returnsUnauthorized()]
negated conditional → KILLED

32

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.InstructorsControllerTests]/[method:logged_in_users_cannot_get()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED

34

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

35

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_ignores_static_file()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED

36

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

37

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_ignores_error_controller()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED

41

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

46

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_blocks_api_key_request_to_unannotated_endpoint()]
removed call to jakarta/servlet/http/HttpServletResponse::setStatus → KILLED

47

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_blocks_api_key_request_to_unannotated_endpoint()]
removed call to java/io/PrintWriter::write → KILLED

48

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_blocks_api_key_request_to_unannotated_endpoint()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED

50

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.interceptors.ApiKeyInterceptorTests]/[method:handler_allows_api_access_to_annotated_endpoint()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/interceptors/ApiKeyInterceptor::preHandle → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support