+ @Override
+ public boolean authenticateUser(String namespace, String authorization, String keyspace,
+ String aid, Operation operation) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Inside User Authentication.......");
+ Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
+ String userId = userCredentials.get(MusicUtil.USERID);
+ String password = userCredentials.get(MusicUtil.PASSWORD);
+
+ Map<String, Object> resultMap = new HashMap<>();
+ String uuid = null;
+ if(! MusicUtil.getIsCadi()) {
+ resultMap = CachingUtil.validateRequest(namespace, userId, password, keyspace, aid,
+ operation);
+ if (!resultMap.isEmpty())
+ return false;
+ String isAAFApp = null;
+ try {
+ isAAFApp= CachingUtil.isAAFApplication(namespace);
+ } catch(MusicServiceException e) {
+ logger.error(e.getErrorMessage(), e);
+ resultMap.put("Exception", e.getMessage());
+ return false;
+ }
+ if(isAAFApp == null) {
+ resultMap.put("Exception", "Namespace: "+namespace+" doesn't exist. Please make sure ns(appName)"
+ + " is correct and Application is onboarded.");
+ return false;
+ }
+ boolean isAAF = Boolean.parseBoolean(isAAFApp);
+ if (userId == null || password == null) {
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"UserId/Password or more required headers is missing.");
+ resultMap.put("Exception",
+ "UserId and Password are mandatory for the operation " + operation);
+ return false;
+ }
+ if(!isAAF && !(operation==Operation.CREATE_KEYSPACE)) {
+ resultMap = CachingUtil.authenticateAIDUser(namespace, userId, password, keyspace);
+ if (!resultMap.isEmpty())
+ return false;
+
+ }
+ if (isAAF && namespace != null && userId != null && password != null) {
+ boolean isValid = true;
+ try {
+ isValid = CachingUtil.authenticateAAFUser(namespace, userId, password, keyspace);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,"Error while aaf authentication for user:" + userId);
+ logger.error(EELFLoggerDelegate.errorLogger,"Error: "+ e.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + namespace);
+ resultMap.put("Exception", e.getMessage());
+ }
+ if (!isValid) {
+ logger.error(EELFLoggerDelegate.errorLogger,"User not authenticated...", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ resultMap.put("Exception", "User not authenticated...");
+ }
+ if (!resultMap.isEmpty())
+ return false;
+
+ }
+ } else {
+
+ String cachedKS = CachingUtil.getKSFromCadiCache(userId);
+ if(cachedKS != null && !cachedKS.equals(keyspace)) {
+ resultMap.put("Exception", "User not authenticated to access this keyspace...");
+ return false;
+ }
+ }
+
+ if (operation==Operation.CREATE_KEYSPACE) {
+ try {
+ logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace.");
+ PreparedQueryObject pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString(
+ "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), namespace));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
+ MusicUtil.DEFAULTKEYSPACENAME));
+ Row rs = MusicCore.get(pQuery).one();
+ uuid = rs.getUUID("uuid").toString();
+ resultMap.put("uuid", "existing");
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID.");
+ uuid = MusicUtil.generateUUID();
+ resultMap.put("uuid", "new");
+ }
+ resultMap.put("aid", uuid);
+ CachingUtil.updateCadiCache(userId, keyspace);
+ }
+ return true;