dc6559ab2175540c7348555a12972fb75b12cc6c
[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  * 
37  */
38 package org.onap.portalsdk.external.authorization.util;
39
40 import java.io.IOException;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43
44 import javax.xml.bind.DatatypeConverter;
45
46 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
47 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
48 import org.onap.portalsdk.core.util.SystemProperties;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.http.MediaType;
51
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 public class EcompExternalAuthUtils {
55
56         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EcompExternalAuthUtils.class);
57         
58         public static final String EXT_EMPTY_JSON_STRING = "{}";
59         public static final String EXT_ROLE_FIELD = "role";
60         public static final String EXT_PERM_FIELD = "perm";
61         public static final String EXT_PERM_FIELD_TYPE = "type";
62         public static final String EXT_PERM_ACCESS = ".access";
63         public static final String EXT_ROLE_FIELD_NAME = "name";
64         public static final String EXT_NULL_VALUE = "null";
65         public static final String EXT_FIELD_DESCRIPTION = "description";
66         public static final String EXT_FIELD_PERMS = "perms";
67         public static final String EXT_ROLE_FIELD_OWNER = ".owner";
68         public static final String EXT_ROLE_FIELD_ADMIN = ".admin";
69
70         public static final Pattern VALID_USER_DOMAIN_ADDRESS_REGEX = 
71                     Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
72         
73         public static HttpHeaders base64encodeKeyForAAFBasicAuth(String username, String password) throws Exception {
74                 String usernamePass = username + ":" + password;
75                 String encToBase64 = String.valueOf((DatatypeConverter.printBase64Binary(usernamePass.getBytes())));
76                 HttpHeaders headers = new HttpHeaders();
77                 headers.add("Authorization", "Basic " + encToBase64);
78                 headers.setContentType(MediaType.APPLICATION_JSON);
79                 return headers;
80         }
81
82         public static String decryptPass(String encrypted) throws Exception {
83                 String result = "";
84                 if (encrypted != null && encrypted.length() > 0) {
85                         try {
86                                 result = CipherUtil.decryptPKC(encrypted,
87                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
88                         } catch (Exception e) {
89                                 logger.error(EELFLoggerDelegate.errorLogger,"decryptedPassword failed", e);
90                                 throw e;
91                         }
92                 }
93                 return result;
94         }
95         
96         /**
97          * Validates, if given username has fully domain address
98          * @param String
99          * @return true or false
100          */
101         public static boolean validate(String username) {
102         Matcher matcher = VALID_USER_DOMAIN_ADDRESS_REGEX.matcher(username);
103         return matcher.find();
104         }
105         
106         /**
107          * 
108          * It checks whether the namespace is matching or not
109          * 
110          * @param permTypeVal
111          * @param appNamespaceVal
112          * @return true or false
113          */
114         public static boolean checkNameSpaceMatching(String permTypeVal, String appNamespaceVal) {
115                 String[] typeNamespace = permTypeVal.split("\\.");
116                 String[] appNamespace = appNamespaceVal.split("\\.");
117                 boolean isNamespaceMatching = true;
118                 if (appNamespace.length <= typeNamespace.length) {
119                         for (int k = 0; k < appNamespace.length; k++) {
120                                 if (!appNamespace[k].equals(typeNamespace[k]))
121                                         isNamespaceMatching = false;
122                         }
123
124                 } else {
125                         isNamespaceMatching = false;
126                 }
127                 return isNamespaceMatching;
128         }
129         
130         /**
131          * 
132          * It validates whether given string is JSON or not
133          * 
134          * @param jsonInString
135          * @return true or false
136          */
137           public static boolean isJSONValid(String jsonInString ) {
138                     try {
139                        final ObjectMapper mapper = new ObjectMapper();
140                        mapper.readTree(jsonInString);
141                        return true;
142                     } catch (IOException e) {
143                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to parse Json!", e);
144                        return false;
145                     }
146                   }
147 }