|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.jwt; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | +import java.util.Collections; |
| 22 | + |
| 23 | +import com.nimbusds.jose.JWSAlgorithm; |
| 24 | +import com.nimbusds.jose.jwk.source.JWKSource; |
| 25 | +import com.nimbusds.jose.jwk.source.RemoteJWKSet; |
| 26 | +import com.nimbusds.jose.proc.JWSKeySelector; |
| 27 | +import com.nimbusds.jose.proc.JWSVerificationKeySelector; |
| 28 | +import com.nimbusds.jose.proc.SecurityContext; |
| 29 | +import com.nimbusds.jose.util.Resource; |
| 30 | +import com.nimbusds.jose.util.ResourceRetriever; |
| 31 | +import com.nimbusds.jwt.proc.ConfigurableJWTProcessor; |
| 32 | +import com.nimbusds.jwt.proc.DefaultJWTProcessor; |
| 33 | +import com.nimbusds.jwt.proc.JWTProcessor; |
| 34 | + |
| 35 | +import org.springframework.http.HttpHeaders; |
| 36 | +import org.springframework.http.HttpMethod; |
| 37 | +import org.springframework.http.MediaType; |
| 38 | +import org.springframework.http.RequestEntity; |
| 39 | +import org.springframework.http.ResponseEntity; |
| 40 | +import org.springframework.util.Assert; |
| 41 | +import org.springframework.web.client.RestOperations; |
| 42 | +import org.springframework.web.client.RestTemplate; |
| 43 | + |
| 44 | +/** |
| 45 | + * A collection of builders for creating Nimbus {@link JWTProcessor} instances. |
| 46 | + * |
| 47 | + * @author Josh Cummings |
| 48 | + * @since 5.2 |
| 49 | + * @see NimbusJwtDecoder |
| 50 | + */ |
| 51 | +public final class JwtProcessors { |
| 52 | + |
| 53 | + /** |
| 54 | + * Use the given |
| 55 | + * <a href="https://tools.ietf.org/html/rfc7517#section-5">JWK Set</a> uri. |
| 56 | + * |
| 57 | + * @param jwkSetUri the JWK Set uri to use |
| 58 | + * @return a {@link JwtProcessors} for further configurations |
| 59 | + */ |
| 60 | + public static JwkSetUriJwtProcessorBuilder withJwkSetUri(String jwkSetUri) { |
| 61 | + return new JwkSetUriJwtProcessorBuilder(jwkSetUri); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * A builder for creating Nimbus {@link JWTProcessor} instances based on a |
| 66 | + * <a target="_blank" href="https://tools.ietf.org/html/rfc7517#section-5">JWK Set</a> uri. |
| 67 | + */ |
| 68 | + public static final class JwkSetUriJwtProcessorBuilder { |
| 69 | + private String jwkSetUri; |
| 70 | + private JWSAlgorithm jwsAlgorithm = JWSAlgorithm.RS256; |
| 71 | + private RestOperations restOperations = new RestTemplate(); |
| 72 | + |
| 73 | + private JwkSetUriJwtProcessorBuilder(String jwkSetUri) { |
| 74 | + Assert.hasText(jwkSetUri, "jwkSetUri cannot be empty"); |
| 75 | + this.jwkSetUri = jwkSetUri; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Use the given signing |
| 80 | + * <a href="https://tools.ietf.org/html/rfc7515#section-4.1.1" target="_blank">algorithm</a>. |
| 81 | + * |
| 82 | + * @param jwsAlgorithm the algorithm to use |
| 83 | + * @return a {@link JwtProcessors} for further configurations |
| 84 | + */ |
| 85 | + public JwkSetUriJwtProcessorBuilder jwsAlgorithm(String jwsAlgorithm) { |
| 86 | + Assert.hasText(jwsAlgorithm, "jwsAlgorithm cannot be empty"); |
| 87 | + this.jwsAlgorithm = JWSAlgorithm.parse(jwsAlgorithm); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Use the given {@link RestOperations} to coordinate with the authorization servers indicated in the |
| 93 | + * <a href="https://tools.ietf.org/html/rfc7517#section-5">JWK Set</a> uri |
| 94 | + * as well as the |
| 95 | + * <a href="http://openid.net/specs/openid-connect-core-1_0.html#IssuerIdentifier">Issuer</a>. |
| 96 | + * |
| 97 | + * @param restOperations |
| 98 | + * @return |
| 99 | + */ |
| 100 | + public JwkSetUriJwtProcessorBuilder restOperations(RestOperations restOperations) { |
| 101 | + Assert.notNull(restOperations, "restOperations cannot be null"); |
| 102 | + this.restOperations = restOperations; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Build the configured {@link JwtDecoder}. |
| 108 | + * |
| 109 | + * @return the configured {@link JwtDecoder} |
| 110 | + */ |
| 111 | + public JWTProcessor<SecurityContext> build() { |
| 112 | + ResourceRetriever jwkSetRetriever = new RestOperationsResourceRetriever(this.restOperations); |
| 113 | + JWKSource<SecurityContext> jwkSource = new RemoteJWKSet<>(toURL(this.jwkSetUri), jwkSetRetriever); |
| 114 | + JWSKeySelector<SecurityContext> jwsKeySelector = |
| 115 | + new JWSVerificationKeySelector<>(this.jwsAlgorithm, jwkSource); |
| 116 | + ConfigurableJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>(); |
| 117 | + jwtProcessor.setJWSKeySelector(jwsKeySelector); |
| 118 | + |
| 119 | + // Spring Security validates the claim set independent from Nimbus |
| 120 | + jwtProcessor.setJWTClaimsSetVerifier((claims, context) -> { }); |
| 121 | + |
| 122 | + return jwtProcessor; |
| 123 | + } |
| 124 | + |
| 125 | + private static URL toURL(String url) { |
| 126 | + try { |
| 127 | + return new URL(url); |
| 128 | + } catch (MalformedURLException ex) { |
| 129 | + throw new IllegalArgumentException("Invalid JWK Set URL \"" + url + "\" : " + ex.getMessage(), ex); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static class RestOperationsResourceRetriever implements ResourceRetriever { |
| 134 | + private final RestOperations restOperations; |
| 135 | + |
| 136 | + RestOperationsResourceRetriever(RestOperations restOperations) { |
| 137 | + Assert.notNull(restOperations, "restOperations cannot be null"); |
| 138 | + this.restOperations = restOperations; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Resource retrieveResource(URL url) throws IOException { |
| 143 | + HttpHeaders headers = new HttpHeaders(); |
| 144 | + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); |
| 145 | + |
| 146 | + ResponseEntity<String> response; |
| 147 | + try { |
| 148 | + RequestEntity<Void> request = new RequestEntity<>(headers, HttpMethod.GET, url.toURI()); |
| 149 | + response = this.restOperations.exchange(request, String.class); |
| 150 | + } catch (Exception ex) { |
| 151 | + throw new IOException(ex); |
| 152 | + } |
| 153 | + |
| 154 | + if (response.getStatusCodeValue() != 200) { |
| 155 | + throw new IOException(response.toString()); |
| 156 | + } |
| 157 | + |
| 158 | + return new Resource(response.getBody(), "UTF-8"); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments