|
|
|
@ -24,9 +24,9 @@ import io.jsonwebtoken.MalformedJwtException; |
|
|
|
import io.jsonwebtoken.SignatureAlgorithm; |
|
|
|
import io.jsonwebtoken.SignatureException; |
|
|
|
import io.jsonwebtoken.UnsupportedJwtException; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
|
import org.springframework.security.core.GrantedAuthority; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
@ -35,7 +35,7 @@ import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
|
import org.thingsboard.server.common.data.security.model.JwtToken; |
|
|
|
import org.thingsboard.server.config.JwtSettings; |
|
|
|
import org.thingsboard.server.config.JwtSettingsService; |
|
|
|
import org.thingsboard.server.service.security.exception.JwtExpiredTokenException; |
|
|
|
import org.thingsboard.server.service.security.model.JwtTokenPair; |
|
|
|
import org.thingsboard.server.service.security.model.SecurityUser; |
|
|
|
@ -49,6 +49,7 @@ import java.util.UUID; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@Component |
|
|
|
@RequiredArgsConstructor |
|
|
|
@Slf4j |
|
|
|
public class JwtTokenFactory { |
|
|
|
|
|
|
|
@ -61,12 +62,7 @@ public class JwtTokenFactory { |
|
|
|
private static final String TENANT_ID = "tenantId"; |
|
|
|
private static final String CUSTOMER_ID = "customerId"; |
|
|
|
|
|
|
|
private final JwtSettings settings; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
public JwtTokenFactory(JwtSettings settings) { |
|
|
|
this.settings = settings; |
|
|
|
} |
|
|
|
private final JwtSettingsService jwtSettingsService; |
|
|
|
|
|
|
|
/** |
|
|
|
* Factory method for issuing new JWT Tokens. |
|
|
|
@ -79,7 +75,7 @@ public class JwtTokenFactory { |
|
|
|
UserPrincipal principal = securityUser.getUserPrincipal(); |
|
|
|
|
|
|
|
JwtBuilder jwtBuilder = setUpToken(securityUser, securityUser.getAuthorities().stream() |
|
|
|
.map(GrantedAuthority::getAuthority).collect(Collectors.toList()), settings.getTokenExpirationTime()); |
|
|
|
.map(GrantedAuthority::getAuthority).collect(Collectors.toList()), jwtSettingsService.getJwtSettings().getTokenExpirationTime()); |
|
|
|
jwtBuilder.claim(FIRST_NAME, securityUser.getFirstName()) |
|
|
|
.claim(LAST_NAME, securityUser.getLastName()) |
|
|
|
.claim(ENABLED, securityUser.isEnabled()) |
|
|
|
@ -138,7 +134,7 @@ public class JwtTokenFactory { |
|
|
|
public JwtToken createRefreshToken(SecurityUser securityUser) { |
|
|
|
UserPrincipal principal = securityUser.getUserPrincipal(); |
|
|
|
|
|
|
|
String token = setUpToken(securityUser, Collections.singletonList(Authority.REFRESH_TOKEN.name()), settings.getRefreshTokenExpTime()) |
|
|
|
String token = setUpToken(securityUser, Collections.singletonList(Authority.REFRESH_TOKEN.name()), jwtSettingsService.getJwtSettings().getRefreshTokenExpTime()) |
|
|
|
.claim(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID) |
|
|
|
.setId(UUID.randomUUID().toString()).compact(); |
|
|
|
|
|
|
|
@ -188,16 +184,16 @@ public class JwtTokenFactory { |
|
|
|
|
|
|
|
return Jwts.builder() |
|
|
|
.setClaims(claims) |
|
|
|
.setIssuer(settings.getTokenIssuer()) |
|
|
|
.setIssuer(jwtSettingsService.getJwtSettings().getTokenIssuer()) |
|
|
|
.setIssuedAt(Date.from(currentTime.toInstant())) |
|
|
|
.setExpiration(Date.from(currentTime.plusSeconds(expirationTime).toInstant())) |
|
|
|
.signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()); |
|
|
|
.signWith(SignatureAlgorithm.HS512, jwtSettingsService.getJwtSettings().getTokenSigningKey()); |
|
|
|
} |
|
|
|
|
|
|
|
public Jws<Claims> parseTokenClaims(JwtToken token) { |
|
|
|
try { |
|
|
|
return Jwts.parser() |
|
|
|
.setSigningKey(settings.getTokenSigningKey()) |
|
|
|
.setSigningKey(jwtSettingsService.getJwtSettings().getTokenSigningKey()) |
|
|
|
.parseClaimsJws(token.getToken()); |
|
|
|
} catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException | SignatureException ex) { |
|
|
|
log.debug("Invalid JWT Token", ex); |
|
|
|
|