Skip to content

Commit

Permalink
Simplify withings auth config part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Jul 15, 2023
1 parent 6010a57 commit 2cbbe12
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.JdbcOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.web.SecurityFilterChain;

import io.github.mucsi96.kubetools.security.KubetoolsSecurityConfigurer;
import mucsi96.traininglog.oauth.AccessTokenResponseClient;
import mucsi96.traininglog.oauth.AuthorizedClientManager;
import mucsi96.traininglog.oauth.RefreshTokenResponseClient;

@Configuration
@EnableWebSecurity
Expand All @@ -40,15 +36,4 @@ public OAuth2AuthorizedClientService oAuth2AuthorizedClientService(
JdbcOperations jdbcOperations, ClientRegistrationRepository clientRegistrationRepository) {
return new JdbcOAuth2AuthorizedClientService(jdbcOperations, clientRegistrationRepository);
}

@Bean
OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository,
RefreshTokenResponseClient refreshTokenResponseClient) {
return new AuthorizedClientManager(
clientRegistrationRepository,
authorizedClientRepository,
refreshTokenResponseClient);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class WithingsController {

private final WithingsService withingsService;
private final WeightService weightService;
private final OAuth2AuthorizedClientManager authorizedClientManager;
private final OAuth2AuthorizedClientManager withingsAuthorizedClientManager;

@PostMapping("/sync")
public void sync(
Expand All @@ -43,7 +43,7 @@ public void sync(
.build();

try {
OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest);
OAuth2AuthorizedClient authorizedClient = withingsAuthorizedClientManager.authorize(authorizeRequest);
if (!weightService.getTodayWeight().isPresent()) {
withingsService.getTodayWeight(authorizedClient).ifPresent(weightService::saveWeight);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mucsi96.traininglog.withings.oauth;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;

@Configuration
public class WithingsOAuthConfiguration {

@Bean
OAuth2AuthorizedClientManager withingsAuthorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository,
WithingsRefreshTokenResponseClient refreshTokenResponseClient) {

DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);

OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
.authorizationCode()
.refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenResponseClient))
.build();

authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

return authorizedClientManager;
}
}

0 comments on commit 2cbbe12

Please sign in to comment.