diff --git a/src/main/java/org/gcube/portal/threadlocalexec/SmartGearsPortalValve.java b/src/main/java/org/gcube/portal/threadlocalexec/SmartGearsPortalValve.java index 2782a46..eb1422e 100644 --- a/src/main/java/org/gcube/portal/threadlocalexec/SmartGearsPortalValve.java +++ b/src/main/java/org/gcube/portal/threadlocalexec/SmartGearsPortalValve.java @@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.UserInfo; @@ -46,10 +47,18 @@ public class SmartGearsPortalValve extends ValveBase { String scope = context.getCurrentScope(request); String username = getCurrentUsername(request); if (scope != null && username != null) { + String userToken = null; try { - String userToken = getAuthorizationToken(username, scope); + ScopeProvider.instance.set(scope); + userToken = authorizationService().resolveTokenByUserAndContext(username, scope); SecurityTokenProvider.instance.set(userToken); - } catch (Exception e) { + } + 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("Something went wrong in generating token for " + username + " in scope " + scope); e.printStackTrace(); } @@ -66,11 +75,16 @@ public class SmartGearsPortalValve extends ValveBase { * @param scope * @throws Exception */ - private static String getAuthorizationToken(String username, String scope) throws Exception { - ScopeProvider.instance.set(scope); + private static String generateAuthorizationToken(String username, String scope) { List userRoles = new ArrayList<>(); userRoles.add(DEFAULT_ROLE); - String token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope); + String token; + try { + token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope); + } catch (Exception e) { + e.printStackTrace(); + return null; + } return token; }