use of io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsClient in project micronaut-security by micronaut-projects.
the class ClientCredentialsHttpClientFilter method doFilter.
@Override
public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
Optional<OauthClientConfiguration> oauthClientOptional = getClientConfiguration(request);
if (!oauthClientOptional.isPresent()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Did not find any OAuth 2.0 client which should decorate the request with an access token received from client credentials request");
}
return chain.proceed(request);
}
OauthClientConfiguration oauthClient = oauthClientOptional.get();
Optional<ClientCredentialsClient> clientCredentialsClientOptional = getClient(oauthClient);
if (!clientCredentialsClientOptional.isPresent()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Could not retrieve client credentials client for OAuth 2.0 client {}", oauthClient.getName());
}
return chain.proceed(request);
}
ClientCredentialsTokenPropagator tokenHandler = getTokenHandler(oauthClient);
return Flux.from(clientCredentialsClientOptional.get().requestToken(getScope(oauthClient))).map(TokenResponse::getAccessToken).switchMap(accessToken -> {
if (StringUtils.isNotEmpty(accessToken)) {
tokenHandler.writeToken(request, accessToken);
}
return chain.proceed(request);
});
}
Aggregations