push addional code
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / onboarding-rest-war / src / main / java / org / openecomp / server / filters / ActionAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.server.filters;
22
23 import java.io.IOException;
24 import java.security.Principal;
25 import java.util.Base64;
26
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;
36
37 public class ActionAuthenticationFilter implements Filter {
38
39   private boolean runningOnLocal = true;
40
41   @Override
42   public void destroy() {
43
44   }
45
46   @Override
47   public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
48       throws IOException, ServletException {
49     if (runningOnLocal) {
50
51       HttpServletRequest httpRequest = (HttpServletRequest) arg0;
52       String authorizationHeader = httpRequest.getHeader("Authorization");
53       if (authorizationHeader != null && !authorizationHeader.isEmpty()) {
54         String username;
55         try {
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);
62           return;
63         }
64         if (username.startsWith("AUTH")) {
65           HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(httpRequest) {
66             @Override
67             public java.lang.String getRemoteUser() {
68               return getUserPrincipal().getName();
69             }
70
71             @Override
72             public Principal getUserPrincipal() {
73               return () -> username.substring(0, username.indexOf("-"));
74             }
75
76             @Override
77             public boolean isUserInRole(String role) {
78               try {
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) {
85                 return false;
86               }
87             }
88           };
89           arg2.doFilter(servletRequest, arg1);
90         } else {
91           setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
92         }
93       } else {
94         setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_UNAUTHORIZED);
95       }
96     } else {
97       //call super doFilter of cadi authentication filter
98     }
99
100
101   }
102
103   private void setResponseStatus(HttpServletResponse response, int status) {
104     response.setStatus(status);
105   }
106
107   @Override
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
112   }*/
113   }
114
115 }