-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for EC JWT signing and logging
- Loading branch information
Showing
3 changed files
with
28 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,55 @@ | ||
package generiek.jwt; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jose.jwk.source.RemoteJWKSet; | ||
import com.nimbusds.jose.proc.BadJOSEException; | ||
import com.nimbusds.jose.proc.JWSKeySelector; | ||
import com.nimbusds.jose.proc.JWSVerificationKeySelector; | ||
import com.nimbusds.jose.proc.SecurityContext; | ||
import com.nimbusds.jose.util.DefaultResourceRetriever; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.proc.ConfigurableJWTProcessor; | ||
import com.nimbusds.jwt.proc.DefaultJWTProcessor; | ||
import generiek.api.EnrollmentEndpoint; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.text.ParseException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class JWTValidator { | ||
|
||
private static final Log LOG = LogFactory.getLog(EnrollmentEndpoint.class); | ||
|
||
private final String jwkSetUri; | ||
private final ConfigurableJWTProcessor<SecurityContext> jwtProcessor; | ||
|
||
public JWTValidator(String jwkSetUri, int connectTimeout, int readTimeout, int sizeLimit) throws MalformedURLException { | ||
this.jwkSetUri = jwkSetUri; | ||
this.jwtProcessor = new DefaultJWTProcessor<>(); | ||
DefaultResourceRetriever resourceRetriever = new DefaultResourceRetriever(connectTimeout, readTimeout, sizeLimit); | ||
RemoteJWKSet<SecurityContext> remoteJWKSet = new RemoteJWKSet<>(new URL(jwkSetUri), resourceRetriever); | ||
Set<JWSAlgorithm> jwsAlgs = new HashSet<>(); | ||
jwsAlgs.add(JWSAlgorithm.RS256); | ||
jwsAlgs.add(JWSAlgorithm.ES256); | ||
JWSKeySelector<SecurityContext> keySelector = | ||
new JWSVerificationKeySelector<>(JWSAlgorithm.RS256, remoteJWKSet); | ||
new JWSVerificationKeySelector<>(jwsAlgs, remoteJWKSet); | ||
this.jwtProcessor.setJWSKeySelector(keySelector); | ||
} | ||
|
||
@SneakyThrows | ||
public JWTClaimsSet validate(String jwtToken) { | ||
return jwtProcessor.process(jwtToken, null); | ||
public JWTClaimsSet validate(String jwtToken) throws ParseException, BadJOSEException, JOSEException { | ||
try { | ||
return jwtProcessor.process(jwtToken, null); | ||
} catch (ParseException | BadJOSEException | JOSEException e) { | ||
LOG.error(String.format("Error in validation of JWT token for token %s against jwkSetUri %s", | ||
jwtToken, this.jwkSetUri), e); | ||
throw e; | ||
} | ||
} | ||
|
||
} |