Search in sources :

Example 1 with UserSession

use of io.vertigo.persona.security.UserSession in project vertigo by KleeGroup.

the class AuthorizationManagerImpl method getUserPermissionsOpt.

private Optional<UserAuthorizations> getUserPermissionsOpt() {
    final Optional<UserSession> userSessionOpt = securityManager.getCurrentUserSession();
    if (!userSessionOpt.isPresent()) {
        // Si il n'y a pas de session alors pas d'autorisation.
        return Optional.empty();
    }
    UserAuthorizations userAuthorizations = userSessionOpt.get().getAttribute(USER_SESSION_ACL_KEY);
    if (userAuthorizations == null) {
        userAuthorizations = new UserAuthorizations();
        userSessionOpt.get().putAttribute(USER_SESSION_ACL_KEY, userAuthorizations);
    }
    return Optional.of(userAuthorizations);
}
Also used : UserSession(io.vertigo.persona.security.UserSession) UserAuthorizations(io.vertigo.account.authorization.UserAuthorizations)

Example 2 with UserSession

use of io.vertigo.persona.security.UserSession in project vertigo by KleeGroup.

the class SessionWebServiceHandlerPlugin method handle.

/**
 * {@inheritDoc}
 */
@Override
public Object handle(final Request request, final Response response, final WebServiceCallContext routeContext, final HandlerChain chain) throws SessionException {
    // obtain session (create if needed)
    final Session session = request.session(true);
    final UserSession user = obtainUserSession(session);
    try {
        // Bind userSession to SecurityManager
        securityManager.startCurrentUserSession(user);
        return chain.handle(request, response, routeContext);
    } catch (final VSecurityException e) {
        if (session.isNew()) {
            // If a new session is badly use, we invalid it (light protection against DDOS)
            session.invalidate();
            // If session was just created, we translate securityException as a Session expiration.
            throw (SessionException) new SessionException("Session has expired").initCause(e);
        }
        throw e;
    } finally {
        // Unbind userSession to SecurityManager
        securityManager.stopCurrentUserSession();
    }
}
Also used : UserSession(io.vertigo.persona.security.UserSession) SessionException(io.vertigo.vega.webservice.exception.SessionException) VSecurityException(io.vertigo.vega.webservice.exception.VSecurityException) Session(spark.Session) UserSession(io.vertigo.persona.security.UserSession)

Example 3 with UserSession

use of io.vertigo.persona.security.UserSession in project vertigo by KleeGroup.

the class SessionWebServiceHandlerPlugin method obtainUserSession.

// ==========================================================================
// =================GESTION DE LA SESSION UTILISATEUR========================
// ==========================================================================
/**
 * Retourne la session utilisateur.
 *
 * @return Session utilisateur
 */
private UserSession obtainUserSession(final Session session) {
    UserSession user = session.attribute(USER_SESSION);
    // Si la session user n'est pas créée on la crée
    if (user == null) {
        user = securityManager.createUserSession();
        session.attribute(USER_SESSION, user);
    }
    return user;
}
Also used : UserSession(io.vertigo.persona.security.UserSession)

Example 4 with UserSession

use of io.vertigo.persona.security.UserSession in project vertigo by KleeGroup.

the class AuthenticationManagerTest method testLoginFail.

