Search in sources :

Example 1 with UserSession

use of org.mamute.model.UserSession in project mamute by caelum.

the class Access method login.

public User login(User user) {
    if (user.isBanned())
        throw new BannedUserException();
    UserSession newSession = user.newSession();
    Cookie cookie = new Cookie(BRUTAL_SESSION, newSession.getSessionKey());
    cookie.setPath("/");
    cookie.setHttpOnly(true);
    cookie.setMaxAge(Integer.MAX_VALUE);
    response.addCookie(cookie);
    users.save(newSession);
    this.userAndSession = new UserAndSession(user, newSession);
    return user;
}
Also used : Cookie(javax.servlet.http.Cookie) UserAndSession(org.mamute.dto.UserAndSession) UserSession(org.mamute.model.UserSession)

Example 2 with UserSession

use of org.mamute.model.UserSession in project mamute by caelum.

the class UserDAOTest method should_find_by_session_key.

@Test
public void should_find_by_session_key() {
    User gui = user("Guilherme Sonar", "gui@email.com.br");
    users.save(gui);
    UserSession userSession = gui.newSession();
    users.save(userSession);
    assertEquals(gui, users.findBySessionKey(userSession.getSessionKey()).getUser());
    assertNull(users.findBySessionKey("12345"));
    assertNull(users.findBySessionKey(null));
}
Also used : User(org.mamute.model.User) UserSession(org.mamute.model.UserSession) Test(org.junit.Test)

Example 3 with UserSession

use of org.mamute.model.UserSession in project mamute by caelum.

the class AccessTest method should_auto_login_with_valid_cookie.

@Test
public void should_auto_login_with_valid_cookie() {
    String sessionKey = "session-key";
    Cookie brutalCookie = new Cookie(Access.BRUTAL_SESSION, sessionKey);
    Cookie[] cookies = new Cookie[] { anyCookie(), anyCookie(), brutalCookie, anyCookie(), anyCookie() };
    when(request.getCookies()).thenReturn(cookies);
    when(users.findBySessionKey(sessionKey)).thenReturn(new UserAndSession(user, new UserSession(user, sessionKey)));
    Access access = new Access(response, request, users);
    assertTrue(access.tryToAutoLogin());
}
Also used : Cookie(javax.servlet.http.Cookie) UserAndSession(org.mamute.dto.UserAndSession) UserSession(org.mamute.model.UserSession) Access(org.mamute.auth.Access) Test(org.junit.Test)

Aggregations

UserSession (org.mamute.model.UserSession)3 Cookie (javax.servlet.http.Cookie)2 Test (org.junit.Test)2 UserAndSession (org.mamute.dto.UserAndSession)2 Access (org.mamute.auth.Access)1 User (org.mamute.model.User)1