2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.server.filters;
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
22 import javax.servlet.*;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletRequestWrapper;
25 import javax.servlet.http.HttpServletResponse;
26 import java.io.IOException;
27 import java.security.Principal;
28 import java.util.Base64;
30 public class ActionAuthenticationFilter implements Filter {
32 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
33 private boolean runningOnLocal = true;
36 public void destroy() {
37 // TODO Auto-generated method stub
42 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
43 throws IOException, ServletException {
46 HttpServletRequest httpRequest = (HttpServletRequest) arg0;
47 String authorizationHeader = httpRequest.getHeader("Authorization");
48 if (authorizationHeader != null && !authorizationHeader.isEmpty()) {
51 String base64Credentials =
52 httpRequest.getHeader("Authorization").replace("Basic", "").trim();
53 String decodedCredentials = new String(Base64.getDecoder().decode(base64Credentials));
54 username = decodedCredentials.substring(0, decodedCredentials.indexOf(":"));
55 } catch (Exception exception) {
56 log.error("Failed to decode credentials", exception);
57 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
60 if (username.startsWith("AUTH")) {
61 HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(httpRequest) {
63 public java.lang.String getRemoteUser() {
64 return getUserPrincipal().getName();
68 public Principal getUserPrincipal() {
69 return () -> username.substring(0, username.indexOf("-"));
73 public boolean isUserInRole(String role) {
75 ActionLibraryPrivilege requiredPrivilege =
76 ActionLibraryPrivilege.getPrivilege(httpRequest.getMethod());
77 ActionLibraryPrivilege userPrivilege = ActionLibraryPrivilege
78 .valueOf(username.substring(username.indexOf("-") + 1).toUpperCase());
79 return userPrivilege.ordinal() >= requiredPrivilege.ordinal();
80 } catch (Exception exception) {
81 log.error("Failed to validate UserInRole", exception);
86 arg2.doFilter(servletRequest, arg1);
88 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
91 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_UNAUTHORIZED);
94 //call super doFilter of cadi authentication filter
100 private void setResponseStatus(HttpServletResponse response, int status) {
101 response.setStatus(status);
105 public void init(FilterConfig arg0) throws ServletException {