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