2016-04-01 13:09:40 +02:00
|
|
|
package org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mapping;
|
|
|
|
|
2016-09-27 15:44:14 +02:00
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2022-04-05 18:20:49 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
2016-09-27 15:44:14 +02:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
2022-04-05 18:20:49 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
|
2016-04-01 13:09:40 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2016-09-27 15:44:14 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2022-04-05 18:20:49 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.dataanalysis.wps.statisticalmanager.synchserver.is.InformationSystemUtils;
|
2017-05-05 10:08:44 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
public class TokenManager {
|
|
|
|
|
2022-04-05 18:20:49 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(TokenManager.class);
|
2017-05-05 10:08:44 +02:00
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
String username;
|
|
|
|
String scope;
|
2016-09-27 15:44:14 +02:00
|
|
|
String token;
|
2017-09-19 17:19:27 +02:00
|
|
|
String tokenQualifier;
|
2022-04-05 18:20:49 +02:00
|
|
|
|
|
|
|
public String getScope() {
|
2016-04-01 13:09:40 +02:00
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:20:49 +02:00
|
|
|
public String getUserName() {
|
2016-04-01 13:09:40 +02:00
|
|
|
return username;
|
|
|
|
}
|
2017-05-05 10:08:44 +02:00
|
|
|
|
2022-04-05 18:20:49 +02:00
|
|
|
public String getToken() {
|
2016-09-27 15:44:14 +02:00
|
|
|
return token;
|
|
|
|
}
|
2022-04-05 18:20:49 +02:00
|
|
|
|
2017-09-19 17:19:27 +02:00
|
|
|
public String getTokenQualifier() {
|
|
|
|
return tokenQualifier;
|
|
|
|
}
|
2017-05-05 10:08:44 +02:00
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
public void getCredentials() {
|
2022-04-05 18:20:49 +02:00
|
|
|
try {
|
|
|
|
LOGGER.info("Retrieving token credentials");
|
|
|
|
// get username from SmartGears
|
2017-05-05 10:08:44 +02:00
|
|
|
username = AuthorizationProvider.instance.get().getClient().getId();
|
2016-09-27 15:44:14 +02:00
|
|
|
token = SecurityTokenProvider.instance.get();
|
2022-04-05 18:20:49 +02:00
|
|
|
if (token == null || token.isEmpty()) {
|
|
|
|
String jwtToken = AccessTokenProvider.instance.get();
|
|
|
|
scope = ScopeProvider.instance.get();
|
|
|
|
token = getGcubeTokenFromUriResolver(jwtToken, scope);
|
|
|
|
}
|
2016-09-27 15:44:14 +02:00
|
|
|
AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
scope = entry.getContext();
|
2017-09-19 17:19:27 +02:00
|
|
|
tokenQualifier = entry.getQualifier();
|
2022-04-05 18:20:49 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.error("Error Retrieving token credentials: "+e.getLocalizedMessage(),e);
|
2017-05-05 10:08:44 +02:00
|
|
|
scope = null;
|
2022-04-05 18:20:49 +02:00
|
|
|
username = null;
|
2017-05-05 10:08:44 +02:00
|
|
|
|
|
|
|
}
|
2022-04-05 18:20:49 +02:00
|
|
|
if ((scope == null || username == null) && ConfigurationManager.isSimulationMode()) {
|
2017-05-05 10:08:44 +02:00
|
|
|
scope = ConfigurationManager.defaultScope;
|
|
|
|
username = ConfigurationManager.defaultUsername;
|
|
|
|
}
|
2022-04-05 18:20:49 +02:00
|
|
|
LOGGER.info("Retrieved scope: {} Username: {} Token {} SIMULATION MODE: {} ", scope, username, token,
|
|
|
|
ConfigurationManager.isSimulationMode());
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:20:49 +02:00
|
|
|
public String getGcubeTokenFromUriResolver(String jwtToken, String scope) throws Exception {
|
|
|
|
String gcubeToken = null;
|
|
|
|
String uriResolverOatURL = InformationSystemUtils.retrieveUriResolverOat(scope);
|
|
|
|
try {
|
|
|
|
LOGGER.info("Create Request: "+ uriResolverOatURL);
|
|
|
|
URL urlObj = new URL(uriResolverOatURL);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.setRequestProperty("Authorization", "Bearer " + jwtToken);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
try (AutoCloseable conc = () -> connection.disconnect()) {
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
LOGGER.info("Response Code: " + responseCode);
|
|
|
|
|
|
|
|
if (Response.Status.fromStatusCode(responseCode).compareTo(Response.Status.OK) == 0) {
|
|
|
|
try (InputStream ins = connection.getInputStream();
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(ins))) {
|
|
|
|
String inputLine = null;
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gcubeToken = inputLine;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String error = "Invalid Response Code retrieving GCube Token from Uri Resolver: " + responseCode;
|
|
|
|
LOGGER.error(error);
|
|
|
|
try (InputStream ins = connection.getErrorStream();
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(ins))) {
|
|
|
|
String inputLine = null;
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
|
|
LOGGER.error(inputLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Exception(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error("Error retrieving GcubeToken from Uri Resolver: "+e.getLocalizedMessage());
|
|
|
|
e.printStackTrace();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
LOGGER.info("Retrieved GcubeToken: "+gcubeToken);
|
|
|
|
return gcubeToken;
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
}
|