Search in sources :

Example 16 with UserId

use of org.thingsboard.server.common.data.id.UserId in project thingsboard by thingsboard.

the class JpaUserDaoTest method testSave.

@Test
@DatabaseSetup("classpath:dbunit/user.xml")
public void testSave() throws IOException {
    User user = new User();
    user.setId(new UserId(UUID.fromString("cd481534-27cc-11e7-93ae-92361f002671")));
    user.setTenantId(new TenantId(UUID.fromString("1edcb2c6-27cb-11e7-93ae-92361f002671")));
    user.setCustomerId(new CustomerId(UUID.fromString("51477cb4-27cb-11e7-93ae-92361f002671")));
    user.setEmail("user@thingsboard.org");
    user.setFirstName("Jackson");
    user.setLastName("Roberts");
    ObjectMapper mapper = new ObjectMapper();
    String additionalInfo = "{\"key\":\"value-100\"}";
    JsonNode jsonNode = mapper.readTree(additionalInfo);
    user.setAdditionalInfo(jsonNode);
    userDao.save(user);
    assertEquals(6, userDao.find().size());
    User savedUser = userDao.findByEmail("user@thingsboard.org");
    assertNotNull(savedUser);
    assertEquals(additionalInfo, savedUser.getAdditionalInfo().toString());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) User(org.thingsboard.server.common.data.User) UserId(org.thingsboard.server.common.data.id.UserId) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 17 with UserId

use of org.thingsboard.server.common.data.id.UserId in project thingsboard by thingsboard.

the class JwtTokenFactory method parseAccessJwtToken.

public SecurityUser parseAccessJwtToken(RawAccessJwtToken rawAccessToken) {
    Jws<Claims> jwsClaims = rawAccessToken.parseClaims(settings.getTokenSigningKey());
    Claims claims = jwsClaims.getBody();
    String subject = claims.getSubject();
    List<String> scopes = claims.get(SCOPES, List.class);
    if (scopes == null || scopes.isEmpty()) {
        throw new IllegalArgumentException("JWT Token doesn't have any scopes");
    }
    SecurityUser securityUser = new SecurityUser(new UserId(UUID.fromString(claims.get(USER_ID, String.class))));
    securityUser.setEmail(subject);
    securityUser.setAuthority(Authority.parse(scopes.get(0)));
    securityUser.setFirstName(claims.get(FIRST_NAME, String.class));
    securityUser.setLastName(claims.get(LAST_NAME, String.class));
    securityUser.setEnabled(claims.get(ENABLED, Boolean.class));
    boolean isPublic = claims.get(IS_PUBLIC, Boolean.class);
    UserPrincipal principal = new UserPrincipal(isPublic ? UserPrincipal.Type.PUBLIC_ID : UserPrincipal.Type.USER_NAME, subject);
    securityUser.setUserPrincipal(principal);
    String tenantId = claims.get(TENANT_ID, String.class);
    if (tenantId != null) {
        securityUser.setTenantId(new TenantId(UUID.fromString(tenantId)));
    }
    String customerId = claims.get(CUSTOMER_ID, String.class);
    if (customerId != null) {
        securityUser.setCustomerId(new CustomerId(UUID.fromString(customerId)));
    }
    return securityUser;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Claims(io.jsonwebtoken.Claims) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Aggregations

UserId (org.thingsboard.server.common.data.id.UserId)17 User (org.thingsboard.server.common.data.User)9 CustomerId (org.thingsboard.server.common.data.id.CustomerId)9 TenantId (org.thingsboard.server.common.data.id.TenantId)8 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)4 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)4 UserPrincipal (org.thingsboard.server.service.security.model.UserPrincipal)3 Claims (io.jsonwebtoken.Claims)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 Customer (org.thingsboard.server.common.data.Customer)2 UserCredentialsId (org.thingsboard.server.common.data.id.UserCredentialsId)2 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)2 PluginApiCallSecurityContext (org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)1 URI (java.net.URI)1