Search in sources :

Example 1 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class UserServiceImpl method requestPasswordReset.

@Override
public UserCredentials requestPasswordReset(String email) {
    log.trace("Executing requestPasswordReset email [{}]", email);
    validateString(email, "Incorrect email " + email);
    User user = userDao.findByEmail(email);
    if (user == null) {
        throw new IncorrectParameterException(String.format("Unable to find user by email [%s]", email));
    }
    UserCredentials userCredentials = userCredentialsDao.findByUserId(user.getUuidId());
    if (!userCredentials.isEnabled()) {
        throw new IncorrectParameterException("Unable to reset password for inactive user");
    }
    userCredentials.setResetToken(RandomStringUtils.randomAlphanumeric(DEFAULT_TOKEN_LENGTH));
    return saveUserCredentials(userCredentials);
}
Also used : User(org.thingsboard.server.common.data.User) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 2 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class UserServiceImpl method activateUserCredentials.

@Override
public UserCredentials activateUserCredentials(String activateToken, String password) {
    log.trace("Executing activateUserCredentials activateToken [{}], password [{}]", activateToken, password);
    validateString(activateToken, "Incorrect activateToken " + activateToken);
    validateString(password, "Incorrect password " + password);
    UserCredentials userCredentials = userCredentialsDao.findByActivateToken(activateToken);
    if (userCredentials == null) {
        throw new IncorrectParameterException(String.format("Unable to find user credentials by activateToken [%s]", activateToken));
    }
    if (userCredentials.isEnabled()) {
        throw new IncorrectParameterException("User credentials already activated");
    }
    userCredentials.setEnabled(true);
    userCredentials.setActivateToken(null);
    userCredentials.setPassword(password);
    return saveUserCredentials(userCredentials);
}
Also used : IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 3 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class UserCredentialsEntity method toData.

@Override
public UserCredentials toData() {
    UserCredentials userCredentials = new UserCredentials(new UserCredentialsId(id));
    userCredentials.setCreatedTime(UUIDs.unixTimestamp(id));
    if (userId != null) {
        userCredentials.setUserId(new UserId(userId));
    }
    userCredentials.setEnabled(enabled);
    userCredentials.setPassword(password);
    userCredentials.setActivateToken(activateToken);
    userCredentials.setResetToken(resetToken);
    return userCredentials;
}
Also used : UserCredentialsId(org.thingsboard.server.common.data.id.UserCredentialsId) UserId(org.thingsboard.server.common.data.id.UserId) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 4 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class UserCredentialsEntity method toData.

@Override
public UserCredentials toData() {
    UserCredentials userCredentials = new UserCredentials(new UserCredentialsId(getId()));
    userCredentials.setCreatedTime(UUIDs.unixTimestamp(getId()));
    if (userId != null) {
        userCredentials.setUserId(new UserId(toUUID(userId)));
    }
    userCredentials.setEnabled(enabled);
    userCredentials.setPassword(password);
    userCredentials.setActivateToken(activateToken);
    userCredentials.setResetToken(resetToken);
    return userCredentials;
}
Also used : UserCredentialsId(org.thingsboard.server.common.data.id.UserCredentialsId) UserId(org.thingsboard.server.common.data.id.UserId) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials)

Example 5 with UserCredentials

use of org.thingsboard.server.common.data.security.UserCredentials in project thingsboard by thingsboard.

the class RefreshTokenAuthenticationProvider method authenticateByUserId.

private SecurityUser authenticateByUserId(UserId userId) {
    User user = userService.findUserById(userId);
    if (user == null) {
        throw new UsernameNotFoundException("User not found by refresh token");
    }
    UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
    if (userCredentials == null) {
        throw new UsernameNotFoundException("User credentials not found");
    }
    if (!userCredentials.isEnabled()) {
        throw new DisabledException("User is not active");
    }
    if (user.getAuthority() == null)
        throw new InsufficientAuthenticationException("User has no authority assigned");
    UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.USER_NAME, user.getEmail());
    SecurityUser securityUser = new SecurityUser(user, userCredentials.isEnabled(), userPrincipal);
    return securityUser;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Aggregations

UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)24 User (org.thingsboard.server.common.data.User)12 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)8 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.Test)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 UserId (org.thingsboard.server.common.data.id.UserId)4 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)3 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)3 UserPrincipal (org.thingsboard.server.service.security.model.UserPrincipal)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpStatus (org.springframework.http.HttpStatus)2 ResponseEntity (org.springframework.http.ResponseEntity)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 UserCredentialsId (org.thingsboard.server.common.data.id.UserCredentialsId)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2