io.convergence_platform.common.validations.ValidateJwtValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.validations;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Base64;
public class ValidateJwtValidator implements ConstraintValidator {
public static void pathVariable(String value, String varName) {
boolean valid = true;
if ("".equals(value)) {
valid = false;
} else {
valid = validate(value);
}
ValidationHelper.raiseValidationErrorForPathVariable(valid, "jwt", varName);
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return validate(value);
}
private static boolean validate(String value) {
if (value == null) {
return false;
}
return isJWT(value);
}
private static boolean isJWT(String jwt) {
String[] jwtSplitted = jwt.split("\\.");
if (jwtSplitted.length != 3) // The JWT is composed of three parts
return false;
try {
String jsonFirstPart = new String(Base64.getDecoder().decode(jwtSplitted[0]));
if (!jsonFirstPart.contains("\"alg\":")) // The first part has the attribute "alg"
return false;
Base64.getDecoder().decode(jwtSplitted[1]);
} catch (Exception err) {
return false;
}
return true;
}
}