Search in sources :

Example 21 with UserCredentials

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

the class JpaUserCredentialsDaoTest method testFindByActivateToken.

@Test
@DatabaseSetup("classpath:dbunit/user_credentials.xml")
public void testFindByActivateToken() {
    UserCredentials userCredentials = userCredentialsDao.findByActivateToken("ACTIVATE_TOKEN_1");
    assertNotNull(userCredentials);
    assertEquals("3ed10af0-27d5-11e7-93ae-92361f002671", userCredentials.getId().toString());
}
Also used : UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 22 with UserCredentials

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

the class JpaUserCredentialsDaoTest method testFindByResetToken.

@Test
@DatabaseSetup("classpath:dbunit/user_credentials.xml")
public void testFindByResetToken() {
    UserCredentials userCredentials = userCredentialsDao.findByResetToken("RESET_TOKEN_2");
    assertNotNull(userCredentials);
    assertEquals("4b9e010c-27d5-11e7-93ae-92361f002671", userCredentials.getId().toString());
}
Also used : UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 23 with UserCredentials

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

the class AuthController method checkActivateToken.

@RequestMapping(value = "/noauth/activate", params = { "activateToken" }, method = RequestMethod.GET)
public ResponseEntity<String> checkActivateToken(@RequestParam(value = "activateToken") String activateToken) {
    HttpHeaders headers = new HttpHeaders();
    HttpStatus responseStatus;
    UserCredentials userCredentials = userService.findUserCredentialsByActivateToken(activateToken);
    if (userCredentials != null) {
        String createURI = "/login/createPassword";
        try {
            URI location = new URI(createURI + "?activateToken=" + activateToken);
            headers.setLocation(location);
            responseStatus = HttpStatus.SEE_OTHER;
        } catch (URISyntaxException e) {
            log.error("Unable to create URI with address [{}]", createURI);
            responseStatus = HttpStatus.BAD_REQUEST;
        }
    } else {
        responseStatus = HttpStatus.CONFLICT;
    }
    return new ResponseEntity<>(headers, responseStatus);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 24 with UserCredentials

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

the class RestAuthenticationProvider method authenticateByUsernameAndPassword.

private Authentication authenticateByUsernameAndPassword(UserPrincipal userPrincipal, String username, String password) {
    User user = userService.findUserByEmail(username);
    if (user == null) {
        throw new UsernameNotFoundException("User not found: " + username);
    }
    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 (!encoder.matches(password, userCredentials.getPassword())) {
        throw new BadCredentialsException("Authentication Failed. Username or Password not valid.");
    }
    if (user.getAuthority() == null)
        throw new InsufficientAuthenticationException("User has no authority assigned");
    SecurityUser securityUser = new SecurityUser(user, userCredentials.isEnabled(), userPrincipal);
    return new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities());
}
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)

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