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 java.io.IOException;
24 import java.security.Principal;
25 import java.util.Base64;
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletRequestWrapper;
35 import javax.servlet.http.HttpServletResponse;
37 public class ActionAuthenticationFilter implements Filter {
39 private boolean runningOnLocal = true;
42 public void destroy() {
47 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
48 throws IOException, ServletException {
51 HttpServletRequest httpRequest = (HttpServletRequest) arg0;
52 String authorizationHeader = httpRequest.getHeader("Authorization");
53 if (authorizationHeader != null && !authorizationHeader.isEmpty()) {
56 String base64Credentials =
57 httpRequest.getHeader("Authorization").replace("Basic", "").trim();
58 String decodedCredentials = new String(Base64.getDecoder().decode(base64Credentials));
59 username = decodedCredentials.substring(0, decodedCredentials.indexOf(":"));
60 } catch (Exception e0) {
61 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
64 if (username.startsWith("AUTH")) {
65 HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(httpRequest) {
67 public java.lang.String getRemoteUser() {
68 return getUserPrincipal().getName();
72 public Principal getUserPrincipal() {
73 return () -> username.substring(0, username.indexOf("-"));
77 public boolean isUserInRole(String role) {
79 ActionLibraryPrivilege requiredPrivilege =
80 ActionLibraryPrivilege.getPrivilege(httpRequest.getMethod());
81 ActionLibraryPrivilege userPrivilege = ActionLibraryPrivilege
82 .valueOf(username.substring(username.indexOf("-") + 1).toUpperCase());
83 return userPrivilege.ordinal() >= requiredPrivilege.ordinal();
84 } catch (Exception e0) {
89 arg2.doFilter(servletRequest, arg1);
91 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
94 setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_UNAUTHORIZED);
97 //call super doFilter of cadi authentication filter
103 private void setResponseStatus(HttpServletResponse response, int status) {
104 response.setStatus(status);
108 public void init(FilterConfig arg0) throws ServletException {
109 /*runningOnLocal = System.getProperty("file.separator").equals("\\");
110 if (!runningOnLocal){
111 // call to super init of cadi filter as we are not running on windows