2015-05-29 18:32:49 +02:00
|
|
|
package org.gcube.common.authorization.library;
|
|
|
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2021-05-24 16:30:18 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
|
2015-05-29 18:32:49 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2016-01-22 17:28:11 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-09-08 12:54:49 +02:00
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
2017-02-27 17:57:50 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-29 18:32:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
public class AuthorizedTasks {
|
|
|
|
|
2017-02-27 17:57:50 +01:00
|
|
|
private static Logger logger= LoggerFactory.getLogger(AuthorizedTasks.class);
|
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
/**
|
|
|
|
* Binds a {@link Callable} task to the current scope and user.
|
|
|
|
* @param task the task
|
|
|
|
* @return an equivalent {@link Callable} task bound to the current scope and user
|
|
|
|
*/
|
|
|
|
static public <V> Callable<V> bind(final Callable<V> task) {
|
|
|
|
|
|
|
|
|
2016-09-08 12:54:49 +02:00
|
|
|
final Caller userCall = AuthorizationProvider.instance.get();
|
2016-01-22 17:28:11 +01:00
|
|
|
|
|
|
|
final String token = SecurityTokenProvider.instance.get();
|
2015-05-29 18:32:49 +02:00
|
|
|
|
2017-02-27 17:57:50 +01:00
|
|
|
final String scope = ScopeProvider.instance.get();
|
|
|
|
|
2021-05-24 16:30:18 +02:00
|
|
|
final String accessToken = AccessTokenProvider.instance.get();
|
2020-05-29 17:23:37 +02:00
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
return new Callable<V>() {
|
|
|
|
@Override
|
|
|
|
public V call() throws Exception {
|
|
|
|
|
2020-05-29 17:23:37 +02:00
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
AuthorizationProvider.instance.set(userCall);
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
2017-02-27 17:57:50 +01:00
|
|
|
ScopeProvider.instance.set(scope);
|
2021-05-24 16:30:18 +02:00
|
|
|
AccessTokenProvider.instance.set(accessToken);
|
2017-02-27 17:57:50 +01:00
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
try {
|
2017-02-27 17:57:50 +01:00
|
|
|
logger.info("setting on authorized task scope {} and token {}", scope, token);
|
2015-05-29 18:32:49 +02:00
|
|
|
return task.call();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
AuthorizationProvider.instance.reset();
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.reset();
|
2017-02-27 17:57:50 +01:00
|
|
|
ScopeProvider.instance.reset();
|
2015-05-29 18:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds a {@link Runnable} task to the current scope and user.
|
|
|
|
* @param task the task
|
|
|
|
* @return an equivalent {@link Runnable} task bound to the current scope and user
|
|
|
|
*/
|
|
|
|
static public <V> Runnable bind(final Runnable task) {
|
|
|
|
|
|
|
|
|
2016-09-08 12:54:49 +02:00
|
|
|
final Caller userCall = AuthorizationProvider.instance.get();
|
2016-01-22 17:28:11 +01:00
|
|
|
|
|
|
|
final String token = SecurityTokenProvider.instance.get();
|
2015-05-29 18:32:49 +02:00
|
|
|
|
2017-02-27 17:57:50 +01:00
|
|
|
final String scope = ScopeProvider.instance.get();
|
|
|
|
|
2021-05-24 16:30:18 +02:00
|
|
|
final String accessToken = AccessTokenProvider.instance.get();
|
2020-05-29 17:23:37 +02:00
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
return new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
AuthorizationProvider.instance.set(userCall);
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
2017-02-27 17:57:50 +01:00
|
|
|
ScopeProvider.instance.set(scope);
|
2021-05-24 16:30:18 +02:00
|
|
|
AccessTokenProvider.instance.set(accessToken);
|
2020-05-29 17:23:37 +02:00
|
|
|
|
2015-05-29 18:32:49 +02:00
|
|
|
try {
|
2017-02-27 17:57:50 +01:00
|
|
|
logger.info("setting on authorized task scope {} and token {}", scope, token);
|
2015-05-29 18:32:49 +02:00
|
|
|
task.run();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
AuthorizationProvider.instance.reset();
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.reset();
|
2017-02-27 17:57:50 +01:00
|
|
|
ScopeProvider.instance.reset();
|
2015-05-29 18:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|