Search in sources :

Example 1 with UserId

use of org.thingsboard.server.common.data.id.UserId 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 2 with UserId

use of org.thingsboard.server.common.data.id.UserId 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 3 with UserId

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

the class RefreshTokenAuthenticationProvider method authenticateByPublicId.

private SecurityUser authenticateByPublicId(String publicId) {
    CustomerId customerId;
    try {
        customerId = new CustomerId(UUID.fromString(publicId));
    } catch (Exception e) {
        throw new BadCredentialsException("Refresh token is not valid");
    }
    Customer publicCustomer = customerService.findCustomerById(customerId);
    if (publicCustomer == null) {
        throw new UsernameNotFoundException("Public entity not found by refresh token");
    }
    if (!publicCustomer.isPublic()) {
        throw new BadCredentialsException("Refresh token is not valid");
    }
    User user = new User(new UserId(EntityId.NULL_UUID));
    user.setTenantId(publicCustomer.getTenantId());
    user.setCustomerId(publicCustomer.getId());
    user.setEmail(publicId);
    user.setAuthority(Authority.CUSTOMER_USER);
    user.setFirstName("Public");
    user.setLastName("Public");
    UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.PUBLIC_ID, publicId);
    SecurityUser securityUser = new SecurityUser(user, true, 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) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Example 4 with UserId

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

the class JwtTokenFactory method parseRefreshToken.

public SecurityUser parseRefreshToken(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("Refresh Token doesn't have any scopes");
    }
    if (!scopes.get(0).equals(Authority.REFRESH_TOKEN.name())) {
        throw new IllegalArgumentException("Invalid Refresh Token scope");
    }
    boolean isPublic = claims.get(IS_PUBLIC, Boolean.class);
    UserPrincipal principal = new UserPrincipal(isPublic ? UserPrincipal.Type.PUBLIC_ID : UserPrincipal.Type.USER_NAME, subject);
    SecurityUser securityUser = new SecurityUser(new UserId(UUID.fromString(claims.get(USER_ID, String.class))));
    securityUser.setUserPrincipal(principal);
    return securityUser;
}
Also used : Claims(io.jsonwebtoken.Claims) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) UserId(org.thingsboard.server.common.data.id.UserId) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Example 5 with UserId

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

the class RestAuthenticationProvider method authenticateByPublicId.

private Authentication authenticateByPublicId(UserPrincipal userPrincipal, String publicId) {
    CustomerId customerId;
    try {
        customerId = new CustomerId(UUID.fromString(publicId));
    } catch (Exception e) {
        throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
    }
    Customer publicCustomer = customerService.findCustomerById(customerId);
    if (publicCustomer == null) {
        throw new UsernameNotFoundException("Public entity not found: " + publicId);
    }
    if (!publicCustomer.isPublic()) {
        throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
    }
    User user = new User(new UserId(EntityId.NULL_UUID));
    user.setTenantId(publicCustomer.getTenantId());
    user.setCustomerId(publicCustomer.getId());
    user.setEmail(publicId);
    user.setAuthority(Authority.CUSTOMER_USER);
    user.setFirstName("Public");
    user.setLastName("Public");
    SecurityUser securityUser = new SecurityUser(user, true, 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) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException)

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