Sonar Changes for Authentication Classes
[music.git] / src / main / java / org / onap / music / authentication / MusicAuthorizationFilter.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.authentication;
26
27 import java.io.IOException;
28
29 import javax.servlet.Filter;
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletRequest;
34 import javax.servlet.ServletResponse;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.onap.music.eelf.logging.EELFLoggerDelegate;
38 import org.onap.music.exceptions.MusicAuthenticationException;
39 import org.onap.music.main.MusicUtil;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 /**
43  * This filter class does authorization from AAF
44  *  
45  * @author sp931a
46  *
47  */
48 //@PropertySource(value = {"file:/opt/app/music/etc/music.properties"})
49 public class MusicAuthorizationFilter implements Filter {
50
51     private String musicNS = MusicUtil.getMusicAafNs();
52     
53     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicAuthorizationFilter.class);
54
55     public MusicAuthorizationFilter() throws ServletException {
56         super();
57     }
58
59     @Override
60     public void init(FilterConfig filterConfig) throws ServletException {
61         // Do Nothing
62     }
63
64     @Override
65     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
66         throws IOException, ServletException {
67         HttpServletResponse httpResponse = null;
68
69         boolean isAuthAllowed = false;
70
71         if (null != servletRequest && null != servletResponse) {
72             httpResponse = (HttpServletResponse) servletResponse;
73             long startTime = 0;
74             if( null != servletRequest.getAttribute("startTime")) {
75                 startTime = ((Long)servletRequest.getAttribute("startTime")).longValue();
76             } else {
77                 startTime = System.currentTimeMillis(); // this will set only incase the request attribute not found
78             }
79
80             try {
81                 isAuthAllowed = AuthUtil.isAccessAllowed(servletRequest, musicNS);
82             } catch (MusicAuthenticationException e) {
83                 logger.error(EELFLoggerDelegate.securityLogger,
84                     "Error while checking authorization Music Namespace: " + musicNS + " : " + e.getMessage(),e);
85             } catch ( Exception e) {
86                 logger.error(EELFLoggerDelegate.securityLogger,
87                     "Error while checking authorization Music Namespace: " + musicNS + " : " + e.getMessage(),e);
88             }
89
90             long endTime = System.currentTimeMillis();
91             
92             //startTime set in <code>CadiAuthFilter</code> doFilter
93             logger.debug(EELFLoggerDelegate.securityLogger,
94                 "Time took for authentication & authorization : " 
95                 + (endTime - startTime) + " milliseconds");
96
97             if (!isAuthAllowed) {
98                 logger.info(EELFLoggerDelegate.securityLogger,
99                     "Unauthorized Access");
100                 AuthorizationError authError = new AuthorizationError();
101                 authError.setResponseCode(HttpServletResponse.SC_UNAUTHORIZED);
102                 authError.setResponseMessage("Unauthorized Access - Please make sure you are "
103                     + "onboarded and have proper access to MUSIC. ");
104
105                 byte[] responseToSend = restResponseBytes(authError);
106                 httpResponse.setHeader("Content-Type", "application/json");
107
108                 httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
109                 servletResponse.getOutputStream().write(responseToSend);
110                 return;
111             } else {
112                 filterChain.doFilter(servletRequest, servletResponse);
113             }
114         }
115     }
116
117     private byte[] restResponseBytes(AuthorizationError eErrorResponse) throws IOException {
118         String serialized = new ObjectMapper().writeValueAsString(eErrorResponse);
119         return serialized.getBytes();
120     }
121 }
122