|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.jwt;
|
17 | 17 |
|
18 |
| -import java.security.interfaces.RSAPublicKey; |
19 |
| -import java.util.Collections; |
20 |
| -import java.util.HashMap; |
21 |
| -import java.util.HashSet; |
22 |
| -import java.util.LinkedHashMap; |
23 |
| -import java.util.Map; |
24 |
| -import java.util.Set; |
25 |
| -import java.util.function.Consumer; |
26 |
| -import java.util.function.Function; |
27 |
| -import javax.crypto.SecretKey; |
28 |
| - |
29 | 18 | import com.nimbusds.jose.Header;
|
30 | 19 | import com.nimbusds.jose.JOSEException;
|
31 | 20 | import com.nimbusds.jose.JWSAlgorithm;
|
|
47 | 36 | import com.nimbusds.jwt.SignedJWT;
|
48 | 37 | import com.nimbusds.jwt.proc.DefaultJWTProcessor;
|
49 | 38 | import com.nimbusds.jwt.proc.JWTProcessor;
|
50 |
| -import reactor.core.publisher.Flux; |
51 |
| -import reactor.core.publisher.Mono; |
52 |
| - |
53 | 39 | import org.springframework.core.convert.converter.Converter;
|
| 40 | +import org.springframework.security.oauth2.core.OAuth2Error; |
54 | 41 | import org.springframework.security.oauth2.core.OAuth2TokenValidator;
|
55 | 42 | import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
|
56 | 43 | import org.springframework.security.oauth2.jose.jws.JwsAlgorithm;
|
57 | 44 | import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
|
58 | 45 | import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
|
59 | 46 | import org.springframework.util.Assert;
|
| 47 | +import org.springframework.util.StringUtils; |
60 | 48 | import org.springframework.web.reactive.function.client.WebClient;
|
| 49 | +import reactor.core.publisher.Flux; |
| 50 | +import reactor.core.publisher.Mono; |
| 51 | + |
| 52 | +import javax.crypto.SecretKey; |
| 53 | +import java.security.interfaces.RSAPublicKey; |
| 54 | +import java.util.Collection; |
| 55 | +import java.util.Collections; |
| 56 | +import java.util.HashMap; |
| 57 | +import java.util.HashSet; |
| 58 | +import java.util.LinkedHashMap; |
| 59 | +import java.util.Map; |
| 60 | +import java.util.Set; |
| 61 | +import java.util.function.Consumer; |
| 62 | +import java.util.function.Function; |
61 | 63 |
|
62 | 64 | /**
|
63 |
| - * An implementation of a {@link ReactiveJwtDecoder} that "decodes" a |
64 |
| - * JSON Web Token (JWT) and additionally verifies it's digital signature if the JWT is a |
65 |
| - * JSON Web Signature (JWS). |
66 |
| - * |
67 |
| - * <p> |
68 |
| - * <b>NOTE:</b> This implementation uses the Nimbus JOSE + JWT SDK internally. |
69 |
| - * |
70 |
| - * @author Rob Winch |
71 |
| - * @author Joe Grandja |
| 65 | +* An implementation of a {@link ReactiveJwtDecoder} that "decodes" a |
| 66 | +* JSON Web Token (JWT) and additionally verifies it's digital signature if the JWT is a |
| 67 | +* JSON Web Signature (JWS). |
| 68 | +* |
| 69 | +* <p> |
| 70 | +* <b>NOTE:</b> This implementation uses the Nimbus JOSE + JWT SDK internally. |
| 71 | +* |
| 72 | +* @author Rob Winch |
| 73 | +* @author Joe Grandja |
72 | 74 | * @since 5.1
|
73 | 75 | * @see ReactiveJwtDecoder
|
74 | 76 | * @see <a target="_blank" href="https://tools.ietf.org/html/rfc7519">JSON Web Token (JWT)</a>
|
@@ -178,10 +180,16 @@ private Jwt createJwt(JWT parsedJwt, JWTClaimsSet jwtClaimsSet) {
|
178 | 180 |
|
179 | 181 | private Jwt validateJwt(Jwt jwt) {
|
180 | 182 | OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt);
|
181 |
| - |
182 |
| - if ( result.hasErrors() ) { |
183 |
| - String message = result.getErrors().iterator().next().getDescription(); |
184 |
| - throw new JwtValidationException(message, result.getErrors()); |
| 183 | + if (result.hasErrors()) { |
| 184 | + Collection<OAuth2Error> errors = result.getErrors(); |
| 185 | + String validationErrorString = "Unable to validate Jwt"; |
| 186 | + for (OAuth2Error oAuth2Error : errors) { |
| 187 | + if (!StringUtils.isEmpty(oAuth2Error.getDescription())) { |
| 188 | + validationErrorString = oAuth2Error.getDescription(); |
| 189 | + break; |
| 190 | + } |
| 191 | + } |
| 192 | + throw new JwtValidationException(validationErrorString, errors); |
185 | 193 | }
|
186 | 194 |
|
187 | 195 | return jwt;
|
|
0 commit comments