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:
parent
7dca141d2f
commit
74dd7b1637
|
@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import org.apache.catalina.connector.Request;
|
import org.apache.catalina.connector.Request;
|
||||||
import org.apache.catalina.connector.Response;
|
import org.apache.catalina.connector.Response;
|
||||||
import org.apache.catalina.valves.ValveBase;
|
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.AuthorizationProvider;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
import org.gcube.common.authorization.library.provider.UserInfo;
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
||||||
|
@ -46,10 +47,18 @@ public class SmartGearsPortalValve extends ValveBase {
|
||||||
String scope = context.getCurrentScope(request);
|
String scope = context.getCurrentScope(request);
|
||||||
String username = getCurrentUsername(request);
|
String username = getCurrentUsername(request);
|
||||||
if (scope != null && username != null) {
|
if (scope != null && username != null) {
|
||||||
|
String userToken = null;
|
||||||
try {
|
try {
|
||||||
String userToken = getAuthorizationToken(username, scope);
|
ScopeProvider.instance.set(scope);
|
||||||
|
userToken = authorizationService().resolveTokenByUserAndContext(username, scope);
|
||||||
SecurityTokenProvider.instance.set(userToken);
|
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);
|
_log.error("Something went wrong in generating token for " + username + " in scope " + scope);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -66,11 +75,16 @@ public class SmartGearsPortalValve extends ValveBase {
|
||||||
* @param scope
|
* @param scope
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private static String getAuthorizationToken(String username, String scope) throws Exception {
|
private static String generateAuthorizationToken(String username, String scope) {
|
||||||
ScopeProvider.instance.set(scope);
|
|
||||||
List<String> userRoles = new ArrayList<>();
|
List<String> userRoles = new ArrayList<>();
|
||||||
userRoles.add(DEFAULT_ROLE);
|
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;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue