Commit 52ba207e authored by root's avatar root
Browse files

Add functionality to this method:

public String getDataSeekersOrganizationPolicy(String dataSeekerId) 
parent 94ad583d
...@@ -100,5 +100,8 @@ ...@@ -100,5 +100,8 @@
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20190722</version> <version>20190722</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -10,13 +10,19 @@ import com.tecnalia.DVPolicyEngine.model.Access; ...@@ -10,13 +10,19 @@ import com.tecnalia.DVPolicyEngine.model.Access;
import com.tecnalia.DVPolicyEngine.utils.HttpUtils; import com.tecnalia.DVPolicyEngine.utils.HttpUtils;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
/** /**
* *
...@@ -24,6 +30,25 @@ import org.springframework.stereotype.Service; ...@@ -24,6 +30,25 @@ import org.springframework.stereotype.Service;
*/ */
@Service @Service
public class PolicyService { public class PolicyService {
@Value("${datavaults.api.url}")
private String datavaultsUrl = "";
@Value("${datavaults.auth.server.url}")
private String authServerUrl = "";
@Value("${datavaults.auth.client.id}")
private String authClientId = "";
@Value("${datavaults.auth.client.secret}")
private String authClientSecret = "";
@Value("${datavaults.auth.grant.type}")
private String authGrantType = "";
@Value("${datavaults.auth.username}")
private String authUsername = "";
@Value("${datavaults.auth.password}")
private String authPassword = "";
private static final Logger log = LoggerFactory.getLogger(PolicyService.class); private static final Logger log = LoggerFactory.getLogger(PolicyService.class);
private final HttpUtils httpUtils; private final HttpUtils httpUtils;
...@@ -43,18 +68,10 @@ public class PolicyService { ...@@ -43,18 +68,10 @@ public class PolicyService {
String organizationAttributesAsString=""; String organizationAttributesAsString="";
try { try {
//connect usign Keycloak credentials url to GetDataSeekersOrganizationPolicy -> result organization attributes
//Aqui habrá que mirar cómo conectarse a Keycloak, sin con clientId o clientSecret, com user, password???, habrá que cambiar este método por otro
/* cape.enforce.usage.url=https://www.cape-suite.eu/cape-service-sdk/api/v2/services/consents/enforceUsageRules
cape.auth.server.url=https://www.cape-suite.eu/auth/realms/Cape/protocol/openid-connect/token
cape.auth.client.id=ids-usage-control
cape.auth.client.secret=579246ac-1adf-4c89-b94d-7f3613b3467b
cape.auth.grant.type=client_credentials
*/
organizationAttributesAsString =httpUtils.sendHttpPostRequestWithOAuthClientCredentials("url_adressAPI","authServerUrl","authClientId","authClientSecret","authGrantType"); organizationAttributesAsString =httpUtils.sendHttpPostRequestWithOAuth(datavaultsUrl,authServerUrl,authClientId,authClientSecret,authGrantType,authUsername,authPassword);
} catch (Exception e) { } catch (Exception e) {
...@@ -132,4 +149,9 @@ cape.auth.grant.type=client_credentials ...@@ -132,4 +149,9 @@ cape.auth.grant.type=client_credentials
} }
} }
...@@ -256,12 +256,19 @@ public class HttpUtils { ...@@ -256,12 +256,19 @@ public class HttpUtils {
* @param authClientId * @param authClientId
* @param authClientSecret * @param authClientSecret
* @param authGrantType * @param authGrantType
* @param usernmae
* @param passwrod
* @return the HTTP response if HTTP code is OK (200). * @return the HTTP response if HTTP code is OK (200).
* @throws URISyntaxException if the input address is not a valid URI. * @throws URISyntaxException if the input address is not a valid URI.
* @throws RuntimeException if an error occurred when connecting or processing the HTTP * @throws RuntimeException if an error occurred when connecting or processing the HTTP
* request. * request.
*/ */
public String sendHttpPostRequestWithOAuthClientCredentials(String address,String authServerUrl, String authClientId, String authClientSecret, String authGrantType) throws
public String sendHttpPostRequestWithOAuth(String address,String authServerUrl, String authClientId, String authClientSecret, String authGrantType,String username,String password) throws
RuntimeException, URISyntaxException { RuntimeException, URISyntaxException {
//Get access token from Auth. Server //Get access token from Auth. Server
...@@ -269,6 +276,11 @@ public class HttpUtils { ...@@ -269,6 +276,11 @@ public class HttpUtils {
authParams.put("client_id", authClientId); authParams.put("client_id", authClientId);
authParams.put("client_secret", authClientSecret); authParams.put("client_secret", authClientSecret);
authParams.put("grant_type", authGrantType); authParams.put("grant_type", authGrantType);
authParams.put("username", username);
authParams.put("password", password);
String accessTokenJsonStr = sendHttpPostRequestFormEncoded(authServerUrl, authParams); String accessTokenJsonStr = sendHttpPostRequestFormEncoded(authServerUrl, authParams);
...@@ -332,7 +344,7 @@ public class HttpUtils { ...@@ -332,7 +344,7 @@ public class HttpUtils {
//Get access token from Auth. Server //Get access token from Auth. Server
Map<String,String>authParams = new HashMap<String, String>(); Map<String,String>authParams = new HashMap<String, String>();
authParams.put("username", username); authParams.put("username", username);
authParams.put("username", password); authParams.put("password", password);
authParams.put("grant_type", authGrantType); authParams.put("grant_type", authGrantType);
String accessTokenJsonStr = sendHttpPostRequestFormEncoded(authServerUrl, authParams); String accessTokenJsonStr = sendHttpPostRequestFormEncoded(authServerUrl, authParams);
JSONObject accessTokenJson = new JSONObject(accessTokenJsonStr); JSONObject accessTokenJson = new JSONObject(accessTokenJsonStr);
......
...@@ -6,4 +6,14 @@ spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false ...@@ -6,4 +6,14 @@ spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
springdoc.swagger-ui.path=/swagger-ui.html springdoc.swagger-ui.path=/swagger-ui.html
\ No newline at end of file
datavaults.api.url=https://datavaults-cloud.euprojects.net/api/v1/group
datavaults.auth.server.url=https://datavaults-auth.euprojects.net/auth/realms/platform/protocol/openid-connect/token
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.password=3
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment