Lucio Lelii 2017-02-27 16:57:50 +00:00
parent 58c25be50f
commit 5aa5300cc0
2 changed files with 17 additions and 1 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.common</groupId>
<artifactId>common-authorization</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.0.2-SNAPSHOT</version>
<name>authorization service common library</name>
<parent>

View File

@ -5,10 +5,15 @@ import java.util.concurrent.Callable;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuthorizedTasks {
private static Logger logger= LoggerFactory.getLogger(AuthorizedTasks.class);
/**
* Binds a {@link Callable} task to the current scope and user.
* @param task the task
@ -21,18 +26,24 @@ public class AuthorizedTasks {
final String token = SecurityTokenProvider.instance.get();
final String scope = ScopeProvider.instance.get();
return new Callable<V>() {
@Override
public V call() throws Exception {
AuthorizationProvider.instance.set(userCall);
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(scope);
try {
logger.info("setting on authorized task scope {} and token {}", scope, token);
return task.call();
}
finally {
AuthorizationProvider.instance.reset();
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
}
@ -51,17 +62,22 @@ public class AuthorizedTasks {
final String token = SecurityTokenProvider.instance.get();
final String scope = ScopeProvider.instance.get();
return new Runnable() {
@Override
public void run() {
AuthorizationProvider.instance.set(userCall);
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(scope);
try {
logger.info("setting on authorized task scope {} and token {}", scope, token);
task.run();
}
finally {
AuthorizationProvider.instance.reset();
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
}