*/
package org.onap.music.main;
-import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.UUID;
-import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.jcs.JCS;
import org.apache.commons.jcs.access.CacheAccess;
-import org.codehaus.jackson.map.ObjectMapper;
+import org.mindrot.jbcrypt.BCrypt;
import org.onap.music.datastore.PreparedQueryObject;
-import org.onap.music.datastore.jsonobjects.AAFResponse;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.AppMessages;
import org.onap.music.eelf.logging.format.ErrorSeverity;
import org.onap.music.eelf.logging.format.ErrorTypes;
import org.onap.music.exceptions.MusicServiceException;
-
-import com.att.eelf.configuration.EELFLogger;
+import org.onap.music.datastore.jsonobjects.JsonCallback;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache");
private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache");
private static CacheAccess<String, Map<String, String>> musicValidateCache = JCS.getInstance("musicValidateCache");
+ private static CacheAccess<String, JsonCallback> callBackCache = JCS.getInstance("callBackCache");
+ private static CacheAccess<String, List<String>> callbackNotifyList = JCS.getInstance("callbackNotifyList");
private static Map<String, Number> userAttempts = new HashMap<>();
private static Map<String, Calendar> lastFailedTime = new HashMap<>();
return true;
return false;
}
+
+ public static void updateCallBackCache(String appName, JsonCallback jsonCallBack) {
+ logger.info("updateCallBackCache: updating cache.....");
+ callBackCache.put(appName, jsonCallBack);
+ }
+
+ public static JsonCallback getCallBackCache(String appName) {
+ return callBackCache.get(appName);
+ }
+ public static void updateCallbackNotifyList(List<String> notifyList) {
+ logger.info("callbackNotifyList: updating cache.....");
+ callbackNotifyList.put("callbackNotify", notifyList);
+ }
+
+ public static List<String> getCallbackNotifyList() {
+ return callbackNotifyList.get("callbackNotify");
+ }
+
public void initializeMusicCache() {
logger.info(EELFLoggerDelegate.applicationLogger,"Initializing Music Cache...");
musicCache.put("isInitialized", "true");
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), false));
} catch (Exception e1) {
logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- e1.printStackTrace();
}
ResultSet rs = MusicCore.get(pQuery);
Iterator<Row> it = rs.iterator();
String keySpace = row.getString("application_name");
try {
userAttempts.put(nameSpace, 0);
- boolean aafRresponse = triggerAAF(nameSpace, userId, password);
- if (aafRresponse) {
+ boolean responseObj = triggerAAF(nameSpace, userId, password);
+ if (responseObj) {
map = new HashMap<>();
map.put(userId, password);
aafCache.put(nameSpace, map);
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Something at AAF was changed for ns: " + nameSpace+" So not updating Cache for the namespace. ");
- e.printStackTrace();
}
}
public static boolean authenticateAAFUser(String nameSpace, String userId, String password,
String keySpace) throws Exception {
- if (aafCache.get(nameSpace) != null) {
+ if (aafCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) {
if (keySpace != null && !musicCache.get(keySpace).equals(nameSpace)) {
logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace.");
} else if (aafCache.get(nameSpace).get(userId).equals(password)) {
}
}
- boolean aafRresponse = triggerAAF(nameSpace, userId, password);
- if (aafRresponse) {
- //TODO
+ boolean responseObj = triggerAAF(nameSpace, userId, password);
+ if (responseObj) {
//if (responseObj.getNs().get(0).getAdmin().contains(userId)) {
- //Map<String, String> map = new HashMap<>();
- //map.put(userId, password);
- //aafCache.put(nameSpace, map);
+ Map<String, String> map = new HashMap<>();
+ map.put(userId, password);
+ aafCache.put(nameSpace, map);
+ CachingUtil.updateMusicCache(keySpace, nameSpace);
return true;
//}
}
logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated");
- return aafRresponse;
+ return false;
}
private static boolean triggerAAF(String nameSpace, String userId, String password)
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Basic " + base64Creds)
.header("content-type", "application/json").get(ClientResponse.class);
- if(response.getStatus() == 200)
- return true;
- else if (response.getStatus() != 200) {
+ if (response.getStatus() != 200) {
if (userAttempts.get(nameSpace) == null)
userAttempts.put(nameSpace, 0);
if ((Integer) userAttempts.get(nameSpace) >= 2) {
String x = response.getEntity(String.class);
AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class);*/
- return false;
+ return true;
}
public static void updateMusicCache(String keyspace, String nameSpace) {
appNameCache.put(namespace, isAAF);
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- e.printStackTrace();
}
}
return isAAF;
uuid = rs.getUUID("uuid").toString();
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger,"Exception occured during uuid retrieval from DB."+e.getMessage());
- e.printStackTrace();
}
}
return uuid;
appName = rs.getString("application_name");
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- e.printStackTrace();
}
return appName;
}
rs = MusicCore.get(queryObject).one();
} catch (MusicServiceException e) {
// TODO Auto-generated catch block
- e.printStackTrace();
resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
return resultMap;
} catch (InvalidQueryException e) {
logger.error(EELFLoggerDelegate.errorLogger,"Application is not onboarded. Please contact admin.");
resultMap.put("Exception", "Application is not onboarded. Please contact admin.");
} else {
- if(!(rs.getString("username").equals(userId)) || !(rs.getString("password").equals(password))) {
+ if(!(rs.getString("username").equals(userId)) || !(BCrypt.checkpw(password, rs.getString("password")))) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId);
resultMap.put("Exception", "Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId);
public static Map<String, Object> authenticateAIDUser(String nameSpace, String userId, String password,
String keyspace) {
Map<String, Object> resultMap = new HashMap<>();
+ String pwd = null;
if((musicCache.get(keyspace) != null) && (musicValidateCache.get(nameSpace) != null)
&& (musicValidateCache.get(nameSpace).containsKey(userId))) {
if(!musicCache.get(keyspace).equals(nameSpace)) {
resultMap.put("Exception", "Namespace and keyspace doesn't match");
return resultMap;
}
- if(!musicValidateCache.get(nameSpace).get(userId).equals(password)) {
+ if(!BCrypt.checkpw(password,musicValidateCache.get(nameSpace).get(userId))) {
resultMap.put("Exception", "Namespace, userId and password doesn't match");
return resultMap;
}
try {
queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace));
} catch (Exception e) {
- e.printStackTrace();
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
}
Row rs = null;
try {
rs = MusicCore.get(queryObject).one();
} catch (MusicServiceException e) {
- e.printStackTrace();
- resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
+ resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
return resultMap;
}
if(rs == null) {
}
else {
String user = rs.getString("username");
- String pwd = rs.getString("password");
+ pwd = rs.getString("password");
String ns = rs.getString("application_name");
if(!ns.equals(nameSpace)) {
resultMap.put("Exception", "Namespace and keyspace doesn't match");
resultMap.put("Exception", "Invalid userId :"+userId);
return resultMap;
}
- if(!pwd.equals(password)) {
+ if(!BCrypt.checkpw(password, pwd)) {
resultMap.put("Exception", "Invalid password");
return resultMap;
}
}
CachingUtil.updateMusicCache(keyspace, nameSpace);
- CachingUtil.updateMusicValidateCache(nameSpace, userId, password);
+ CachingUtil.updateMusicValidateCache(nameSpace, userId, pwd);
return resultMap;
}
+
+ public static void deleteKeysFromDB(String deleteKeys) {
+ PreparedQueryObject pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString(
+ "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")");
+ try {
+ MusicCore.nonKeyRelatedPut(pQuery, "eventual");
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, "Error in deleteKeysFromDB");
+ }
+ }
}