Search in sources :

Example 6 with User

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

the class LocaleResolverUserBasedImpl method resolveLocale.

@Override
public Locale resolveLocale(HttpServletRequest request) {
    try {
        boolean isRegisteredUser = securityContextResolver.hasRole(SecurityConstants.ROLE_USER);
        if (isRegisteredUser) {
            User user = securityContextResolver.getUser();
            Locale userLocale = StringUtils.parseLocaleString(user.getLocale());
            return userLocale;
        }
    } catch (Throwable t) {
        log.warn("Failed to resolve locale based on user data", t);
    }
    return super.resolveLocale(request);
}
Also used : Locale(java.util.Locale) User(org.summerb.microservices.users.api.dto.User)

Example 7 with User

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

the class LocaleResolverUserBasedImpl method updateUserProfileWithNewLocale.

private void updateUserProfileWithNewLocale(String newLocale) throws UserNotFoundException, FieldValidationException {
    User user = securityContextResolver.getUser();
    if (user.getLocale().equalsIgnoreCase(newLocale)) {
        return;
    }
    user.setLocale(newLocale);
    userService.updateUser(user);
}
Also used : User(org.summerb.microservices.users.api.dto.User)

Example 8 with User

use of org.summerb.microservices.users.api.dto.User 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);
    }
}
Also used : User(org.summerb.microservices.users.api.dto.User) AuthToken(org.summerb.microservices.users.api.dto.AuthToken)

Example 9 with User

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

the class AuthTokenServiceImpl method authenticate.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken authenticate(String userEmail, String passwordPlain, String clientIp) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(passwordPlain != null);
    Preconditions.checkArgument(clientIp != null);
    try {
        User user = validateAndGetUser(userEmail, passwordPlain);
        return createAuthToken(user.getEmail(), clientIp, UUID.randomUUID().toString(), UUID.randomUUID().toString());
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, InvalidPasswordException.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) InvalidPasswordException(org.summerb.microservices.users.api.exceptions.InvalidPasswordException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with User

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

Aggregations

User (org.summerb.microservices.users.api.dto.User)61 Test (org.junit.Test)34 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)13 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)11 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)11 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)11 Transactional (org.springframework.transaction.annotation.Transactional)8 Date (java.util.Date)4 PagerParams (org.summerb.approaches.jdbccrud.api.dto.PagerParams)4 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 UserDetailsImpl (org.summerb.approaches.springmvc.security.dto.UserDetailsImpl)2 ValidationContext (org.summerb.approaches.validation.ValidationContext)2 GenericException (org.summerb.utils.exceptions.GenericException)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 EventBus (com.google.common.eventbus.EventBus)1 Gson (com.google.gson.Gson)1 Locale (java.util.Locale)1 Secured (org.springframework.security.access.annotation.Secured)1