-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df61330
commit aa6f6ba
Showing
15 changed files
with
1,024 additions
and
61 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
96 changes: 96 additions & 0 deletions
96
...wt/src/main/java/io/micronaut/security/token/jwt/signature/jwks/HttpClientJwksClient.java
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.security.token.jwt.signature.jwks; | ||
|
||
import io.micronaut.context.BeanContext; | ||
import io.micronaut.context.annotation.Primary; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.util.SupplierUtil; | ||
import io.micronaut.http.client.HttpClient; | ||
import io.micronaut.http.client.HttpClientConfiguration; | ||
import io.micronaut.http.client.HttpClientRegistry; | ||
import io.micronaut.http.client.HttpVersionSelection; | ||
import io.micronaut.http.client.LoadBalancer; | ||
import io.micronaut.http.client.ServiceHttpClientConfiguration; | ||
import io.micronaut.http.client.exceptions.HttpClientException; | ||
import io.micronaut.inject.qualifiers.Qualifiers; | ||
import jakarta.inject.Singleton; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Implementation of {@link JwksClient} that uses the Micronaut {@link HttpClient}. | ||
* | ||
* <p> | ||
* If a named service-specific client is configured (i.e. with "micronaut.http.services.foo.*") with a | ||
* name that matches the name used for security configuration (i.e. "micronaut.security.token.jwt.signatures.jwks.foo.*") | ||
* then that client will be used for the request. Otherwise, a default client will be used. | ||
* | ||
* @author Jeremy Grelle | ||
* @since 4.5.0 | ||
*/ | ||
@Singleton | ||
@Primary | ||
@Requires(classes = HttpClient.class) | ||
public class HttpClientJwksClient implements JwksClient { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(HttpClientJwksClient.class); | ||
|
||
private final BeanContext beanContext; | ||
private final HttpClientRegistry<HttpClient> clientRegistry; | ||
private final Supplier<HttpClient> defaultJwkSetClient; | ||
private final ConcurrentHashMap<String, HttpClient> jwkSetClients = new ConcurrentHashMap<>(); | ||
|
||
public HttpClientJwksClient(BeanContext beanContext, HttpClientRegistry<HttpClient> clientRegistry, HttpClientConfiguration defaultClientConfiguration) { | ||
this.beanContext = beanContext; | ||
this.clientRegistry = clientRegistry; | ||
this.defaultJwkSetClient = SupplierUtil.memoized(() -> beanContext.createBean(HttpClient.class, LoadBalancer.empty(), defaultClientConfiguration)); | ||
} | ||
|
||
@Override | ||
public String load(@Nullable String providerName, @NonNull String url) throws HttpClientException { | ||
try { | ||
return Mono.from(getClient(providerName).retrieve(url)).block(); | ||
} catch (HttpClientException e) { | ||
if (LOG.isErrorEnabled()) { | ||
LOG.error("Exception loading JWK from " + url, e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Retrieves an HTTP client for the given provider. | ||
* | ||
* @param providerName The provider name | ||
* @return An HTTP client to use to send the JWKS request | ||
*/ | ||
protected HttpClient getClient(@Nullable String providerName) { | ||
if (providerName == null) { | ||
return defaultJwkSetClient.get(); | ||
} | ||
return jwkSetClients.computeIfAbsent(providerName, provider -> | ||
beanContext.findBean(ServiceHttpClientConfiguration.class, Qualifiers.byName(provider)) | ||
.map(serviceConfig -> this.clientRegistry.getClient(HttpVersionSelection.forClientConfiguration(serviceConfig), provider, "/")) | ||
.orElseGet(defaultJwkSetClient)); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
security-jwt/src/main/java/io/micronaut/security/token/jwt/signature/jwks/JwksClient.java
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.security.token.jwt.signature.jwks; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
|
||
/** | ||
* Client for loading Json Web Key Set content over http. | ||
* | ||
* @author Jeremy Grelle | ||
* @since 4.5.0 | ||
*/ | ||
public interface JwksClient { | ||
|
||
/** | ||
* Loads remote Json Web Key Set content over http. | ||
* | ||
* @param providerName The jwks provider name | ||
* @param url The URL for loading the remote JWK Set | ||
* @return The JWK Set response body content | ||
*/ | ||
@Nullable | ||
String load(@Nullable String providerName, @NonNull String url); | ||
} |
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
Oops, something went wrong.