Search in sources :

Example 1 with TrustedClientService

use of com.haulmont.cuba.security.app.TrustedClientService in project cuba by cuba-platform.

the class CubaRemoteInvocationExecutor method invoke.

@Override
public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (invocation instanceof CubaRemoteInvocation) {
        CubaRemoteInvocation cubaInvocation = (CubaRemoteInvocation) invocation;
        UUID sessionId = cubaInvocation.getSessionId();
        if (sessionId != null) {
            UserSession session = userSessions.getAndRefresh(sessionId);
            if (session == null) {
                ServerConfig serverConfig = configuration.getConfig(ServerConfig.class);
                String sessionProviderUrl = serverConfig.getUserSessionProviderUrl();
                if (StringUtils.isNotBlank(sessionProviderUrl)) {
                    log.debug("User session {} not found, trying to get it from {}", sessionId, sessionProviderUrl);
                    try {
                        HttpServiceProxy proxyFactory = new HttpServiceProxy(getServerSelector(sessionProviderUrl));
                        proxyFactory.setServiceUrl("cuba_TrustedClientService");
                        proxyFactory.setServiceInterface(TrustedClientService.class);
                        proxyFactory.afterPropertiesSet();
                        TrustedClientService trustedClientService = (TrustedClientService) proxyFactory.getObject();
                        if (trustedClientService != null) {
                            UserSession userSession = trustedClientService.findSession(serverConfig.getTrustedClientPassword(), sessionId);
                            if (userSession != null) {
                                userSessions.add(userSession);
                            } else {
                                log.debug("User session {} not found on {}", sessionId, sessionProviderUrl);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Error getting user session from {}", sessionProviderUrl, e);
                    }
                }
            }
            AppContext.setSecurityContext(new SecurityContext(sessionId));
        }
        if (cubaInvocation.getLocale() != null) {
            Locale requestLocale = Locale.forLanguageTag(cubaInvocation.getLocale());
            if (!globalConfig.getAvailableLocales().containsValue(requestLocale)) {
                requestLocale = null;
            }
            UserInvocationContext.setRequestScopeInfo(sessionId, requestLocale, cubaInvocation.getTimeZone(), cubaInvocation.getAddress(), cubaInvocation.getClientInfo());
        }
    }
    Object result;
    try {
        result = invocation.invoke(targetObject);
    } finally {
        AppContext.setSecurityContext(null);
        UserInvocationContext.clearRequestScopeInfo();
    }
    return result;
}
Also used : Locale(java.util.Locale) ServerConfig(com.haulmont.cuba.core.app.ServerConfig) TrustedClientService(com.haulmont.cuba.security.app.TrustedClientService) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with TrustedClientService

use of com.haulmont.cuba.security.app.TrustedClientService in project cuba by cuba-platform.

the class ControllerUtils method getUserSession.

public static UserSession getUserSession(HttpServletRequest req) {
    String s = req.getParameter("s");
    if (s != null) {
        try {
            UUID id = UUID.fromString(s);
            WebAuthConfig webAuthConfig = AppBeans.get(Configuration.class).getConfig(WebAuthConfig.class);
            TrustedClientService trustedClientService = AppBeans.get(TrustedClientService.NAME);
            UserSession session = trustedClientService.findSession(webAuthConfig.getTrustedClientPassword(), id);
            if (session != null) {
                req.getSession().setAttribute(App.USER_SESSION_ATTR, session);
                return session;
            } else {
                return null;
            }
        } catch (Exception e) {
            log.warn("Unable to get session from Login Service", e);
            return null;
        }
    } else {
        // noinspection UnnecessaryLocalVariable
        UserSession userSession = (UserSession) req.getSession().getAttribute(App.USER_SESSION_ATTR);
        return userSession;
    }
}
Also used : TrustedClientService(com.haulmont.cuba.security.app.TrustedClientService) Configuration(com.haulmont.cuba.core.global.Configuration) WebAuthConfig(com.haulmont.cuba.web.auth.WebAuthConfig) UserSession(com.haulmont.cuba.security.global.UserSession) UUID(java.util.UUID) MalformedURLException(java.net.MalformedURLException)

Aggregations

TrustedClientService (com.haulmont.cuba.security.app.TrustedClientService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 UUID (java.util.UUID)2 ServerConfig (com.haulmont.cuba.core.app.ServerConfig)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 WebAuthConfig (com.haulmont.cuba.web.auth.WebAuthConfig)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 Locale (java.util.Locale)1