2017-12-11 15:48:34 +01:00
|
|
|
package org.gcube.data.transfer.service.transfers.engine.impl;
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2018-02-23 10:45:03 +01:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2017-12-11 15:48:34 +01:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class TokenUtils {
|
|
|
|
|
|
|
|
public static String getCurrentScope(){
|
|
|
|
// try{
|
|
|
|
// String token=SecurityTokenProvider.instance.get();
|
|
|
|
// log.debug("Token is : "+token);
|
|
|
|
// if(token==null) throw new Exception("Security Token is null");
|
|
|
|
// AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
// return entry.getContext();
|
|
|
|
// }catch(Exception e ){
|
|
|
|
// log.debug("Unable to resolve token, checking scope provider..",e);
|
|
|
|
// return ScopeProvider.instance.get();
|
|
|
|
// }
|
|
|
|
|
|
|
|
String scope=ScopeProvider.instance.get();
|
|
|
|
if(scope!=null) {
|
|
|
|
log.debug("Found scope provider {}, skipping token",scope);
|
|
|
|
return scope;
|
|
|
|
}else{
|
|
|
|
try{
|
|
|
|
log.debug("Scope provider not set, reverting to token");
|
|
|
|
String token=SecurityTokenProvider.instance.get();
|
|
|
|
log.debug("Token is : "+token);
|
|
|
|
if(token==null) throw new Exception("Security Token is null");
|
|
|
|
AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
return entry.getContext();
|
|
|
|
}catch(Exception e){
|
|
|
|
throw new RuntimeException("Unable to evaluate scope ",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-23 10:45:03 +01:00
|
|
|
|
|
|
|
public static String getCurrentUser() throws ObjectNotFound, Exception {
|
|
|
|
String token=SecurityTokenProvider.instance.get();
|
|
|
|
return authorizationService().get(token).getClientInfo().getId();
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:48:34 +01:00
|
|
|
}
|