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
This commit is contained in:
Massimiliano Assante 2016-11-17 10:19:01 +00:00
parent 7dca141d2f
commit 74dd7b1637
1 changed files with 19 additions and 5 deletions

View File

@ -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<String> 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;
}