Add collaboration feature
[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 org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25
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;
38
39 public class ActionAuthenticationFilter implements Filter {
40
41   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
42   private boolean runningOnLocal = true;
43
44   @Override
45   public void destroy() {
46     // TODO Auto-generated method stub
47
48   }
49
50   @Override
51   public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
52       throws IOException, ServletException {
53     if (runningOnLocal) {
54
55       HttpServletRequest httpRequest = (HttpServletRequest) arg0;
56       String authorizationHeader = httpRequest.getHeader("Authorization");
57       if (authorizationHeader != null && !authorizationHeader.isEmpty()) {
58         String username;
59         try {
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);
67           return;
68         }
69         if (username.startsWith("AUTH")) {
70           HttpServletRequestWrapper servletRequest = new HttpServletRequestWrapper(httpRequest) {
71             @Override
72             public java.lang.String getRemoteUser() {
73               return getUserPrincipal().getName();
74             }
75
76             @Override
77             public Principal getUserPrincipal() {
78               return () -> username.substring(0, username.indexOf("-"));
79             }
80
81             @Override
82             public boolean isUserInRole(String role) {
83               try {
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);
91                 return false;
92               }
93             }
94           };
95           arg2.doFilter(servletRequest, arg1);
96         } else {
97           setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_FORBIDDEN);
98         }
99       } else {
100         setResponseStatus((HttpServletResponse) arg1, HttpServletResponse.SC_UNAUTHORIZED);
101       }
102     } else {
103       //call super doFilter of cadi authentication filter
104     }
105
106
107   }
108
109   private void setResponseStatus(HttpServletResponse response, int status) {
110     response.setStatus(status);
111   }
112
113   @Override
114   public void init(FilterConfig arg0) throws ServletException {
115
116   }
117
118 }