Fix in CNName
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / logging / CNName.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.logging;
23
24 import ch.qos.logback.access.pattern.AccessConverter;
25 import ch.qos.logback.access.spi.IAccessEvent;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import javax.security.auth.x500.X500Principal;
30 import javax.servlet.http.HttpServletRequest;
31 import java.security.cert.X509Certificate;
32
33 import static java.util.Base64.getDecoder;
34
35 public class CNName extends AccessConverter {
36         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(CNName.class);
37
38         /**
39          * Converts access events to String response codes
40          * 
41          * @param accessEvent the IAccessEvent
42          */
43         public String convert(IAccessEvent accessEvent) {
44                 if (!isStarted()) {
45                         return "INACTIVE_HEADER_CONV";
46                 }
47
48                 String cipherSuite = (String) accessEvent.getRequest().getAttribute("javax.servlet.request.cipher_suite");
49                 String authUser = null;
50                 if (cipherSuite != null) {
51                         try {
52                 X509Certificate certChain[] = (X509Certificate[]) accessEvent.getRequest()
53                         .getAttribute("javax.servlet.request.X509Certificate");
54                                 if(certChain == null || certChain.length == 0){
55
56                                         HttpServletRequest request = accessEvent.getRequest();
57
58                                         String authorization = request.getHeader("Authorization");
59
60                     // Set the auth user to "-" so if the authorization header is not found
61                                         // Or if the decoded basic auth credentials are not found in the format required
62                                         // it should return "-"
63                                         // If the decoded string is in the right format, find the index of ":"
64                     // Then get the substring of the starting point to the colon not including the colon
65
66                     authUser = "-";
67
68                                         if(authorization != null && authorization.startsWith("Basic ")){
69                                                 String credentials = authorization.replace("Basic ", "");
70                         byte[] userCredentials = getDecoder().decode(credentials.getBytes("utf-8"));
71                         credentials = new String(userCredentials);
72
73                                                 int codePoint = credentials.indexOf(':');
74
75                                                 if(codePoint != -1){
76                             authUser = credentials.substring(0, codePoint);
77                                                 }
78
79                                         }
80
81                                         return authUser;
82
83                                 } else {
84                                         X509Certificate clientCert = certChain[0];
85                                         X500Principal subjectDN = clientCert.getSubjectX500Principal();
86                                         authUser = subjectDN.toString();
87                                         return authUser;
88                                 }
89                         } catch(Exception e){
90                 LOGGER.error(e.getMessage(),e);
91                                 return "-";
92                         }
93                 } else {
94                         return "-";
95                 }
96         }
97
98 }