2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.server.filters;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletRequestWrapper;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.IOException;
36 import java.security.Principal;
37 import java.util.Base64;
39 public class ActionAuthenticationFilter implements Filter {
41 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
42 private boolean runningOnLocal = true;
45 public void destroy() {
46 // TODO Auto-generated method stub
51 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
52 throws IOException, ServletException {
55 HttpServletRequest httpRequest = (HttpServletRequest) arg0;
56 String authorizationHeader = httpRequest.getHeader("Authorization");
57 if (authorizationHeader != null && !authorizationHeader.isEmpty()) {
60 String base64Credentials =
61 httpRequest.getHeader("Authorization").replace("Basic", "").trim();
62 String decodedCredentials = new String(Base64.getDecoder().decode(base64Credentials));
63 username = decodedCredentials.substring(0, decodedCredentials.indexOf(":"));
64 } catch (Exception exception) {
65 log.debug("",exception);
66 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
69 if (username.startsWith("AUTH")) {
70 HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(httpRequest) {
72 public java.lang.String getRemoteUser() {
73 return getUserPrincipal().getName();
77 public Principal getUserPrincipal() {
78 return () -> username.substring(0, username.indexOf("-"));
82 public boolean isUserInRole(String role) {
84 ActionLibraryPrivilege requiredPrivilege =
85 ActionLibraryPrivilege.getPrivilege(httpRequest.getMethod());
86 ActionLibraryPrivilege userPrivilege = ActionLibraryPrivilege
87 .valueOf(username.substring(username.indexOf("-") + 1).toUpperCase());
88 return userPrivilege.ordinal() >= requiredPrivilege.ordinal();
89 } catch (Exception exception) {
90 log.debug("",exception);
95 arg2.doFilter(servletRequest, arg1);
97 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
100 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_UNAUTHORIZED);
103 //call super doFilter of cadi authentication filter
109 private void setResponseStatus(HttpServletResponse response, int status) {
110 response.setStatus(status);
114 public void init(FilterConfig arg0) throws ServletException {