use of org.summerb.microservices.users.api.dto.AuthToken in project summerb by skarpushin.
the class PersistentTokenRepositoryDefaultImpl method removeUserTokens.
@Override
public void removeUserTokens(String username) {
try {
User user = userService.getUserByEmail(username);
List<AuthToken> authTokens = authTokenService.findUserAuthTokens(user.getUuid());
for (AuthToken authToken : authTokens) {
authTokenService.deleteAuthToken(authToken.getUuid());
}
} catch (Throwable e) {
throw new RuntimeException("Failed to delete user auth tokens", e);
}
}
use of org.summerb.microservices.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);
}
}
use of org.summerb.microservices.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;
}
use of org.summerb.microservices.users.api.dto.AuthToken in project summerb by skarpushin.
the class AuthTokenServiceImpl method isAuthTokenValid.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken isAuthTokenValid(String userUuid, String authTokenUuid, String tokenValue) throws UserNotFoundException {
Preconditions.checkArgument(userUuid != null);
Preconditions.checkArgument(authTokenUuid != null);
Preconditions.checkArgument(StringUtils.hasText(tokenValue), "TokenValue is mandatory");
try {
// First - check token itself
AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
if (authToken.getExpiresAt() < getNow()) {
authTokenDao.deleteAuthToken(authTokenUuid);
return null;
}
if (!tokenValue.equals(authToken.getTokenValue())) {
return null;
}
// Check reference to user
User user = userService.getUserByUuid(userUuid);
if (!authToken.getUserUuid().equals(user.getUuid())) {
return null;
}
// Now we need to update time when token was checked
authToken.setTokenValue(UUID.randomUUID().toString());
authToken.setLastVerifiedAt(getNow());
authTokenDao.updateToken(authTokenUuid, authToken.getLastVerifiedAt(), authToken.getTokenValue());
return authToken;
} catch (AuthTokenNotFoundException nfe) {
return null;
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
String msg = String.format("Failed to check auth token '%s' validity for user '%s'", authTokenUuid, userUuid);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.microservices.users.api.dto.AuthToken in project summerb by skarpushin.
the class AuthTokenDaoInMemoryImpl method updateToken.
@Override
public synchronized void updateToken(String authTokenUuid, long now, String newTokenValue) {
AuthToken token = tokens.get(authTokenUuid);
if (token == null || token.getLastVerifiedAt() >= now) {
return;
}
token.setLastVerifiedAt(now);
token.setTokenValue(newTokenValue);
}
Aggregations