Open Kilda Java Documentation
ExtraAuthInterceptor.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.utils;
17 
18 import static org.openkilda.messaging.Utils.EXTRA_AUTH;
19 
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.core.annotation.AnnotationUtils;
23 import org.springframework.web.method.HandlerMethod;
24 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
25 
26 import java.util.concurrent.TimeUnit;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 
33 public class ExtraAuthInterceptor extends HandlerInterceptorAdapter {
34 
35  private static final Logger LOGGER = LoggerFactory.getLogger(ExtraAuthInterceptor.class);
36 
37  @Override
38  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
39  throws Exception {
40  if (!supports(handler)) {
41  return true;
42  }
43 
44  HandlerMethod handlerMethod = (HandlerMethod) handler;
45  ExtraAuthRequired annotation = handlerMethod.getMethodAnnotation(ExtraAuthRequired.class);
46  if (annotation == null) {
47  Class<?> handlerClass = handlerMethod.getMethod().getDeclaringClass();
48  annotation = AnnotationUtils.findAnnotation(handlerClass, ExtraAuthRequired.class);
49  if (annotation == null) {
50  return true;
51  }
52  }
53 
54  long currentAuth = System.currentTimeMillis();
55 
56  final String extraAuthHeader = request.getHeader(EXTRA_AUTH);
57  long extraAuth;
58  try {
59  extraAuth = Long.parseLong(extraAuthHeader);
60  } catch (NumberFormatException ex) {
61  LOGGER.warn("Invalid {} header: {}", EXTRA_AUTH, extraAuthHeader);
62 
63  response.getWriter().write("Invalid Auth: " + currentAuth);
64  response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
65  return false;
66  }
67 
68  if (Math.abs(currentAuth - extraAuth) > TimeUnit.SECONDS.toMillis(120)) {
69  /*
70  * The request needs to be within 120 seconds of the system clock.
71  */
72  response.getWriter().write("Invalid Auth: " + currentAuth);
73  response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
74  return false;
75  }
76 
77  return true;
78  }
79 
80  private boolean supports(Object handler) {
81  return handler instanceof HandlerMethod;
82  }
83 }
static final String EXTRA_AUTH
Definition: Utils.java:47
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)