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