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);
}
}
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;
}
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;
}
}
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);
}
}
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);
}
Aggregations