Search in sources :

Example 11 with AuthToken

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

the class AuthTokenDaoInMemoryImpl method findAuthTokensByUser.

@Override
public synchronized List<AuthToken> findAuthTokensByUser(String userUuid) {
    Collection<AuthToken> data = idxByUser.get(userUuid);
    List<AuthToken> ret = new ArrayList<AuthToken>(data.size());
    for (AuthToken d : data) {
        ret.add(clone(d));
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) AuthToken(org.summerb.users.api.dto.AuthToken)

Example 12 with AuthToken

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

the class AuthTokenDaoInMemoryImpl method loadPersistedTokens.

private void loadPersistedTokens() {
    try {
        if (pathNameToPersistedTokens == null) {
            log.info("No pathNameToPersistedTokens configured, no tokens will be loaded");
            return;
        }
        File file = new File(pathNameToPersistedTokens);
        if (!file.exists()) {
            log.warn("pathNameToPersistedTokens configured, but file nto found, no tokens will be loaded");
            return;
        }
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            long now = System.currentTimeMillis();
            log.info("Loaded tokens from: " + file.getAbsolutePath());
            for (String line; (line = br.readLine()) != null; ) {
                AuthToken token = tryParseToken(line);
                if (token == null || token.getExpiresAt() < now) {
                    continue;
                }
                internalAddToken(token);
            }
            log.info("Loaded tokens count: " + tokens.size());
        }
    } catch (Throwable t) {
        log.error("Failed to load persisted tokens", t);
    }
}
Also used : BufferedReader(java.io.BufferedReader) AuthToken(org.summerb.users.api.dto.AuthToken) FileReader(java.io.FileReader) File(java.io.File)

Example 13 with AuthToken

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

the class AuthTokenDaoImplTest method testCreateAuthToken_expectOk.

@Test
public void testCreateAuthToken_expectOk() throws Exception {
    User user = userService.createUser(UserFactory.createNewUserTemplate());
    passwordService.setUserPassword(user.getUuid(), "aaa");
    AuthToken authToken = authTokenService.authenticate(user.getEmail(), "aaa", "LOCAL");
    assertNotNull(authToken);
}
Also used : User(org.summerb.users.api.dto.User) AuthToken(org.summerb.users.api.dto.AuthToken) Test(org.junit.Test)

Example 14 with AuthToken

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

the class AuthTokenDaoImplTest method testIsAuthTokenValid_expectTokenMustBeValidRightAfterCreation.

@Test
public void testIsAuthTokenValid_expectTokenMustBeValidRightAfterCreation() throws Exception {
    User user = userService.createUser(UserFactory.createNewUserTemplate());
    passwordService.setUserPassword(user.getUuid(), "aaa");
    AuthToken authToken = authTokenService.authenticate(user.getEmail(), "aaa", "LOCAL");
    assertNotNull(authToken);
    AuthToken result = authTokenService.isAuthTokenValid(user.getUuid(), authToken.getUuid(), authToken.getTokenValue());
    assertNotNull(result);
}
Also used : User(org.summerb.users.api.dto.User) AuthToken(org.summerb.users.api.dto.AuthToken) Test(org.junit.Test)

Example 15 with AuthToken

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

the class AuthTokenDaoImplTest method testDeleteAuthToken_expectDeletedAuthTokenMustNotBeValid.

@Test
public void testDeleteAuthToken_expectDeletedAuthTokenMustNotBeValid() throws Exception {
    User user = userService.createUser(UserFactory.createNewUserTemplate());
    passwordService.setUserPassword(user.getUuid(), "aaa");
    AuthToken authToken = authTokenService.authenticate(user.getEmail(), "aaa", "LOCAL");
    assertNotNull(authToken);
    AuthToken result = authTokenService.isAuthTokenValid(user.getUuid(), authToken.getUuid(), authToken.getTokenValue());
    assertNotNull(result);
    authTokenService.deleteAuthToken(authToken.getUuid());
    result = authTokenService.isAuthTokenValid(user.getUuid(), authToken.getUuid(), authToken.getTokenValue());
    assertNull(result);
}
Also used : User(org.summerb.users.api.dto.User) 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