7adbe87aaafc82b981d2d27cc4c7ec0b01d68ad1
[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
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
35         /**
36          * Converts access events to String response codes
37          * 
38          * @param accessEvent the IAccessEvent
39          */
40         public String convert(IAccessEvent accessEvent) {
41                 if (!isStarted()) {
42                         return "INACTIVE_HEADER_CONV";
43                 }
44
45                 String cipherSuite = (String) accessEvent.getRequest().getAttribute("javax.servlet.request.cipher_suite");
46                 String authUser = null;
47                 if (cipherSuite != null) {
48                         try {
49                 X509Certificate certChain[] = (X509Certificate[]) accessEvent.getRequest()
50                         .getAttribute("javax.servlet.request.X509Certificate");
51                                 if(certChain == null || certChain.length == 0){
52
53                                         HttpServletRequest request = accessEvent.getRequest();
54
55                                         String authorization = request.getHeader("Authorization");
56
57                     // Set the auth user to "-" so if the authorization header is not found
58                                         // Or if the decoded basic auth credentials are not found in the format required
59                                         // it should return "-"
60                                         // If the decoded string is in the right format, find the index of ":"
61                     // Then get the substring of the starting point to the colon not including the colon
62
63                     authUser = "-";
64
65                                         if(authorization != null && authorization.startsWith("Basic ")){
66                                                 String credentials = authorization.replace("Basic ", "");
67                         byte[] userCredentials = getDecoder().decode(credentials.getBytes("utf-8"));
68                         credentials = new String(userCredentials);
69
70                                                 int codePoint = credentials.indexOf(':');
71
72                                                 if(codePoint != -1){
73                             authUser = credentials.substring(0, codePoint);
74                                                 }
75
76                                         }
77
78                                         return authUser;
79
80                                 } else {
81                                         X509Certificate clientCert = certChain[0];
82                                         X500Principal subjectDN = clientCert.getSubjectX500Principal();
83                                         authUser = subjectDN.toString();
84                                         return authUser;
85                                 }
86                         } catch(Exception ex){
87                                 return "-";
88                         }
89                 } else {
90                         return "-";
91                 }
92         }
93
94 }