diff --git a/src/main/java/com/tecnalia/DVPolicyEngine/api/GetAccessApiController.java b/src/main/java/com/tecnalia/DVPolicyEngine/api/GetAccessApiController.java index 27818d65341f9eec68df30853f9087c8bf1a804d..af64428e12674622e5e5b307f897be67c271ced1 100644 --- a/src/main/java/com/tecnalia/DVPolicyEngine/api/GetAccessApiController.java +++ b/src/main/java/com/tecnalia/DVPolicyEngine/api/GetAccessApiController.java @@ -67,7 +67,7 @@ public class GetAccessApiController implements GetAccessApi { */ String organizationAttributesAsString=policyService.getDataSeekersOrganizationPolicy(dataSeekerID); - organizationAttributesAsString="{\"name\":\"Test Organization\",\"sector\":\"Test Sector\",\"continent\":\"EU\",\"country\":\"GRE\",\"type\":\"Private Entity\",\"size\":\"medium\",\"reputation\":\"7\"}"; + organizationAttributesAsString="{\"name\":\"Test Organization\",\"sector\":\"Test Sector\",\"continent\":\"EU\",\"country\":\"GRE\",\"type\":\"Private Entity\",\"size\":\"medium\",\"reputation\":7}"; System.out.println("organizationAttributesAsString:"+organizationAttributesAsString); diff --git a/src/main/java/com/tecnalia/DVPolicyEngine/service/PolicyService.java b/src/main/java/com/tecnalia/DVPolicyEngine/service/PolicyService.java index fcd44e462fe6e0be285a759113003c63abc333f6..f2488e3d867206e4b5bc12c37f983b924570f275 100644 --- a/src/main/java/com/tecnalia/DVPolicyEngine/service/PolicyService.java +++ b/src/main/java/com/tecnalia/DVPolicyEngine/service/PolicyService.java @@ -47,8 +47,8 @@ public class PolicyService { private String authClientSecret = ""; @Value("${datavaults.auth.grant.type}") private String authGrantType = ""; - @Value("${datavaults.auth.username}") - private String authUsername = ""; +/* @Value("${datavaults.auth.username}") + private String authUsername = "";*/ @Value("${datavaults.auth.password}") private String authPassword = ""; @@ -75,7 +75,8 @@ public class PolicyService { - organizationAttributesAsString =httpUtils.sendHttpPostRequestWithOAuth(datavaultsUrl,authServerUrl,authClientId,authClientSecret,authGrantType,authUsername,authPassword); + // organizationAttributesAsString =httpUtils.sendHttpPostRequestWithOAuth(datavaultsUrl,authServerUrl,authClientId,authClientSecret,authGrantType,authUsername,authPassword); + organizationAttributesAsString =httpUtils.sendHttpPostRequestWithOAuth(datavaultsUrl,authServerUrl,authClientId,authClientSecret,authGrantType,dataSeekerId,authPassword); } catch (Exception e) { @@ -133,119 +134,93 @@ public class PolicyService { JSONObject accessPolicyAsJSON = null; //Example - //organizationAttributesAsString:{"name":"Test Organization","sector":"Test Sector","continent":"EU","country":"GRE","type":"Private Entity","size":"medium","reputation":"7"} + //organizationAttributesAsString:{"name":"Test Organization","sector":"Test Sector","continent":"EU","country":"GRE","type":"Private Entity","size":"medium","reputation":7} //accessPolicyasString:{"Continent":["testContinent","test2Continent"],"Type":["testType","test2Type"],"Sector":["testSector","test2Sector"],"Size":["testSize","test2Size"],"Country":["testCountry","test2Country"],"Reputation":11} - - - - - // boolean result= this.checkParameter("continent", "Continent", organizationAttributesAsString, accessPolicyasString); - - - - - try{ - organizationAttributesJson = new JSONObject(organizationAttributesAsString); - - orgKeys = organizationAttributesJson.keys(); - - /* String sContinent=organizationAttributesJson.getString("continent"); - String sCountry=organizationAttributesJson.getString("country"); - String sSector=organizationAttributesJson.getString("sector"); - String sType=organizationAttributesJson.getString("type"); - String sSize=organizationAttributesJson.getString("size"); - String sReputation=organizationAttributesJson.getString("reputation");*/ - - System.out.println(organizationAttributesJson.toString()); - //sector,continent,country,type,size,reputation - } - catch(Exception e){ - System.out.println("Error"); - } + + try{ + organizationAttributesJson = new JSONObject(organizationAttributesAsString); + + orgKeys = organizationAttributesJson.keys(); + + /* String sContinent=organizationAttributesJson.getString("continent"); + String sCountry=organizationAttributesJson.getString("country"); + String sSector=organizationAttributesJson.getString("sector"); + String sType=organizationAttributesJson.getString("type"); + String sSize=organizationAttributesJson.getString("size"); + String sReputation=organizationAttributesJson.getString("reputation");*/ + + System.out.println(organizationAttributesJson.toString()); + //sector,continent,country,type,size,reputation + } + catch(Exception e){ + System.out.println("Error"); + } - try{ - accessPolicyAsJSON = new JSONObject(accessPolicyasString); - //policyKeys=accessPolicyAsJSON.keys(); - - /* JSONArray jArrayContinent=accessPolicyAsJSON.getJSONArray("Continent"); - JSONArray jArrayCountry=accessPolicyAsJSON.getJSONArray("Country - - JSONArray jArraySector=accessPolicyAsJSON.getJSONArray("Sector"); - JSONArray jArrayType=accessPolicyAsJSON.getJSONArray("Type"); - JSONArray jArraySize=accessPolicyAsJSON.getJSONArray("Size"); - JSONArray jArrayReputation=accessPolicyAsJSON.getJSONArray("Reputation");*/ - - - System.out.println(accessPolicyAsJSON.toString()); + try{ + accessPolicyAsJSON = new JSONObject(accessPolicyasString); + //policyKeys=accessPolicyAsJSON.keys(); + + /* JSONArray jArrayContinent=accessPolicyAsJSON.getJSONArray("Continent"); + JSONArray jArrayCountry=accessPolicyAsJSON.getJSONArray("Country + + JSONArray jArraySector=accessPolicyAsJSON.getJSONArray("Sector"); + JSONArray jArrayType=accessPolicyAsJSON.getJSONArray("Type"); + JSONArray jArraySize=accessPolicyAsJSON.getJSONArray("Size"); + JSONArray jArrayReputation=accessPolicyAsJSON.getJSONArray("Reputation");*/ + + + System.out.println(accessPolicyAsJSON.toString()); + } + catch(Exception e){ + System.out.println("Error"); + } + boolean result=false; + boolean bFind=false; + String orgKey=""; + String policyKey=""; + String sNonConformities=""; + while (orgKeys.hasNext()) { + orgKey = orgKeys.next(); + System.out.println("Key organization:"+orgKey); + String lowerOrgKey=orgKey.toLowerCase(); + + policyKeys=accessPolicyAsJSON.keys(); + while (policyKeys.hasNext()) { + policyKey = policyKeys.next(); + System.out.println("Key policy:"+policyKey); + + String lowerPolicyKey=policyKey.toLowerCase(); + + if (lowerOrgKey.equals(lowerPolicyKey)){ + result= this.checkParameter(orgKey, policyKey, organizationAttributesJson, accessPolicyAsJSON); + System.out.println(result); + bFind=true; + break; + } + } - catch(Exception e){ - System.out.println("Error"); + + + if ((!result)&&(bFind)){ + + sNonConformities+=" Organization key: "+ orgKey + " value not the same as policy key: "+ policyKey + " value."; } - boolean result=false; - String orgKey=""; - String policyKey=""; - String sNonConformities=""; - while (orgKeys.hasNext()) { - orgKey = orgKeys.next(); - System.out.println("Key organization:"+orgKey); - String lowerOrgKey=orgKey.toLowerCase(); - - policyKeys=accessPolicyAsJSON.keys(); - while (policyKeys.hasNext()) { - policyKey = policyKeys.next(); - System.out.println("Key policy:"+policyKey); - - String lowerPolicyKey=policyKey.toLowerCase(); - - if (lowerOrgKey.equals(lowerPolicyKey)){ - result= this.checkParameter(orgKey, policyKey, organizationAttributesJson, accessPolicyAsJSON); - System.out.println(result); - break; - } - - } - // policyKeys.next(); - - if (!result){ - - sNonConformities+="Organization key: "+ orgKey + " value not the same as policy key: "+ policyKey + " value.\n"; - } - - + + - } - access.setDatasetID(datasetID); - if (sNonConformities.equals("")){ - access.setGranted(true); - } - else access.setGranted(false); - access.setNonConformities(sNonConformities); - - - - - - - - - // boolean result= this.checkParameter("continent", "Continent", organizationAttributesJson, accessPolicyAsJSON); - - - - + } + access.setDatasetID(datasetID); + if (sNonConformities.equals("")){ + access.setGranted(true); + } + else access.setGranted(false); + access.setNonConformities(sNonConformities); //check if any value in policy is same of the organization, if everything is on policy return true in granted value and conformities string is empty //if one of the value is not the same return false and construct non conformities string - - - - - - - return access; } @@ -254,26 +229,41 @@ public class PolicyService { boolean result=false; - try{ - - - String sOrgValue=organizationAttributesJson.getString(parameterOrg); - + + String sOrgValue=""; + int iOrgValue=-1; + try{ + sOrgValue=organizationAttributesJson.getString(parameterOrg); System.out.println("Value for organization:"+sOrgValue); - // sOrgValue="testContinent"; + } + catch(Exception e){ + iOrgValue=organizationAttributesJson.getInt(parameterOrg); + System.out.println("Value for organization:"+iOrgValue); + } + + - + try{ JSONArray jArray=accessPolicyAsJSON.getJSONArray(parameterPolicy); - - if (jArray.toString().contains(sOrgValue)) - + + if (jArray.toString().contains(sOrgValue)){ + result=true; System.out.println("Is the same value?:"+result); - + } + } catch(Exception e){ - + int iValue=accessPolicyAsJSON.getInt(parameterPolicy); + if (iValue==iOrgValue){ + + result=true; + System.out.println("Is the same value?:"+result); + } + } + + return result; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0ac7cdfa40eee7dc00e967bad047c9b091a6a231..e80e72d1611651ae30e49e1efa4d8e493fabe78d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,5 +15,5 @@ datavaults.auth.server.url=https://datavaults-auth.euprojects.net/auth/realms/pl datavaults.auth.client.id=backend-service datavaults.auth.client.secret=04892d79-f600-4410-a9ac-6860aa5bdf3e datavaults.auth.grant.type=password -datavaults.auth.username=trainer +#datavaults.auth.username=trainer datavaults.auth.password=3 \ No newline at end of file