Search in sources :

Example 96 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class PermissionTest method test.

@Test
public void test() throws LoginException {
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    UserSession userSession = lw.login(USER_NAME, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
    assertNotNull(userSession);
    boolean permitted = userSession.isPermitted(PermissionType.SCREEN, PERM_TARGET_SCREEN);
    assertFalse(permitted);
    permitted = userSession.isPermitted(PermissionType.SCREEN, "some action");
    // permitted all if not explicitly denied
    assertTrue(permitted);
    permitted = userSession.isPermitted(PermissionType.ENTITY_ATTR, PERM_TARGET_ATTR);
    // READ access permitted
    assertTrue(permitted);
    permitted = userSession.isPermitted(PermissionType.ENTITY_ATTR, PERM_TARGET_ATTR, 2);
    // READ/WRITE access denied
    assertFalse(permitted);
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 97 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class ServerTokenStoreImpl method removeAccessTokenFromMemory.

protected void removeAccessTokenFromMemory(String tokenValue) {
    RestUserSessionInfo sessionInfo;
    lock.writeLock().lock();
    try {
        accessTokenValueToAccessTokenStore.remove(tokenValue);
        accessTokenValueToAuthenticationStore.remove(tokenValue);
        accessTokenValueToUserLoginStore.remove(tokenValue);
        String authenticationKey = accessTokenValueToAuthenticationKeyStore.remove(tokenValue);
        if (authenticationKey != null) {
            authenticationToAccessTokenStore.remove(authenticationKey);
        }
        sessionInfo = accessTokenValueToSessionInfoStore.remove(tokenValue);
    } finally {
        lock.writeLock().unlock();
    }
    if (sessionInfo != null) {
        try {
            UserSession session = userSessions.get(sessionInfo.getId());
            if (session != null) {
                AppContext.setSecurityContext(new SecurityContext(session));
                try {
                    authenticationManager.logout();
                } finally {
                    AppContext.setSecurityContext(null);
                }
            }
        } catch (NoUserSessionException ignored) {
        }
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 98 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class Authentication method begin.

/**
 * Begin an authenticated code block.
 * <br>
 * If a valid current thread session exists, does nothing.
 * Otherwise sets the current thread session, logging in if necessary.
 * <br>
 * Subsequent {@link #end()} method must be called in "finally" section.
 *
 * @param login user login. If null, a value of {@code cuba.jmxUserLogin} app property is used.
 * @return new or cached instance of system user session
 */
public UserSession begin(@Nullable String login) {
    if (cleanupCounter.get() == null) {
        cleanupCounter.set(0);
    }
    // check if a current thread session exists, that is we got here from authenticated code
    SecurityContext securityContext = AppContext.getSecurityContext();
    if (securityContext != null) {
        UserSession userSession = userSessions.getAndRefresh(securityContext.getSessionId());
        if (userSession != null) {
            log.trace("Already authenticated, do nothing");
            cleanupCounter.set(cleanupCounter.get() + 1);
            if (log.isTraceEnabled()) {
                log.trace("New cleanup counter value: {}", cleanupCounter.get());
            }
            return userSession;
        }
    }
    // no current thread session or it is expired - need to authenticate
    if (StringUtils.isBlank(login)) {
        login = getSystemLogin();
    }
    UserSession session = null;
    log.trace("Authenticating as {}", login);
    UUID sessionId = sessions.get(login);
    if (sessionId != null) {
        session = userSessions.getAndRefresh(sessionId);
    }
    if (session == null) {
        // saved session doesn't exist or is expired
        synchronized (this) {
            // double check to prevent the same log in by subsequent threads
            sessionId = sessions.get(login);
            if (sessionId != null) {
                session = userSessions.get(sessionId);
            }
            if (session == null) {
                try {
                    session = authenticationManager.login(new SystemUserCredentials(login)).getSession();
                    session.setClientInfo("System authentication");
                } catch (LoginException e) {
                    throw new RuntimeException("Unable to perform system login", e);
                }
                sessions.put(login, session.getId());
            }
        }
    }
    AppContext.setSecurityContext(new SecurityContext(session));
    return session;
}
Also used : SystemUserCredentials(com.haulmont.cuba.security.auth.SystemUserCredentials) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.getSecurityContext(com.haulmont.cuba.core.sys.AppContext.getSecurityContext) AppContext.setSecurityContext(com.haulmont.cuba.core.sys.AppContext.setSecurityContext) LoginException(com.haulmont.cuba.security.global.LoginException) UUID(java.util.UUID)

Example 99 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class LoginWorkerBean method getSystemSession.

@Override
public UserSession getSystemSession(String trustedClientPassword) throws LoginException {
    if (!trustedLoginHandler.checkPassword(trustedClientPassword)) {
        Locale locale = messages.getTools().getDefaultLocale();
        throw new LoginException(messages.formatMessage(MSG_PACK, "LoginException.InvalidLoginOrPassword", locale, serverConfig.getJmxUserLogin()));
    }
    SecurityContext currentSecContext = AppContext.getSecurityContext();
    UserSession userSession;
    try {
        // we need to reset security context to prevent reusing current session
        AppContext.setSecurityContext(null);
        userSession = authentication.begin();
        authentication.end();
    } finally {
        AppContext.setSecurityContext(currentSecContext);
    }
    return userSession;
}
Also used : Locale(java.util.Locale) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) LoginException(com.haulmont.cuba.security.global.LoginException)

Example 100 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class TrustedClientServiceBean method getSystemSession.

@Nonnull
@Override
public UserSession getSystemSession(String trustedClientPassword) throws LoginException {
    checkTrustedClientCredentials(trustedClientPassword);
    return withSecurityContext(null, () -> {
        UserSession userSession = authentication.begin();
        authentication.end();
        return userSession;
    });
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) Nonnull(javax.annotation.Nonnull)

Aggregations

UserSession (com.haulmont.cuba.security.global.UserSession)127 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)29 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)25 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)24 LoginException (com.haulmont.cuba.security.global.LoginException)23 Test (org.junit.Test)19 User (com.haulmont.cuba.security.entity.User)17 UUID (java.util.UUID)16 IOException (java.io.IOException)14 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)12 ArrayList (java.util.ArrayList)11 Locale (java.util.Locale)11 List (java.util.List)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)7 LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4