Skip to content

Commit d8acc12

Browse files
authored
Fix/jwt claim validation (#3753)
fix: jwks JWT validation and reuse consumer Signed-off-by: developharsh <harsh237hk@gmail.com>
1 parent b33e1be commit d8acc12

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/security/apiToken/ApiTokenAuthenticationProvider.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ApiTokenAuthenticationProvider implements AuthenticationProvider {
5858

5959
@NotNull
6060
private final Sw360UserService userService;
61+
private volatile JWTValidator jwtValidator;
6162

6263
@Override
6364
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
@@ -71,8 +72,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
7172
String tokenFromAuthentication = (String) authentication.getCredentials();
7273
if (Sw360ResourceServer.IS_JWKS_VALIDATION_ENABLED && authentication instanceof ApiTokenAuthentication
7374
&& ((ApiTokenAuthentication) authentication).getType() == AuthType.JWKS) {
74-
JWTValidator validator = new JWTValidator(Sw360ResourceServer.JWKS_ISSUER_URL,
75-
Sw360ResourceServer.JWKS_ENDPOINT_URL, Sw360ResourceServer.JWT_CLAIM_AUD);
75+
JWTValidator validator = getJwtValidator();
7676
JwtClaims jwtClaims = null;
7777
try {
7878
jwtClaims = validator.validateJWT(tokenFromAuthentication);
@@ -119,6 +119,21 @@ private User getUserFromTokenHash(String tokenHash) {
119119
}
120120
}
121121

122+
private JWTValidator getJwtValidator() {
123+
JWTValidator localValidator = jwtValidator;
124+
if (localValidator == null) {
125+
synchronized (this) {
126+
localValidator = jwtValidator;
127+
if (localValidator == null) {
128+
localValidator = new JWTValidator(Sw360ResourceServer.JWKS_ISSUER_URL,
129+
Sw360ResourceServer.JWKS_ENDPOINT_URL, Sw360ResourceServer.JWT_CLAIM_AUD);
130+
jwtValidator = localValidator;
131+
}
132+
}
133+
}
134+
return localValidator;
135+
}
136+
122137
private User getUserFromClientId(String clientId) {
123138
try {
124139
return userService.getUserFromClientId(clientId);

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/security/jwksvalidation/JWTValidator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import org.jose4j.keys.resolvers.HttpsJwksVerificationKeyResolver;
1919

2020
public class JWTValidator {
21-
JwtConsumer jwtConsumer;
21+
private static final int ALLOWED_CLOCK_SKEW_SECONDS = 30;
22+
private final JwtConsumer jwtConsumer;
2223

2324
/**
2425
* Creates a validator for JWT access tokens issued by the given PF instance.
@@ -30,9 +31,10 @@ public JWTValidator(String issuerUrl, String jwksurl, String aud) {
3031
HttpsJwks httpsJkws = new HttpsJwks(jwksurl);
3132
HttpsJwksVerificationKeyResolver httpsJwksKeyResolver = new HttpsJwksVerificationKeyResolver(httpsJkws);
3233
JwtConsumerBuilder jwtConsumerBuilder = new JwtConsumerBuilder()
33-
//TODO:Recheck
34-
// .setRequireExpirationTime()
35-
// .setAllowedClockSkewInSeconds(30)
34+
.setRequireExpirationTime()
35+
.setRequireIssuedAt()
36+
.setRequireNotBefore()
37+
.setAllowedClockSkewInSeconds(ALLOWED_CLOCK_SKEW_SECONDS)
3638
.setExpectedIssuer(issuerUrl)
3739
.setVerificationKeyResolver(httpsJwksKeyResolver);
3840
if (aud.isEmpty()) {

0 commit comments

Comments
 (0)