Search in sources :

Example 36 with SecurityContext

use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.

the class AuthenticationManagerBean method logout.

@Override
public void logout() {
    try {
        UserSession session = userSessionSource.getUserSession();
        userSessions.remove(session);
        log.info("Logged out: {}", session);
        withSecurityContext(new SecurityContext(serverSession), () -> publishUserLoggedOut(session));
    } catch (SecurityException e) {
        log.warn("Couldn't logout: {}", e);
    } catch (NoUserSessionException e) {
        log.warn("NoUserSessionException thrown on logout");
    }
}
Also used : SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.withSecurityContext(com.haulmont.cuba.core.sys.AppContext.withSecurityContext)

Example 37 with SecurityContext

use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.

the class AuthenticationManagerBean method login.

@Override
@Nonnull
public AuthenticationDetails login(Credentials credentials) throws LoginException {
    checkNotNullArgument(credentials, "credentials should not be null");
    SecurityContext previousSecurityContext = AppContext.getSecurityContext();
    AppContext.setSecurityContext(new SecurityContext(serverSession));
    AuthenticationDetails authenticationDetails = null;
    try {
        try (Transaction tx = persistence.createTransaction()) {
            publishBeforeLoginEvent(credentials);
            authenticationDetails = authenticateInternal(credentials);
            tx.commit();
            userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession());
            setTimeZone(credentials, authenticationDetails);
            setSessionAttributes(credentials, authenticationDetails);
            storeSession(credentials, authenticationDetails);
            log.info("Logged in: {}", authenticationDetails.getSession());
            publishUserLoggedInEvent(credentials, authenticationDetails);
            return authenticationDetails;
        } finally {
            publishAfterLoginEvent(credentials, authenticationDetails);
        }
    } finally {
        AppContext.setSecurityContext(previousSecurityContext);
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.withSecurityContext(com.haulmont.cuba.core.sys.AppContext.withSecurityContext) Nonnull(javax.annotation.Nonnull)

Example 38 with SecurityContext

use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.

the class FileDownloadController method getSession.

protected UserSession getSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UUID sessionId;
    try {
        sessionId = UUID.fromString(request.getParameter("s"));
    } catch (Exception e) {
        return null;
    }
    AppContext.setSecurityContext(new SecurityContext(sessionId));
    try {
        UserSession userSession = userSessionService.getUserSession(sessionId);
        return userSession;
    } catch (NoUserSessionException e) {
        return null;
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 39 with SecurityContext

use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.

the class IdpLogoutCallbackController method logout.

@RequestMapping(value = "logout", method = RequestMethod.POST)
public void logout(@RequestParam(name = "idpSessionId") String idpSessionId, @RequestParam(name = "trustedServicePassword") String trustedServicePassword, HttpServletResponse response) {
    if ((!webAuthConfig.getExternalAuthentication() && !webIdpConfig.getIdpEnabled()) || Strings.isNullOrEmpty(webIdpConfig.getIdpBaseURL())) {
        log.warn("IDP options is not set, but logout callback url is requested");
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return;
    }
    if (!Objects.equals(webIdpConfig.getIdpTrustedServicePassword(), trustedServicePassword)) {
        log.warn("Incorrect trusted service password passed from IDP");
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return;
    }
    UserSession systemSession;
    try {
        systemSession = trustedClientService.getSystemSession(webAuthConfig.getTrustedClientPassword());
    } catch (LoginException e) {
        log.error("Unable to obtain system session", e);
        return;
    }
    log.trace("Logout user session by IDP session {}", idpSessionId);
    AppContext.withSecurityContext(new SecurityContext(systemSession), () -> idpService.logoutUserSession(idpSessionId));
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) LoginException(com.haulmont.cuba.security.global.LoginException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 40 with SecurityContext

use of com.haulmont.cuba.core.sys.SecurityContext in project cuba by cuba-platform.

the class FileUploadController method getSession.

protected UserSession getSession(HttpServletRequest request, HttpServletResponse response) {
    UUID sessionId;
    try {
        sessionId = UUID.fromString(request.getParameter("s"));
    } catch (Exception e) {
        return null;
    }
    AppContext.setSecurityContext(new SecurityContext(sessionId));
    try {
        return userSessionService.getUserSession(sessionId);
    } catch (NoUserSessionException e) {
        return null;
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Aggregations

SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)43 UserSession (com.haulmont.cuba.security.global.UserSession)29 LoginException (com.haulmont.cuba.security.global.LoginException)13 UUID (java.util.UUID)10 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)8 IOException (java.io.IOException)8 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)6 AppContext.withSecurityContext (com.haulmont.cuba.core.sys.AppContext.withSecurityContext)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)5 Locale (java.util.Locale)5 Transaction (com.haulmont.cuba.core.Transaction)3 AppContext.getSecurityContext (com.haulmont.cuba.core.sys.AppContext.getSecurityContext)3 AppContext.setSecurityContext (com.haulmont.cuba.core.sys.AppContext.setSecurityContext)3 TrustedClientCredentials (com.haulmont.cuba.security.auth.TrustedClientCredentials)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Nonnull (javax.annotation.Nonnull)3 LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)2 LoginPasswordCredentials (com.haulmont.cuba.security.auth.LoginPasswordCredentials)2 SystemUserCredentials (com.haulmont.cuba.security.auth.SystemUserCredentials)2