| 37150 |
amit |
1 |
package com.spice.profitmandi.web.interceptor;
|
|
|
2 |
|
|
|
3 |
import org.apache.logging.log4j.LogManager;
|
|
|
4 |
import org.apache.logging.log4j.Logger;
|
|
|
5 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
6 |
import org.springframework.stereotype.Component;
|
|
|
7 |
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
8 |
import org.springframework.web.servlet.ModelAndView;
|
|
|
9 |
|
|
|
10 |
import com.spice.profitmandi.common.model.Response;
|
|
|
11 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
12 |
import com.spice.profitmandi.dao.entity.fofo.ApiClient;
|
|
|
13 |
import com.spice.profitmandi.service.external.ApiClientAuthService;
|
|
|
14 |
|
|
|
15 |
import javax.servlet.http.HttpServletRequest;
|
|
|
16 |
import javax.servlet.http.HttpServletResponse;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Authenticates third-party requests on /external/** via the X-Api-Key
|
|
|
20 |
* header. On success the resolved {@link ApiClient} is exposed as the
|
|
|
21 |
* "apiClient"/"apiClientId" request attributes.
|
|
|
22 |
*/
|
|
|
23 |
@Component
|
|
|
24 |
public class ApiKeyInterceptor implements HandlerInterceptor {
|
|
|
25 |
|
|
|
26 |
public static final String HEADER_API_KEY = "X-Api-Key";
|
|
|
27 |
public static final String ATTR_API_CLIENT = "apiClient";
|
|
|
28 |
public static final String ATTR_API_CLIENT_ID = "apiClientId";
|
|
|
29 |
|
|
|
30 |
private static final Logger LOGGER = LogManager.getLogger(ApiKeyInterceptor.class);
|
|
|
31 |
|
|
|
32 |
@Autowired
|
|
|
33 |
private ResponseSender<?> responseSender;
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
private ApiClientAuthService apiClientAuthService;
|
|
|
37 |
|
|
|
38 |
@Override
|
|
|
39 |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
40 |
throws Exception {
|
|
|
41 |
String rawKey = request.getHeader(HEADER_API_KEY);
|
|
|
42 |
if (rawKey == null || rawKey.trim().isEmpty()) {
|
|
|
43 |
responseSender.writeUnauthorized(request, response,
|
|
|
44 |
new Response("apiKey", HEADER_API_KEY, "EXT_401", "Missing " + HEADER_API_KEY + " header"));
|
|
|
45 |
return false;
|
|
|
46 |
}
|
|
|
47 |
ApiClient apiClient = apiClientAuthService.resolveByRawKey(rawKey);
|
|
|
48 |
if (apiClient == null) {
|
|
|
49 |
// do not distinguish unknown vs inactive keys
|
|
|
50 |
LOGGER.warn("Rejected external API request with invalid key on {}", request.getRequestURI());
|
|
|
51 |
responseSender.writeUnauthorized(request, response,
|
|
|
52 |
new Response("apiKey", HEADER_API_KEY, "EXT_401", "Invalid or inactive API key"));
|
|
|
53 |
return false;
|
|
|
54 |
}
|
|
|
55 |
request.setAttribute(ATTR_API_CLIENT, apiClient);
|
|
|
56 |
request.setAttribute(ATTR_API_CLIENT_ID, apiClient.getId());
|
|
|
57 |
return true;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
@Override
|
|
|
61 |
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
62 |
ModelAndView modelAndView) throws Exception {
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
@Override
|
|
|
66 |
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
67 |
Exception ex) throws Exception {
|
|
|
68 |
}
|
|
|
69 |
}
|