Search in sources :

Example 1 with UserAndSession

use of org.mamute.dto.UserAndSession 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 UserAndSession

use of org.mamute.dto.UserAndSession 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

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