Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / auth / AAIMicroServiceAuth.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.auth;
22
23 import java.security.cert.X509Certificate;
24 import javax.inject.Inject;
25 import javax.security.auth.x500.X500Principal;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.HttpHeaders;
28 import org.onap.aai.babel.config.BabelAuthConfig;
29 import org.onap.aai.babel.logging.LogHelper;
30 import org.onap.aai.cl.api.Logger;
31
32 /**
33  * Public class for authentication and authorization operations. Authorization is applied according to user and role
34  */
35 public class AAIMicroServiceAuth {
36
37     private static final Logger applicationLogger = LogHelper.INSTANCE;
38
39     private BabelAuthConfig babelAuthConfig;
40
41     /**
42      * @param babelAuthConfig
43      * @throws AAIAuthException
44      */
45     @Inject
46     public AAIMicroServiceAuth(final BabelAuthConfig babelAuthConfig) throws AAIAuthException {
47         this.babelAuthConfig = babelAuthConfig;
48         if (!babelAuthConfig.isAuthenticationDisable()) {
49             AAIMicroServiceAuthCore.init(babelAuthConfig.getAuthPolicyFile());
50         }
51     }
52
53     /**
54      * @param username
55      * @param policyFunction
56      * @return
57      * @throws AAIAuthException
58      */
59     public boolean authorize(String username, String policyFunction) throws AAIAuthException {
60         return AAIMicroServiceAuthCore.authorize(username, policyFunction);
61     }
62
63     /**
64      * @param authUser
65      * @param policyFunction
66      * @return
67      * @throws AAIAuthException
68      */
69     public String authenticate(String authUser, String policyFunction) throws AAIAuthException {
70         if (authorize(authUser, policyFunction)) {
71             return "OK";
72         } else {
73             return "AAI_9101";
74         }
75     }
76
77     /**
78      * @param headers
79      * @param req
80      * @param action
81      * @param apiPath
82      * @return
83      * @throws AAIAuthException
84      */
85     public boolean validateRequest(HttpHeaders headers /* NOSONAR */, HttpServletRequest req,
86             AAIMicroServiceAuthCore.HTTP_METHODS action, String apiPath) throws AAIAuthException {
87
88         applicationLogger.debug("validateRequest: " + apiPath);
89         applicationLogger
90                 .debug("babelAuthConfig.isAuthenticationDisable(): " + babelAuthConfig.isAuthenticationDisable());
91
92         if (babelAuthConfig.isAuthenticationDisable()) {
93             return true;
94         }
95
96         String[] ps = apiPath.split("/");
97         String authPolicyFunctionName = ps[0];
98         if (ps.length > 1 && authPolicyFunctionName.matches("v\\d+")) {
99             authPolicyFunctionName = ps[1];
100         }
101
102         String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
103         String authUser = null;
104
105         if (cipherSuite != null) {
106             X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
107             X509Certificate clientCert = certChain[0];
108             X500Principal subjectDN = clientCert.getSubjectX500Principal();
109             authUser = subjectDN.toString();
110         }
111
112         if (authUser != null) {
113             return "OK".equals(authenticate(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName));
114         } else {
115             return false;
116         }
117     }
118 }