|
@@ -19,6 +19,9 @@ import com.nimbusds.jose.JOSEException;
|
|
|
import com.nimbusds.jose.JWSAlgorithm;
|
|
|
import com.nimbusds.jose.jwk.JWK;
|
|
|
import com.nimbusds.jose.jwk.JWKSelector;
|
|
|
+import com.nimbusds.jose.jwk.JWKSet;
|
|
|
+import com.nimbusds.jose.jwk.RSAKey;
|
|
|
+import com.nimbusds.jose.jwk.source.ImmutableJWKSet;
|
|
|
import com.nimbusds.jose.jwk.source.JWKSource;
|
|
|
import com.nimbusds.jose.proc.BadJOSEException;
|
|
|
import com.nimbusds.jose.proc.JWSKeySelector;
|
|
@@ -33,6 +36,7 @@ import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
|
|
import org.springframework.util.Assert;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
+import java.security.interfaces.RSAPublicKey;
|
|
|
import java.time.Instant;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
@@ -55,32 +59,37 @@ import java.util.Map;
|
|
|
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7517">JSON Web Key (JWK)</a>
|
|
|
* @see <a target="_blank" href="https://connect2id.com/products/nimbus-jose-jwt">Nimbus JOSE + JWT SDK</a>
|
|
|
*/
|
|
|
-public final class NimbusJwkReactiveJwtDecoder implements ReactiveJwtDecoder {
|
|
|
+public final class NimbusReactiveJwtDecoder implements ReactiveJwtDecoder {
|
|
|
private final JWTProcessor<JWKContext> jwtProcessor;
|
|
|
|
|
|
- private final ReactiveRemoteJWKSource reactiveJwkSource;
|
|
|
+ private final ReactiveJWKSource reactiveJwkSource;
|
|
|
|
|
|
private final JWKSelectorFactory jwkSelectorFactory;
|
|
|
|
|
|
- /**
|
|
|
- * Constructs a {@code NimbusJwtDecoderJwkSupport} using the provided parameters.
|
|
|
- *
|
|
|
- * @param jwkSetUrl the JSON Web Key (JWK) Set {@code URL}
|
|
|
- */
|
|
|
- public NimbusJwkReactiveJwtDecoder(String jwkSetUrl) {
|
|
|
- this(jwkSetUrl, JwsAlgorithms.RS256);
|
|
|
+ public NimbusReactiveJwtDecoder(RSAPublicKey publicKey) {
|
|
|
+ JWSAlgorithm algorithm = JWSAlgorithm.parse(JwsAlgorithms.RS256);
|
|
|
+
|
|
|
+ RSAKey rsaKey = rsaKey(publicKey);
|
|
|
+ JWKSet jwkSet = new JWKSet(rsaKey);
|
|
|
+ JWKSource jwkSource = new ImmutableJWKSet<>(jwkSet);
|
|
|
+ JWSKeySelector<JWKContext> jwsKeySelector =
|
|
|
+ new JWSVerificationKeySelector<>(algorithm, jwkSource);
|
|
|
+ DefaultJWTProcessor jwtProcessor = new DefaultJWTProcessor<>();
|
|
|
+ jwtProcessor.setJWSKeySelector(jwsKeySelector);
|
|
|
+
|
|
|
+ this.jwtProcessor = jwtProcessor;
|
|
|
+ this.reactiveJwkSource = new ReactiveJWKSourceAdapter(jwkSource);
|
|
|
+ this.jwkSelectorFactory = new JWKSelectorFactory(algorithm);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Constructs a {@code NimbusJwtDecoderJwkSupport} using the provided parameters.
|
|
|
*
|
|
|
* @param jwkSetUrl the JSON Web Key (JWK) Set {@code URL}
|
|
|
- * @param jwsAlgorithm the JSON Web Algorithm (JWA) used for verifying the digital signatures
|
|
|
*/
|
|
|
- public NimbusJwkReactiveJwtDecoder(String jwkSetUrl, String jwsAlgorithm) {
|
|
|
+ public NimbusReactiveJwtDecoder(String jwkSetUrl) {
|
|
|
Assert.hasText(jwkSetUrl, "jwkSetUrl cannot be empty");
|
|
|
- Assert.hasText(jwsAlgorithm, "jwsAlgorithm cannot be empty");
|
|
|
-
|
|
|
+ String jwsAlgorithm = JwsAlgorithms.RS256;
|
|
|
JWSAlgorithm algorithm = JWSAlgorithm.parse(jwsAlgorithm);
|
|
|
JWKSource jwkSource = new JWKContextJWKSource();
|
|
|
JWSKeySelector<JWKContext> jwsKeySelector =
|
|
@@ -152,4 +161,9 @@ public final class NimbusJwkReactiveJwtDecoder implements ReactiveJwtDecoder {
|
|
|
|
|
|
return new Jwt(parsedJwt.getParsedString(), issuedAt, expiresAt, headers, jwtClaimsSet.getClaims());
|
|
|
}
|
|
|
+
|
|
|
+ private static RSAKey rsaKey(RSAPublicKey publicKey) {
|
|
|
+ return new RSAKey.Builder(publicKey)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
}
|