2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.external.authorization.util;
40 import java.io.IOException;
42 import javax.xml.bind.DatatypeConverter;
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.http.HttpHeaders;
48 import org.springframework.http.MediaType;
50 import com.fasterxml.jackson.databind.ObjectMapper;
52 public class EcompExternalAuthUtils {
54 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EcompExternalAuthUtils.class);
56 public static final String EXT_EMPTY_JSON_STRING = "{}";
57 public static final String EXT_ROLE_FIELD = "role";
58 public static final String EXT_PERM_FIELD = "perm";
59 public static final String EXT_PERM_FIELD_TYPE = "type";
60 public static final String EXT_PERM_ACCESS = ".access";
61 public static final String EXT_ROLE_FIELD_NAME = "name";
62 public static final String EXT_NULL_VALUE = "null";
63 public static final String EXT_FIELD_DESCRIPTION = "description";
64 public static final String EXT_FIELD_PERMS = "perms";
65 public static final String EXT_ROLE_FIELD_OWNER = ".owner";
66 public static final String EXT_ROLE_FIELD_ADMIN = ".admin";
68 public static HttpHeaders base64encodeKeyForAAFBasicAuth() throws Exception {
69 String userName = EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_NAME);
70 String encryptedPass = EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_PASSWORD);
71 String decryptedPass = decryptPass(encryptedPass);
72 String usernamePass = userName + ":" + decryptedPass;
73 String encToBase64 = String.valueOf((DatatypeConverter.printBase64Binary(usernamePass.getBytes())));
74 HttpHeaders headers = new HttpHeaders();
75 headers.add("Authorization", "Basic " + encToBase64);
76 headers.setContentType(MediaType.APPLICATION_JSON);
80 private static String decryptPass(String encrypted) throws Exception {
82 if (encrypted != null && encrypted.length() > 0) {
84 result = CipherUtil.decryptPKC(encrypted,
85 SystemProperties.getProperty(SystemProperties.Decryption_Key));
86 } catch (Exception e) {
87 logger.error(EELFLoggerDelegate.errorLogger,"decryptedPassword failed", e);
96 * It checks whether the namespace is matching or not
99 * @param appNamespaceVal
100 * @return true or false
102 public static boolean checkNameSpaceMatching(String permTypeVal, String appNamespaceVal) {
103 String[] typeNamespace = permTypeVal.split("\\.");
104 String[] appNamespace = appNamespaceVal.split("\\.");
105 boolean isNamespaceMatching = true;
106 if (appNamespace.length <= typeNamespace.length) {
107 for (int k = 0; k < appNamespace.length; k++) {
108 if (!appNamespace[k].equals(typeNamespace[k]))
109 isNamespaceMatching = false;
113 isNamespaceMatching = false;
115 return isNamespaceMatching;
120 * It validates whether given string is JSON or not
122 * @param jsonInString
123 * @return true or false
125 public static boolean isJSONValid(String jsonInString ) {
127 final ObjectMapper mapper = new ObjectMapper();
128 mapper.readTree(jsonInString);
130 } catch (IOException e) {
131 logger.error(EELFLoggerDelegate.errorLogger, "Failed to parse Json!", e);