Skip to content

Commit

Permalink
minor cleanup after JWT caching PR
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Nov 3, 2021
1 parent fc3586d commit d1e2458
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.eclipse.ditto.base.model.common.BinaryValidationResult;
import org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayAuthenticationFailedException;
import org.eclipse.ditto.gateway.service.util.config.security.OAuthConfig;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,22 +34,19 @@ public final class DefaultJwtValidator implements JwtValidator {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJwtValidator.class);

private final PublicKeyProvider publicKeyProvider;
private final OAuthConfig oAuthConfig;

private DefaultJwtValidator(final PublicKeyProvider publicKeyProvider, final OAuthConfig oAuthConfig) {
private DefaultJwtValidator(final PublicKeyProvider publicKeyProvider) {
this.publicKeyProvider = publicKeyProvider;
this.oAuthConfig = oAuthConfig;
}

/**
* Creates a new {@code JwtValidator} instance.
*
* @param publicKeyProvider provider for public keys of jwt issuers.
* @param oAuthConfig the OAuth config.
* @return the instance.
*/
public static JwtValidator of(final PublicKeyProvider publicKeyProvider, final OAuthConfig oAuthConfig) {
return new DefaultJwtValidator(publicKeyProvider, oAuthConfig);
public static JwtValidator of(final PublicKeyProvider publicKeyProvider) {
return new DefaultJwtValidator(publicKeyProvider);
}

@Override
Expand All @@ -60,7 +56,8 @@ public CompletableFuture<BinaryValidationResult> validate(final JsonWebToken jso

return publicKeyProvider.getPublicKeyWithParser(issuer, keyId)
.thenApply(publicKeyWithParserOpt -> publicKeyWithParserOpt
.map(publicKeyWithParser -> tryToValidateWithJwtParser(jsonWebToken, publicKeyWithParser.getJwtParser()))
.map(publicKeyWithParser -> tryToValidateWithJwtParser(jsonWebToken,
publicKeyWithParser.getJwtParser()))
.orElseGet(() -> {
final var msgPattern = "Public Key of issuer <{0}> with key ID <{1}> not found!";
final var msg = MessageFormat.format(msgPattern, issuer, keyId);
Expand All @@ -70,7 +67,8 @@ public CompletableFuture<BinaryValidationResult> validate(final JsonWebToken jso
}));
}

private BinaryValidationResult tryToValidateWithJwtParser(final JsonWebToken jsonWebToken, final JwtParser jwtParser) {
private BinaryValidationResult tryToValidateWithJwtParser(final JsonWebToken jsonWebToken,
final JwtParser jwtParser) {
try {
return validateWithJwtParser(jsonWebToken, jwtParser);
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static JwtAuthenticationFactory newInstance(final OAuthConfig oAuthConfig

public JwtValidator getJwtValidator() {
if (null == jwtValidator) {
jwtValidator = DefaultJwtValidator.of(getPublicKeyProvider(), oAuthConfig);
jwtValidator = DefaultJwtValidator.of(getPublicKeyProvider());
}
return jwtValidator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ final class PublicKeyWithParser {
*
* @param publicKey publicKey to use for jwt parsing.
* @param jwtParser the actual jwtParser using the given publicKey.
* @return the instance.
*/
PublicKeyWithParser(final PublicKey publicKey, final JwtParser jwtParser) {
this.publicKey = publicKey;
Expand Down Expand Up @@ -62,9 +61,9 @@ public int hashCode() {

@Override
public String toString() {
return "PublicKeyWithParser{" +
"publicKey=" + publicKey +
", jwtParser=" + jwtParser +
'}';
return getClass().getSimpleName() + " [" +
"publicKey=" + publicKey +
", jwtParser=" + jwtParser +
"]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void validate() throws ExecutionException, InterruptedException {
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));

final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider, oAuthConfig);
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);

final BinaryValidationResult jwtValidationResult = underTest.validate(VALID_JSON_WEB_TOKEN).get();

Expand All @@ -107,7 +107,7 @@ public void validateTokenWithNbfAheadOfTime() throws ExecutionException, Interru
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));

final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider, oAuthConfig);
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);

final BinaryValidationResult jwtValidationResult =
underTest.validate(VALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME).get();
Expand All @@ -120,7 +120,7 @@ public void validateFailsIfNbfIsTooFarInTheFuture() throws ExecutionException, I
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));

final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider, oAuthConfig);
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);

final BinaryValidationResult jwtValidationResult =
underTest.validate(INVALID_JSON_WEB_TOKEN_WITH_NBF_AHEAD_OF_TIME).get();
Expand All @@ -133,7 +133,7 @@ public void validateFailsIfSignatureIsMissing() throws ExecutionException, Inter
when(publicKeyProvider.getPublicKeyWithParser(JwtTestConstants.ISSUER, JwtTestConstants.KEY_ID)).thenReturn(
CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY, getJwtParser(JwtTestConstants.PUBLIC_KEY)))));

final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider, oAuthConfig);
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);

final BinaryValidationResult jwtValidationResult =
underTest.validate(VALID_JSON_WEB_TOKEN_WITHOUT_SIGNATURE).get();
Expand All @@ -148,7 +148,7 @@ public void validateFails() throws ExecutionException, InterruptedException {
.thenReturn(CompletableFuture.completedFuture(Optional.of(new PublicKeyWithParser(JwtTestConstants.PUBLIC_KEY,
getJwtParser(JwtTestConstants.PUBLIC_KEY)))));

final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider, oAuthConfig);
final JwtValidator underTest = DefaultJwtValidator.of(publicKeyProvider);

final BinaryValidationResult jwtValidationResult = underTest.validate(INVALID_JSON_WEB_TOKEN).get();

Expand Down

0 comments on commit d1e2458

Please sign in to comment.