<commit_before>package de.passau.uni.sec.compose.pdp.servioticy.authz;




import java.io.IOException;
import java.util.List;
import java.lang.String;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import de.passau.uni.sec.compose.pdp.servioticy.PermissionCacheObject;
import de.passau.uni.sec.compose.pdp.servioticy.exception.PDPServioticyException;
import de.passau.uni.sec.compose.pdp.servioticy.idm.IDMCommunicator;

public class AuthorizationServioticy 
{
	private static final String IDM_USER_SECTION = "username";


	/**
	 * Verifies the policy for the currnt SO and a SU
	 *
	 * @param security_metadata_SO_current
	 * @param security_metadata_of_the_SU
	 * @param cache
	 * @param idmHost
	 * @param idmUser
	 * @param idmPass
	 * @param idmPort
	 * @return returns true or false in the "Permission" entry of the cach object depending on the policy evaluation
	 */
	public PermissionCacheObject verifyGetData( String access_token_user ,
			JsonNode security_metadata_SO_current,
			JsonNode security_metadata_of_the_SU, PermissionCacheObject cache,
			String idmHost, String idmUser, String idmPass,int idmPort) throws PDPServioticyException {

		boolean usedCacheObject = false;

		// Check if parameters to connect to IDM exist
	 	 PermissionCacheObject obj = new PermissionCacheObject();
	 	 if(idmUser==null || idmPass == null || idmHost ==null || idmPort<0) {
	 		throw new PDPServioticyException(500, "Not enough parameters to talk to the IDM ", "Not enough parameters to talk to IDM");
		 }
		
		 // Connect to IDM if no information is in the cach object
		 String response = null;
		 if (cache.getCache() == null) {		
		 	 // Check if user is allowed to get data from the SO
			 IDMCommunicator com = new IDMCommunicator(idmUser, idmPass, idmHost, idmPort);
			 try {
				 response = com.getInformationForUser(access_token_user);
				 cache.setCache(response);
			 }
			 catch (PDPServioticyException e){
				 obj.setPermission(false);
				 obj.setCache(e);
				 return obj;
			 }
		 }
		 else {
			usedCacheObject = true;
			response = cache.getCache().toString();
		}
		 // Check if response is correct (including exceptions		 
	 	 // Build a JsonNode
	    	ObjectMapper mapperUser = new ObjectMapper();
	    	JsonNode user_data = null;
		try {
			user_data = mapperUser.readTree(response);
		} catch (JsonProcessingException e1) {
			if (usedCacheObject == true){
				cache.setCache(null);
				verifyGetData(access_token_user, security_metadata_SO_current, security_metadata_of_the_SU, cache, idmHost, idmUser, idmPass, idmPort);
			}
			user_data = null;
		} catch (IOException e1) {
			if (usedCacheObject == true){
				cache.setCache(null);
				verifyGetData(access_token_user, security_metadata_SO_current, security_metadata_of_the_SU, cache, idmHost, idmUser, idmPass, idmPort);
			}
			user_data = null;
		}
		
		// Check policies
		JsonNode userSO = user_data.findValue(IDM_USER_SECTION);<commit_msg>IDM response fild changed to ID<commit_after>package de.passau.uni.sec.compose.pdp.servioticy.authz;




import java.io.IOException;
import java.util.List;
import java.lang.String;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import de.passau.uni.sec.compose.pdp.servioticy.PermissionCacheObject;
import de.passau.uni.sec.compose.pdp.servioticy.exception.PDPServioticyException;
import de.passau.uni.sec.compose.pdp.servioticy.idm.IDMCommunicator;

public class AuthorizationServioticy 
{
	private static final String IDM_USER_SECTION = "id";


	/**
	 * Verifies the policy for the currnt SO and a SU
	 *
	 * @param security_metadata_SO_current
	 * @param security_metadata_of_the_SU
	 * @param cache
	 * @param idmHost
	 * @param idmUser
	 * @param idmPass
	 * @param idmPort
	 * @return returns true or false in the "Permission" entry of the cach object depending on the policy evaluation
	 */
	public PermissionCacheObject verifyGetData( String access_token_user ,
			JsonNode security_metadata_SO_current,
			JsonNode security_metadata_of_the_SU, PermissionCacheObject cache,
			String idmHost, String idmUser, String idmPass,int idmPort) throws PDPServioticyException {

		boolean usedCacheObject = false;

		// Check if parameters to connect to IDM exist
	 	 PermissionCacheObject obj = new PermissionCacheObject();
	 	 if(idmUser==null || idmPass == null || idmHost ==null || idmPort<0) {
	 		throw new PDPServioticyException(500, "Not enough parameters to talk to the IDM ", "Not enough parameters to talk to IDM");
		 }
		
		 // Connect to IDM if no information is in the cach object
		 String response = null;
		 if (cache.getCache() == null) {		
		 	 // Check if user is allowed to get data from the SO
			 IDMCommunicator com = new IDMCommunicator(idmUser, idmPass, idmHost, idmPort);
			 try {
				 response = com.getInformationForUser(access_token_user);
				 cache.setCache(response);
			 }
			 catch (PDPServioticyException e){
				 obj.setPermission(false);
				 obj.setCache(e);
				 return obj;
			 }
		 }
		 else {
			usedCacheObject = true;
			response = cache.getCache().toString();
		}
		 // Check if response is correct (including exceptions		 
	 	 // Build a JsonNode
	    	ObjectMapper mapperUser = new ObjectMapper();
	    	JsonNode user_data = null;
		try {
			user_data = mapperUser.readTree(response);
		} catch (JsonProcessingException e1) {
			if (usedCacheObject == true){
				cache.setCache(null);
				return verifyGetData(access_token_user, security_metadata_SO_current, security_metadata_of_the_SU, cache, idmHost, idmUser, idmPass, idmPort);
			}
			user_data = null;
		} catch (IOException e1) {
			if (usedCacheObject == true){
				cache.setCache(null);
				return verifyGetData(access_token_user, security_metadata_SO_current, security_metadata_of_the_SU, cache, idmHost, idmUser, idmPass, idmPort);
			}
			user_data = null;
		}
		
		// Check policies
		JsonNode userSO = user_data.findValue(IDM_USER_SECTION);
{'fraction_non_alphanumeric': 0.08900855242318657, 'fraction_numerical': 0.0019005384859043396, 'mean_word_length': 4.201812191103789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987441', 'n_tokens_mistral': 2234, 'n_tokens_neox': 1927, 'n_words': 494}