@Test
public void testLoginFail() {
    final AuthenticationToken token = new UsernamePasswordAuthenticationToken("badUserName", "badPassword");
    final Optional<Account> account = authenticationManager.login(token);
    Assert.assertFalse("Shouldn't found any account with a bad login", account.isPresent());
    final Optional<UserSession> userSession = securityManager.getCurrentUserSession();
    Assert.assertTrue("No UserSession", userSession.isPresent());
    Assert.assertFalse("Badly authenticated", userSession.get().isAuthenticated());
}
Also used : Account(io.vertigo.account.account.Account) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken) AuthenticationToken(io.vertigo.account.authentication.AuthenticationToken) UserSession(io.vertigo.persona.security.UserSession) UsernamePasswordAuthenticationToken(io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 5 with UserSession

use of io.vertigo.persona.security.UserSession in project vertigo by KleeGroup.

the class VSecurityManagerTest method testSecuritySqlOnEntity.

@Test
public void testSecuritySqlOnEntity() {
    final Record recordTooExpensive = createRecord();
    recordTooExpensive.setAmount(10000d);
    final Record recordOtherUser = createRecord();
    recordOtherUser.setUtiIdOwner(2000L);
    final Record recordOtherUserAndTooExpensive = createRecord();
    recordOtherUserAndTooExpensive.setUtiIdOwner(2000L);
    recordOtherUserAndTooExpensive.setAmount(10000d);
    final Authorization recordRead = getAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
    final UserSession userSession = securityManager.<TestUserSession>createUserSession();
    try {
        securityManager.startCurrentUserSession(userSession);
        authorizationManager.obtainUserAuthorizations().withSecurityKeys("utiId", DEFAULT_UTI_ID).withSecurityKeys("typId", DEFAULT_TYPE_ID).withSecurityKeys("montantMax", DEFAULT_MONTANT_MAX).addAuthorization(recordRead);
        final boolean canReadRecord = authorizationManager.hasAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
        Assert.assertTrue(canReadRecord);
        final SqlDialect sqlDialect = new PostgreSqlDataBase().getSqlDialect();
        final Tuple2<String, CriteriaCtx> readRecordSql = authorizationManager.getCriteriaSecurity(Record.class, RecordOperations.READ).toSql(sqlDialect);
        // read -> MONTANT<=${montantMax} or UTI_ID_OWNER=${utiId}
        Assert.assertEquals("( AMOUNT <= #AMOUNT_0# OR UTI_ID_OWNER = #UTI_ID_OWNER_1# ) ", readRecordSql.getVal1());
        Assert.assertEquals(100.0, readRecordSql.getVal2().getAttributeValue("AMOUNT_0"));
        Assert.assertEquals(1000L, readRecordSql.getVal2().getAttributeValue("UTI_ID_OWNER_1"));
    } finally {
        securityManager.stopCurrentUserSession();
    }
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) CriteriaCtx(io.vertigo.dynamo.criteria.CriteriaCtx) TestUserSession(io.vertigo.account.data.TestUserSession) UserSession(io.vertigo.persona.security.UserSession) SqlDialect(io.vertigo.database.sql.vendor.SqlDialect) Record(io.vertigo.account.authorization.model.Record) TestUserSession(io.vertigo.account.data.TestUserSession) PostgreSqlDataBase(io.vertigo.database.impl.sql.vendor.postgresql.PostgreSqlDataBase) Test(org.junit.Test)

Aggregations

UserSession (io.vertigo.persona.security.UserSession)21 Test (org.junit.Test)14 TestUserSession (io.vertigo.account.data.TestUserSession)13 Authorization (io.vertigo.account.authorization.metamodel.Authorization)9 Record (io.vertigo.account.authorization.model.Record)8 Account (io.vertigo.account.account.Account)2 AuthenticationToken (io.vertigo.account.authentication.AuthenticationToken)2 UsernamePasswordAuthenticationToken (io.vertigo.account.impl.authentication.UsernamePasswordAuthenticationToken)2 SessionException (io.vertigo.vega.webservice.exception.SessionException)2 UserAuthorizations (io.vertigo.account.authorization.UserAuthorizations)1 PostgreSqlDataBase (io.vertigo.database.impl.sql.vendor.postgresql.PostgreSqlDataBase)1 SqlDialect (io.vertigo.database.sql.vendor.SqlDialect)1 CriteriaCtx (io.vertigo.dynamo.criteria.CriteriaCtx)1 VSecurityException (io.vertigo.vega.webservice.exception.VSecurityException)1 Locale (java.util.Locale)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)1 Session (spark.Session)1