Fix checkstyle issues including javadoc
[aai/babel.git] / src / main / java / org / onap / aai / auth / AAIMicroServiceAuth.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.auth;
23
24 import java.security.cert.X509Certificate;
25 import javax.inject.Inject;
26 import javax.security.auth.x500.X500Principal;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.HttpHeaders;
29 import org.onap.aai.babel.config.BabelAuthConfig;
30 import org.onap.aai.babel.logging.LogHelper;
31 import org.onap.aai.cl.api.Logger;
32
33 /**
34  * Public class for authentication and authorization operations. Authorization is applied according to user and role
35  */
36 public class AAIMicroServiceAuth {
37
38     private static final Logger applicationLogger = LogHelper.INSTANCE;
39
40     private BabelAuthConfig babelAuthConfig;
41
42     /**
43      * @param babelAuthConfig
44      * @throws AAIAuthException
45      *             if the Auth Policy cannot be loaded
46      */
47     @Inject
48     public AAIMicroServiceAuth(final BabelAuthConfig babelAuthConfig) throws AAIAuthException {
49         this.babelAuthConfig = babelAuthConfig;
50         if (!babelAuthConfig.isAuthenticationDisable()) {
51             AAIMicroServiceAuthCore.init(babelAuthConfig.getAuthPolicyFile());
52         }
53     }
54
55     /**
56      * @param headers
57      * @param req
58      * @param action
59      * @param apiPath
60      * @return
61      * @throws AAIAuthException
62      */
63     public boolean validateRequest(HttpHeaders headers /* NOSONAR */, HttpServletRequest req,
64             AAIMicroServiceAuthCore.HTTP_METHODS action, String apiPath) throws AAIAuthException {
65
66         applicationLogger.debug("validateRequest: " + apiPath);
67         applicationLogger
68                 .debug("babelAuthConfig.isAuthenticationDisable(): " + babelAuthConfig.isAuthenticationDisable());
69
70         if (babelAuthConfig.isAuthenticationDisable()) {
71             return true;
72         }
73
74         String[] ps = apiPath.split("/");
75         String authPolicyFunctionName = ps[ps.length - 1];
76         String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
77         String authUser = null;
78
79         if (cipherSuite != null) {
80             X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
81             X509Certificate clientCert = certChain[0];
82             X500Principal subjectDN = clientCert.getSubjectX500Principal();
83             authUser = subjectDN.toString();
84         }
85
86         if (authUser != null) {
87             return AAIMicroServiceAuthCore.authorize(authUser.toLowerCase(),
88                     action.toString() + ":" + authPolicyFunctionName);
89         } else {
90             return false;
91         }
92     }
93 }