From 74dd7b1637cd2c3d31b0b5b1d35a832f0fb14f35 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 17 Nov 2016 10:19:01 +0000 Subject: [PATCH] added method to generate token only if it doesnt exists git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/threadlocal-vars-cleaner@134277 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../SmartGearsPortalValve.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) 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; }