Search in sources :

Example 1 with AuthTokenNotFoundException

use of org.summerb.users.api.exceptions.AuthTokenNotFoundException 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);
    }
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AuthTokenNotFoundException

use of org.summerb.users.api.exceptions.AuthTokenNotFoundException in project summerb by skarpushin.

the class AuthTokenServiceImpl method deleteAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAuthToken(String authTokenUuid) throws AuthTokenNotFoundException {
    Preconditions.checkArgument(authTokenUuid != null);
    try {
        getAuthTokenByUuid(authTokenUuid);
        authTokenDao.deleteAuthToken(authTokenUuid);
    } catch (AuthTokenNotFoundException nfe) {
        // it's ok
        return;
    } catch (Throwable t) {
        String msg = String.format("Failed to delete auth token '%s'", authTokenUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AuthTokenNotFoundException

use of org.summerb.users.api.exceptions.AuthTokenNotFoundException 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)

Aggregations

Transactional (org.springframework.transaction.annotation.Transactional)3 AuthTokenNotFoundException (org.summerb.users.api.exceptions.AuthTokenNotFoundException)3 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)3 AuthToken (org.summerb.users.api.dto.AuthToken)2 User (org.summerb.users.api.dto.User)1 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)1 FieldValidationException (org.summerb.validation.FieldValidationException)1 ValidationError (org.summerb.validation.ValidationError)1