Search in sources :

Example 21 with AuthToken

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

the class AuthTokenServiceImpl method createAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(clientIp != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
    Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
    try {
        User user = userService.getUserByEmail(userEmail);
        AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
        authTokenDao.createAuthToken(authToken);
        return authToken;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with AuthToken

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

the class AuthTokenDaoInMemoryImpl method clone.

private AuthToken clone(AuthToken b) {
    AuthToken a = new AuthToken();
    a.setClientIp(b.getClientIp());
    a.setCreatedAt(b.getCreatedAt());
    a.setExpiresAt(b.getExpiresAt());
    a.setLastVerifiedAt(b.getLastVerifiedAt());
    a.setTokenValue(b.getTokenValue());
    a.setUserUuid(b.getUserUuid());
    a.setUuid(b.getUuid());
    return a;
}
Also used : AuthToken(org.summerb.microservices.users.api.dto.AuthToken)

Example 23 with AuthToken

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

the class AuthTokenDaoInMemoryImpl method tryParseToken.

private AuthToken tryParseToken(String line) {
    try {
        if (!StringUtils.hasText(line) || line.indexOf('\t') < 0) {
            return null;
        }
        String[] parts = line.split("\t");
        if (parts.length != 7) {
            return null;
        }
        AuthToken ret = new AuthToken();
        ret.setClientIp(parts[0]);
        ret.setCreatedAt(Long.valueOf(parts[1]));
        ret.setExpiresAt(Long.valueOf(parts[2]));
        ret.setLastVerifiedAt(Long.valueOf(parts[3]));
        ret.setTokenValue(parts[4]);
        ret.setUserUuid(parts[5]);
        ret.setUuid(parts[6]);
        return ret;
    } catch (Throwable t) {
        log.warn("Failed to parse token from line: " + line, t);
        return null;
    }
}
Also used : AuthToken(org.summerb.microservices.users.api.dto.AuthToken)

Example 24 with AuthToken

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

the class AuthTokenDaoInMemoryImpl method persistTokens.

private void persistTokens() {
    try {
        if (pathNameToPersistedTokens == null) {
            log.info("No pathNameToPersistedTokens configured, no tokens will be persisted");
            return;
        }
        File file = new File(pathNameToPersistedTokens);
        Preconditions.checkState(!file.exists() || file.delete(), "Failed to ensure file is not exist before we start writing new one");
        try (PrintWriter output = new PrintWriter(new FileWriter(file, true))) {
            long now = System.currentTimeMillis();
            for (AuthToken token : tokens.values()) {
                if (token.getExpiresAt() < now) {
                    continue;
                }
                output.printf("%s%s", formatToken(token), System.getProperty("line.separator"));
                output.flush();
            }
        }
        log.info("Tokens persisted: " + file.getAbsolutePath());
    } catch (Throwable t) {
        log.error("Failed to persist tokens", t);
    }
}
Also used : FileWriter(java.io.FileWriter) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 25 with AuthToken

use of org.summerb.microservices.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.microservices.users.api.dto.AuthToken) Test(org.junit.Test)

Aggregations

AuthToken (org.summerb.microservices.users.api.dto.AuthToken)28 Test (org.junit.Test)12 User (org.summerb.microservices.users.api.dto.User)11 Transactional (org.springframework.transaction.annotation.Transactional)3 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)3 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)3 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)3 File (java.io.File)2 Date (java.util.Date)2 AuthTokenNotFoundException (org.summerb.microservices.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.approaches.jdbccrud.api.dto.PagerParams)1 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)1