2018-11-30 14:50:11 +01:00
|
|
|
package org.gcube.authorization;
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2019-02-20 14:51:38 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
2018-12-05 17:45:06 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2019-02-20 14:51:38 +01:00
|
|
|
import org.gcube.common.authorization.client.Binder;
|
2018-12-05 17:45:06 +01:00
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
2018-11-30 14:50:11 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.ContainerInfo;
|
2018-12-05 17:45:06 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
2018-11-30 14:50:11 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ContextAuthorizationTest extends ContextTest {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ContextAuthorizationTest.class);
|
|
|
|
|
|
|
|
@Test
|
2018-12-05 17:45:06 +01:00
|
|
|
public void getInfo() throws Exception {
|
|
|
|
ContextTest.setContext("");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void generateApplicationToken() throws Exception {
|
|
|
|
ContextTest.setContext(ROOT);
|
|
|
|
|
|
|
|
String context = "/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev";
|
|
|
|
UserInfo userInfo = new UserInfo("luca.frosini",new ArrayList<>());
|
|
|
|
String userToken = authorizationService().generateUserToken(userInfo, context);
|
|
|
|
SecurityTokenProvider.instance.set(userToken);
|
|
|
|
|
2018-12-07 17:49:48 +01:00
|
|
|
String applicationName = "nagios.check";
|
2018-11-30 14:50:11 +01:00
|
|
|
String generatedToken = authorizationService().generateExternalServiceToken(applicationName);
|
|
|
|
|
|
|
|
logger.trace("Application Token for application {} in Context {} is {}", applicationName,
|
|
|
|
ContextUtility.getCurrentContext(), generatedToken);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-12-05 17:45:06 +01:00
|
|
|
public void generateHostToken() throws Exception {
|
2018-11-30 14:50:11 +01:00
|
|
|
String currentContext = ContextUtility.getCurrentContext();
|
|
|
|
String host = "pc-frosini.isti.cnr.it";
|
|
|
|
int port = 8080;
|
|
|
|
|
|
|
|
ContainerInfo containerInfo = new ContainerInfo(host, port);
|
|
|
|
String tokenNode = authorizationService().requestActivation(containerInfo, currentContext);
|
|
|
|
|
|
|
|
logger.trace("Token for Context {} for host {}:{} is {}", currentContext, host, port, tokenNode);
|
|
|
|
|
|
|
|
}
|
2018-12-05 17:45:06 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void generateUserToken() throws Exception {
|
|
|
|
String currentContext = ContextUtility.getCurrentContext();
|
|
|
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(SecurityTokenProvider.instance.get());
|
|
|
|
UserInfo userInfo = (UserInfo) authorizationEntry.getClientInfo();
|
|
|
|
String tokenNode = authorizationService().generateUserToken(userInfo, currentContext);
|
|
|
|
logger.trace("Token for Context {} for {} is {}", currentContext, userInfo.getId(), tokenNode);
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:51:38 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void seriliazeUser() throws Exception {
|
|
|
|
OutputStream output = new OutputStream()
|
|
|
|
{
|
|
|
|
private StringBuilder string = new StringBuilder();
|
|
|
|
@Override
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
this.string.append((char) b );
|
|
|
|
}
|
|
|
|
|
|
|
|
//Netbeans IDE automatically overrides this toString()
|
|
|
|
public String toString(){
|
|
|
|
return this.string.toString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UserInfo userInfo = new UserInfo("luca.frosini", new ArrayList<>());
|
|
|
|
Binder.getContext().createMarshaller().marshal(userInfo, output);
|
|
|
|
|
|
|
|
|
|
|
|
logger.debug("{}", output.toString());
|
|
|
|
}
|
2018-11-30 14:50:11 +01:00
|
|
|
}
|