@@ -545,108 +545,6 @@ public void close() {
545545
546546 }
547547
548- /**
549- * A builder for creating {@link NimbusJwtDecoder} instances based on a
550- * {@code JWKSource}.
551- */
552- public static final class JwkSourceJwtDecoderBuilder {
553-
554- private static final JOSEObjectTypeVerifier <SecurityContext > NO_TYPE_VERIFIER = (header , context ) -> {
555- };
556-
557- private final Function <JWKSource <SecurityContext >, Set <JWSAlgorithm >> defaultAlgorithms = (source ) -> Set
558- .of (JWSAlgorithm .RS256 );
559-
560- private final JOSEObjectTypeVerifier <SecurityContext > typeVerifier = NO_TYPE_VERIFIER ;
561-
562- private final Set <SignatureAlgorithm > signatureAlgorithms = new HashSet <>();
563-
564- private Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ;
565-
566- private final JWKSource <SecurityContext > jwkSource ;
567-
568- private JwkSourceJwtDecoderBuilder (JWKSource <SecurityContext > jwkSource ) {
569- Assert .notNull (jwkSource , "jwkSource cannot be null" );
570- this .jwkSource = jwkSource ;
571- this .jwtProcessorCustomizer = (processor ) -> {
572- };
573- }
574-
575- /**
576- * Append the given signing
577- * <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1" target=
578- * "_blank">algorithm</a> to the set of algorithms to use.
579- * @param signatureAlgorithm the algorithm to use
580- * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
581- */
582- public JwkSourceJwtDecoderBuilder jwsAlgorithm (SignatureAlgorithm signatureAlgorithm ) {
583- Assert .notNull (signatureAlgorithm , "signatureAlgorithm cannot be null" );
584- this .signatureAlgorithms .add (signatureAlgorithm );
585- return this ;
586- }
587-
588- /**
589- * Configure the list of
590- * <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1" target=
591- * "_blank">algorithms</a> to use with the given {@link Consumer}.
592- * @param signatureAlgorithmsConsumer a {@link Consumer} for further configuring
593- * the algorithm list
594- * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
595- */
596- public JwkSourceJwtDecoderBuilder jwsAlgorithms (Consumer <Set <SignatureAlgorithm >> signatureAlgorithmsConsumer ) {
597- Assert .notNull (signatureAlgorithmsConsumer , "signatureAlgorithmsConsumer cannot be null" );
598- signatureAlgorithmsConsumer .accept (this .signatureAlgorithms );
599- return this ;
600- }
601-
602- /**
603- * Use the given {@link Consumer} to customize the {@link JWTProcessor
604- * ConfigurableJWTProcessor} before passing it to the build
605- * {@link NimbusJwtDecoder}.
606- * @param jwtProcessorCustomizer the callback used to alter the processor
607- * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
608- * @since 5.4
609- */
610- public JwkSourceJwtDecoderBuilder jwtProcessorCustomizer (
611- Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ) {
612- Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
613- this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
614- return this ;
615- }
616-
617- JWSKeySelector <SecurityContext > jwsKeySelector (JWKSource <SecurityContext > jwkSource ) {
618- if (this .signatureAlgorithms .isEmpty ()) {
619- return new JWSVerificationKeySelector <>(this .defaultAlgorithms .apply (jwkSource ), jwkSource );
620- }
621- Set <JWSAlgorithm > jwsAlgorithms = new HashSet <>();
622- for (SignatureAlgorithm signatureAlgorithm : this .signatureAlgorithms ) {
623- JWSAlgorithm jwsAlgorithm = JWSAlgorithm .parse (signatureAlgorithm .getName ());
624- jwsAlgorithms .add (jwsAlgorithm );
625- }
626- return new JWSVerificationKeySelector <>(jwsAlgorithms , jwkSource );
627- }
628-
629- JWTProcessor <SecurityContext > processor () {
630- ConfigurableJWTProcessor <SecurityContext > jwtProcessor = new DefaultJWTProcessor <>();
631- jwtProcessor .setJWSTypeVerifier (this .typeVerifier );
632- jwtProcessor .setJWSKeySelector (jwsKeySelector (this .jwkSource ));
633- // Spring Security validates the claim set independent from Nimbus
634- jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {
635- });
636- this .jwtProcessorCustomizer .accept (jwtProcessor );
637- return jwtProcessor ;
638- }
639-
640- /**
641- * Build the configured {@link NimbusJwtDecoder}.
642- * @return the configured {@link NimbusJwtDecoder}
643- */
644- public NimbusJwtDecoder build () {
645- return new NimbusJwtDecoder (processor ());
646- }
647-
648- }
649-
650548 /**
651549 * A builder for creating {@link NimbusJwtDecoder} instances based on a public key.
652550 */
@@ -903,4 +801,108 @@ JWTProcessor<SecurityContext> processor() {
903801
904802 }
905803
804+ /**
805+ * A builder for creating {@link NimbusJwtDecoder} instances based on a
806+ * {@code JWKSource}.
807+ *
808+ * @since 7.0
809+ */
810+ public static final class JwkSourceJwtDecoderBuilder {
811+
812+ private static final JOSEObjectTypeVerifier <SecurityContext > NO_TYPE_VERIFIER = (header , context ) -> {
813+ };
814+
815+ private final Function <JWKSource <SecurityContext >, Set <JWSAlgorithm >> defaultAlgorithms = (source ) -> Set
816+ .of (JWSAlgorithm .RS256 );
817+
818+ private final JOSEObjectTypeVerifier <SecurityContext > typeVerifier = NO_TYPE_VERIFIER ;
819+
820+ private final Set <SignatureAlgorithm > signatureAlgorithms = new HashSet <>();
821+
822+ private Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ;
823+
824+ private final JWKSource <SecurityContext > jwkSource ;
825+
826+ private JwkSourceJwtDecoderBuilder (JWKSource <SecurityContext > jwkSource ) {
827+ Assert .notNull (jwkSource , "jwkSource cannot be null" );
828+ this .jwkSource = jwkSource ;
829+ this .jwtProcessorCustomizer = (processor ) -> {
830+ };
831+ }
832+
833+ /**
834+ * Append the given signing
835+ * <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1" target=
836+ * "_blank">algorithm</a> to the set of algorithms to use.
837+ * @param signatureAlgorithm the algorithm to use
838+ * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
839+ */
840+ public JwkSourceJwtDecoderBuilder jwsAlgorithm (SignatureAlgorithm signatureAlgorithm ) {
841+ Assert .notNull (signatureAlgorithm , "signatureAlgorithm cannot be null" );
842+ this .signatureAlgorithms .add (signatureAlgorithm );
843+ return this ;
844+ }
845+
846+ /**
847+ * Configure the list of
848+ * <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1" target=
849+ * "_blank">algorithms</a> to use with the given {@link Consumer}.
850+ * @param signatureAlgorithmsConsumer a {@link Consumer} for further configuring
851+ * the algorithm list
852+ * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
853+ */
854+ public JwkSourceJwtDecoderBuilder jwsAlgorithms (Consumer <Set <SignatureAlgorithm >> signatureAlgorithmsConsumer ) {
855+ Assert .notNull (signatureAlgorithmsConsumer , "signatureAlgorithmsConsumer cannot be null" );
856+ signatureAlgorithmsConsumer .accept (this .signatureAlgorithms );
857+ return this ;
858+ }
859+
860+ /**
861+ * Use the given {@link Consumer} to customize the {@link JWTProcessor
862+ * ConfigurableJWTProcessor} before passing it to the build
863+ * {@link NimbusJwtDecoder}.
864+ * @param jwtProcessorCustomizer the callback used to alter the processor
865+ * @return a {@link JwkSourceJwtDecoderBuilder } for further configurations
866+ * @since 5.4
867+ */
868+ public JwkSourceJwtDecoderBuilder jwtProcessorCustomizer (
869+ Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ) {
870+ Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
871+ this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
872+ return this ;
873+ }
874+
875+ JWSKeySelector <SecurityContext > jwsKeySelector (JWKSource <SecurityContext > jwkSource ) {
876+ if (this .signatureAlgorithms .isEmpty ()) {
877+ return new JWSVerificationKeySelector <>(this .defaultAlgorithms .apply (jwkSource ), jwkSource );
878+ }
879+ Set <JWSAlgorithm > jwsAlgorithms = new HashSet <>();
880+ for (SignatureAlgorithm signatureAlgorithm : this .signatureAlgorithms ) {
881+ JWSAlgorithm jwsAlgorithm = JWSAlgorithm .parse (signatureAlgorithm .getName ());
882+ jwsAlgorithms .add (jwsAlgorithm );
883+ }
884+ return new JWSVerificationKeySelector <>(jwsAlgorithms , jwkSource );
885+ }
886+
887+ JWTProcessor <SecurityContext > processor () {
888+ ConfigurableJWTProcessor <SecurityContext > jwtProcessor = new DefaultJWTProcessor <>();
889+ jwtProcessor .setJWSTypeVerifier (this .typeVerifier );
890+ jwtProcessor .setJWSKeySelector (jwsKeySelector (this .jwkSource ));
891+ // Spring Security validates the claim set independent from Nimbus
892+ jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {
893+ });
894+ this .jwtProcessorCustomizer .accept (jwtProcessor );
895+ return jwtProcessor ;
896+ }
897+
898+ /**
899+ * Build the configured {@link NimbusJwtDecoder}.
900+ * @return the configured {@link NimbusJwtDecoder}
901+ */
902+ public NimbusJwtDecoder build () {
903+ return new NimbusJwtDecoder (processor ());
904+ }
905+
906+ }
907+
906908}
0 commit comments