Search in sources :

Example 11 with UserPrincipal

use of org.thingsboard.server.service.security.model.UserPrincipal in project thingsboard by thingsboard.

the class JwtTokenFactory method createRefreshToken.

public JwtToken createRefreshToken(SecurityUser securityUser) {
    if (StringUtils.isBlank(securityUser.getEmail())) {
        throw new IllegalArgumentException("Cannot create JWT Token without username/email");
    }
    ZonedDateTime currentTime = ZonedDateTime.now();
    UserPrincipal principal = securityUser.getUserPrincipal();
    Claims claims = Jwts.claims().setSubject(principal.getValue());
    claims.put(SCOPES, Collections.singletonList(Authority.REFRESH_TOKEN.name()));
    claims.put(USER_ID, securityUser.getId().getId().toString());
    claims.put(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID);
    String token = Jwts.builder().setClaims(claims).setIssuer(settings.getTokenIssuer()).setId(UUID.randomUUID().toString()).setIssuedAt(Date.from(currentTime.toInstant())).setExpiration(Date.from(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toInstant())).signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()).compact();
    return new AccessJwtToken(token, claims);
}
Also used : Claims(io.jsonwebtoken.Claims) ZonedDateTime(java.time.ZonedDateTime) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal)

Example 12 with UserPrincipal

use of org.thingsboard.server.service.security.model.UserPrincipal 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

UserPrincipal (org.thingsboard.server.service.security.model.UserPrincipal)12 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)7 Claims (io.jsonwebtoken.Claims)4 User (org.thingsboard.server.common.data.User)4 AuthenticationException (org.springframework.security.core.AuthenticationException)3 UserId (org.thingsboard.server.common.data.id.UserId)3 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ZonedDateTime (java.time.ZonedDateTime)2 ServletException (javax.servlet.ServletException)2 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2 CustomerId (org.thingsboard.server.common.data.id.CustomerId)2 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)2 AuthMethodNotSupportedException (org.thingsboard.server.service.security.exception.AuthMethodNotSupportedException)2 JwtToken (org.thingsboard.server.service.security.model.token.JwtToken)2