Supporting new UMA token

This commit is contained in:
Luca Frosini 2021-05-27 15:14:24 +02:00
parent 60b9690890
commit a26f583a0e
4 changed files with 37 additions and 12 deletions

View File

@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [v1.2.0-SNAPSHOT] [r5.0.0] -
- Managing new UMA token and not only old authz gcube token [#21525]
- Switched JSON management to gcube-jackson [#19737]

View File

@ -14,6 +14,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.slf4j.Logger;
@ -119,16 +120,29 @@ public class GXConnection {
return send(this.buildURL(), method);
}
private HttpURLConnection addGCubeAuthorizationToken(HttpURLConnection uConn) throws Exception {
if (!this.extCall) {
String oldGcubeAuthtoken = SecurityTokenProvider.instance.get();
if (Objects.isNull(oldGcubeAuthtoken) || oldGcubeAuthtoken.isEmpty()) {
String umaToken = AccessTokenProvider.instance.get();
if(Objects.isNull(umaToken) || umaToken.isEmpty()) {
throw new IllegalStateException("The security token in the current environment is null.");
}else {
uConn.setRequestProperty("Authorization", "Bearer " + umaToken);
}
} else {
uConn.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, oldGcubeAuthtoken);
}
}
return uConn;
}
private HttpURLConnection send(URL url, HTTPMETHOD method) throws Exception {
HttpURLConnection uConn = (HttpURLConnection) url.openConnection();
if (!this.extCall) {
String token = SecurityTokenProvider.instance.get();
if (Objects.isNull(token) || token.isEmpty())
throw new IllegalStateException("The security token in the current environment is null.");
uConn.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
token);
}
uConn = addGCubeAuthorizationToken(uConn);
uConn.setDoOutput(true);
// uConn.setRequestProperty("Content-type", APPLICATION_JSON_CHARSET_UTF_8);
if(this.agent!=null) {

View File

@ -5,6 +5,7 @@ import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.Map.Entry;
import org.gcube.common.gxhttp.reference.GXConnection.HTTPMETHOD;
@ -15,6 +16,7 @@ import org.slf4j.LoggerFactory;
* Builder for GXHTTP Requests.
*
* @author Manuele Simi (ISTI CNR)
* @author Luca Frosini (ISTI-CNR)
*
*/
public class GXHTTPRequestBuilder {
@ -74,6 +76,8 @@ public class GXHTTPRequestBuilder {
return this;
}
public static final String UUID_REGEX = "^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}-[a-fA-F0-9]{8,9}){1}$";
/**
* Overrides the default security token.
@ -81,10 +85,15 @@ public class GXHTTPRequestBuilder {
* @param token
*/
public void setSecurityToken(String token) {
if (!this.connection.isExtCall())
this.connection.setProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, token);
else
if (!this.connection.isExtCall()) {
if(Pattern.matches(UUID_REGEX, token)) {
this.connection.setProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY, token);
}else {
this.connection.setProperty("Authorization", "Bearer " + token);
}
}else {
throw new UnsupportedOperationException("Cannot set the security token on an external call");
}
}
/**

View File

@ -1,6 +1,7 @@
package org.gcube.common.gxhttp;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;