Search in sources :

Example 16 with AuthToken

use of org.summerb.users.api.dto.AuthToken in project summerb by skarpushin.

the class AuthTokenDaoImplTest method testFindExpiredAuthTokens_expect2Tokens.

@Test
public void testFindExpiredAuthTokens_expect2Tokens() throws Exception {
    User user = userService.createUser(UserFactory.createNewUserTemplate());
    passwordService.setUserPassword(user.getUuid(), "aaa");
    AuthToken authToken1 = authTokenService.authenticate(user.getEmail(), "aaa", "LOCAL");
    assertNotNull(authToken1);
    Thread.sleep(501);
    AuthToken authToken2 = authTokenService.authenticate(user.getEmail(), "aaa", "LOCAL");
    assertNotNull(authToken2);
    Thread.sleep(501);
    List<AuthToken> tokens = authTokenService.findUserAuthTokens(user.getUuid());
    assertEquals(2, tokens.size());
}
Also used : User(org.summerb.users.api.dto.User) AuthToken(org.summerb.users.api.dto.AuthToken) Test(org.junit.Test)

Example 17 with AuthToken

use of org.summerb.users.api.dto.AuthToken in project summerb by skarpushin.

the class AuthTokenDaoImplTest method testUpdateToken_expectValueWillBeUpdated.

@Test
public void testUpdateToken_expectValueWillBeUpdated() throws Exception {
    User user = userService.createUser(UserFactory.createNewUserTemplate());
    passwordService.setUserPassword(user.getUuid(), "aaa");
    AuthToken authToken = authTokenService.createAuthToken(user.getEmail(), "LOCAL", "tUuid1", "tValue1");
    assertNotNull(authToken);
    authTokenDao.updateToken(authToken.getUuid(), new Date().getTime() + 1, "newValue2");
    authToken = authTokenService.getAuthTokenByUuid(authToken.getUuid());
    assertEquals("newValue2", authToken.getTokenValue());
}
Also used : User(org.summerb.users.api.dto.User) AuthToken(org.summerb.users.api.dto.AuthToken) Date(java.util.Date) Test(org.junit.Test)

Example 18 with AuthToken

use of org.summerb.users.api.dto.AuthToken in project summerb by skarpushin.

the class AuthTokenServiceImpl method buildNewAuthToken.

private AuthToken buildNewAuthToken(User user, String clientIp, String tokenUuid, String tokenValueUuid) {
    long now = getNow();
    AuthToken ret = new AuthToken();
    ret.setClientIp(clientIp);
    ret.setCreatedAt(now);
    ret.setExpiresAt(calculateAuthTokenExpirationPoint(now));
    ret.setLastVerifiedAt(now);
    ret.setUserUuid(user.getUuid());
    ret.setUuid(tokenUuid);
    ret.setTokenValue(tokenValueUuid);
    return ret;
}
Also used : AuthToken(org.summerb.users.api.dto.AuthToken)

Example 19 with AuthToken

use of org.summerb.users.api.dto.AuthToken in project summerb by skarpushin.

the class AuthTokenServiceImpl method updateToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void updateToken(String authTokenUuid, long lastVerifiedAt, String newTokenValue) throws AuthTokenNotFoundException, FieldValidationException {
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(newTokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (newTokenValue.equals(authToken.getTokenValue())) {
            throw new FieldValidationException(new ValidationError("validation.newValueExpected", "newTokenValue"));
        }
        // Now we need to update time when token was checked
        authTokenDao.updateToken(authTokenUuid, lastVerifiedAt, newTokenValue);
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, AuthTokenNotFoundException.class);
        String msg = String.format("Failed to update token '%s'", authTokenUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) ValidationError(org.summerb.validation.ValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with AuthToken

use of org.summerb.users.api.dto.AuthToken in project summerb by skarpushin.

the class AuthTokenServiceDbImplTest method testIsAuthTokenValid_blackbox_expectOkForExistentToken.

@Test
public void testIsAuthTokenValid_blackbox_expectOkForExistentToken() throws Exception {
    AuthTokenServiceImpl fixture = AuthTokenServiceDbImplFactory.createAuthTokenServiceDbImpl();
    AuthToken result = fixture.isAuthTokenValid(UserFactory.EXISTENT_USER, AuthTokenFactory.AUTH_TOKEN_EXISTENT, AuthTokenFactory.AUTH_TOKEN_EXISTENT_VALUE);
    assertNotNull(result);
}
Also used : AuthToken(org.summerb.users.api.dto.AuthToken) Test(org.junit.Test)

Aggregations

AuthToken (org.summerb.users.api.dto.AuthToken)28 Test (org.junit.Test)12 User (org.summerb.users.api.dto.User)11 Transactional (org.springframework.transaction.annotation.Transactional)3 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)3 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)3 FieldValidationException (org.summerb.validation.FieldValidationException)3 File (java.io.File)2 Date (java.util.Date)2 AuthTokenNotFoundException (org.summerb.users.api.exceptions.AuthTokenNotFoundException)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 PersistentRememberMeToken (org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken)1 PagerParams (org.summerb.easycrud.api.dto.PagerParams)1 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)1