|
|
|
@ -53,7 +53,7 @@ import com.nimbusds.jwt.SignedJWT;
|
|
|
|
|
|
|
|
|
|
public class DefaultJwtSigningAndValidationService implements JwtSigningAndValidationService {
|
|
|
|
|
|
|
|
|
|
public static final String ALG_NONE = "none"; // TODO storing a default "alg:none" id smells a bit..
|
|
|
|
|
public static final String ALG_NONE = "none";
|
|
|
|
|
|
|
|
|
|
// map of identifier to signer
|
|
|
|
|
private Map<String, JWSSigner> signers = new HashMap<String, JWSSigner>();
|
|
|
|
@ -131,7 +131,7 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param defaultSignerKeyId the defaultSignerKeyId to set
|
|
|
|
|
* @param defaultSignerKeyId the defaultSignerKeyId to set. The "none" key id is reserved for alg:none in this implementation.
|
|
|
|
|
*/
|
|
|
|
|
public void setDefaultSignerKeyId(String defaultSignerId) {
|
|
|
|
|
this.defaultSignerKeyId = defaultSignerId;
|
|
|
|
@ -165,8 +165,6 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
private void buildSignersAndVerifiers() throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
|
|
|
|
|
|
|
signers.put(ALG_NONE, new PlainSigner());
|
|
|
|
|
verifiers.put(ALG_NONE, new PlainVerifier());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, JWK> jwkEntry : keys.entrySet()) {
|
|
|
|
|
|
|
|
|
@ -230,10 +228,12 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
|
|
|
|
|
// At this point, this is a plain JWT and is already good-to-go.
|
|
|
|
|
|
|
|
|
|
} else { // we have a signable JWS at this point.
|
|
|
|
|
} else if (jwt instanceof SignedJWT) { // we have a signable JWS at this point.
|
|
|
|
|
|
|
|
|
|
((SignedJWT) jwt).sign(signer);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
logger.warn("Attempted to sign an unsupported JWT type.");
|
|
|
|
|
}
|
|
|
|
|
} catch (JOSEException e) {
|
|
|
|
|
|
|
|
|
@ -270,10 +270,13 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
|
|
|
|
|
// do nothing because PlainJWT is good already.
|
|
|
|
|
|
|
|
|
|
} else { // we have a signable JWS at this point.
|
|
|
|
|
} else if (jwt instanceof SignedJWT){ // we have a signable JWS at this point.
|
|
|
|
|
|
|
|
|
|
((SignedJWT) jwt).sign(signer);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
logger.warn("Attempted to sign an unsupported JWT type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JOSEException e) {
|
|
|
|
@ -285,21 +288,21 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
@Override
|
|
|
|
|
public boolean validateSignature(JWT jwt) {
|
|
|
|
|
|
|
|
|
|
if (getDefaultSigningAlgorithm().equals(JWSAlgorithm.NONE) {
|
|
|
|
|
if (getDefaultSignerKeyId().equals(ALG_NONE) && (jwt instanceof PlainJWT)) {
|
|
|
|
|
|
|
|
|
|
if (jwt instanceof PlainJWT) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (JWSVerifier verifier : verifiers.values()) {
|
|
|
|
|
try {
|
|
|
|
|
if (jwt.verify(verifier)) {
|
|
|
|
|
return true;
|
|
|
|
|
return PlainVerifier.verify((PlainJWT) jwt);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
for (JWSVerifier verifier : verifiers.values()) {
|
|
|
|
|
try {
|
|
|
|
|
if (((SignedJWT) jwt).verify(verifier)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (JOSEException e) {
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to validate signature, error was: ", e);
|
|
|
|
|
}
|
|
|
|
|
} catch (JOSEException e) {
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to validate signature, error was: ", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
@ -328,8 +331,6 @@ public class DefaultJwtSigningAndValidationService implements JwtSigningAndValid
|
|
|
|
|
public Collection<JWSAlgorithm> getAllSigningAlgsSupported() {
|
|
|
|
|
|
|
|
|
|
Set<JWSAlgorithm> algs = new HashSet<JWSAlgorithm>();
|
|
|
|
|
|
|
|
|
|
//TODO add 'none'
|
|
|
|
|
|
|
|
|
|
for (JWSSigner signer : signers.values()) {
|
|
|
|
|
algs.addAll(signer.supportedAlgorithms());
|
|
|
|
|