56b1527e3a20b5bb7f98082e0870b4ba04db0ec2
[portal/sdk.git] /
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.external.authorization.util;
39
40 import java.io.IOException;
41
42 import javax.xml.bind.DatatypeConverter;
43
44 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
45 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
46 import org.onap.portalsdk.core.util.SystemProperties;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.MediaType;
50
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 public class EcompExternalAuthUtils {
54
55         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EcompExternalAuthUtils.class);
56
57         @Autowired
58         CipherUtil cipherUtil;
59         
60         public static final String EXT_EMPTY_JSON_STRING = "{}";
61         public static final String EXT_ROLE_FIELD = "role";
62         public static final String EXT_PERM_FIELD = "perm";
63         public static final String EXT_PERM_FIELD_TYPE = "type";
64         public static final String EXT_PERM_ACCESS = ".access";
65         public static final String EXT_ROLE_FIELD_NAME = "name";
66         public static final String EXT_NULL_VALUE = "null";
67         public static final String EXT_FIELD_DESCRIPTION = "description";
68         public static final String EXT_FIELD_PERMS = "perms";
69         public static final String EXT_ROLE_FIELD_OWNER = ".owner";
70         public static final String EXT_ROLE_FIELD_ADMIN = ".admin";
71
72         public static HttpHeaders base64encodeKeyForAAFBasicAuth() throws Exception {
73                 String userName = EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_NAME);
74                 String encryptedPass = EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_PASSWORD);
75                 String decryptedPass = decryptPass(encryptedPass);
76                 String usernamePass = userName + ":" + decryptedPass;
77                 String encToBase64 = String.valueOf((DatatypeConverter.printBase64Binary(usernamePass.getBytes())));
78                 HttpHeaders headers = new HttpHeaders();
79                 headers.add("Authorization", "Basic " + encToBase64);
80                 headers.setContentType(MediaType.APPLICATION_JSON);
81                 return headers;
82         }
83
84         private static String decryptPass(String encrypted) throws Exception {
85                 String result = "";
86                 if (encrypted != null && encrypted.length() > 0) {
87                         try {
88                                 result = CipherUtil.decryptPKC(encrypted,
89                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
90                         } catch (Exception e) {
91                                 logger.error(EELFLoggerDelegate.errorLogger,"decryptedPassword failed", e);
92                                 throw e;
93                         }
94                 }
95                 return result;
96         }
97         
98         /**
99          * 
100          * It checks whether the namespace is matching or not
101          * 
102          * @param permTypeVal
103          * @param appNamespaceVal
104          * @return true or false
105          */
106         public static boolean checkNameSpaceMatching(String permTypeVal, String appNamespaceVal) {
107                 String[] typeNamespace = permTypeVal.split("\\.");
108                 String[] appNamespace = appNamespaceVal.split("\\.");
109                 boolean isNamespaceMatching = true;
110                 if (appNamespace.length <= typeNamespace.length) {
111                         for (int k = 0; k < appNamespace.length; k++) {
112                                 if (!appNamespace[k].equals(typeNamespace[k]))
113                                         isNamespaceMatching = false;
114                         }
115
116                 } else {
117                         isNamespaceMatching = false;
118                 }
119                 return isNamespaceMatching;
120         }
121         
122         /**
123          * 
124          * It validates whether given string is JSON or not
125          * 
126          * @param jsonInString
127          * @return true or false
128          */
129           public static boolean isJSONValid(String jsonInString ) {
130                     try {
131                        final ObjectMapper mapper = new ObjectMapper();
132                        mapper.readTree(jsonInString);
133                        return true;
134                     } catch (IOException e) {
135                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to parse Json!", e);
136                        return false;
137                     }
138                   }
139 }