added method to generate token only if it doesnt exists

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@134275 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-11-17 10:15:23 +00:00
parent e6bbddf015
commit f18c1d47e7
1 changed files with 28 additions and 14 deletions

View File

@ -11,6 +11,8 @@ import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.provider.UserInfo;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
@ -259,9 +261,18 @@ public class PortalContext {
if (groupIdNo != null) {
String scope = getCurrentScope(httpServletRequest);
String username = getCurrentUser(httpServletRequest).getUsername();
String userToken = null;
try {
return getAuthorizationToken(username, scope);
} catch (Exception e) {
ScopeProvider.instance.set(scope);
userToken = authorizationService().resolveTokenByUserAndContext(username, scope);
SecurityTokenProvider.instance.set(userToken);
}
catch (ObjectNotFound ex) {
userToken = generateAuthorizationToken(username, scope);
SecurityTokenProvider.instance.set(userToken);
_log.debug("generateAuthorizationToken OK for " + username + " in scope " + scope);
}
catch (Exception e) {
_log.error("Error while trying to generate token for user " + username + "in scope " + scope);
e.printStackTrace();
return null;
@ -285,13 +296,16 @@ public class PortalContext {
* @param scope
* @throws Exception
*/
private static String getAuthorizationToken(String username, String scope) throws Exception {
ScopeProvider.instance.set(scope);
_log.debug("calling service token on scope " + scope);
private static String generateAuthorizationToken(String username, String scope) {
List<String> userRoles = new ArrayList<>();
userRoles.add(DEFAULT_ROLE);
String token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
_log.debug("received token: "+token);
String token;
try {
token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return token;
}
/**