Skip to content

Commit 2cbbe12

Browse files
committed
Simplify withings auth config part 1
1 parent 6010a57 commit 2cbbe12

File tree

5 files changed

+35
-93
lines changed

5 files changed

+35
-93
lines changed

server/src/main/java/mucsi96/traininglog/core/SecurityConfiguration.java

-15
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
88
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
99
import org.springframework.security.oauth2.client.JdbcOAuth2AuthorizedClientService;
10-
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
1110
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
1211
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
13-
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
1412
import org.springframework.security.web.SecurityFilterChain;
1513

1614
import io.github.mucsi96.kubetools.security.KubetoolsSecurityConfigurer;
1715
import mucsi96.traininglog.oauth.AccessTokenResponseClient;
18-
import mucsi96.traininglog.oauth.AuthorizedClientManager;
19-
import mucsi96.traininglog.oauth.RefreshTokenResponseClient;
2016

2117
@Configuration
2218
@EnableWebSecurity
@@ -40,15 +36,4 @@ public OAuth2AuthorizedClientService oAuth2AuthorizedClientService(
4036
JdbcOperations jdbcOperations, ClientRegistrationRepository clientRegistrationRepository) {
4137
return new JdbcOAuth2AuthorizedClientService(jdbcOperations, clientRegistrationRepository);
4238
}
43-
44-
@Bean
45-
OAuth2AuthorizedClientManager authorizedClientManager(
46-
ClientRegistrationRepository clientRegistrationRepository,
47-
OAuth2AuthorizedClientRepository authorizedClientRepository,
48-
RefreshTokenResponseClient refreshTokenResponseClient) {
49-
return new AuthorizedClientManager(
50-
clientRegistrationRepository,
51-
authorizedClientRepository,
52-
refreshTokenResponseClient);
53-
}
5439
}

server/src/main/java/mucsi96/traininglog/oauth/AuthorizedClientManager.java

-37
This file was deleted.

server/src/main/java/mucsi96/traininglog/oauth/RefreshTokenResponseClient.java

-39
This file was deleted.

server/src/main/java/mucsi96/traininglog/withings/WithingsController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class WithingsController {
2828

2929
private final WithingsService withingsService;
3030
private final WeightService weightService;
31-
private final OAuth2AuthorizedClientManager authorizedClientManager;
31+
private final OAuth2AuthorizedClientManager withingsAuthorizedClientManager;
3232

3333
@PostMapping("/sync")
3434
public void sync(
@@ -43,7 +43,7 @@ public void sync(
4343
.build();
4444

4545
try {
46-
OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest);
46+
OAuth2AuthorizedClient authorizedClient = withingsAuthorizedClientManager.authorize(authorizeRequest);
4747
if (!weightService.getTodayWeight().isPresent()) {
4848
withingsService.getTodayWeight(authorizedClient).ifPresent(weightService::saveWeight);
4949
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package mucsi96.traininglog.withings.oauth;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
6+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
7+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
8+
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
9+
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
10+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
11+
12+
@Configuration
13+
public class WithingsOAuthConfiguration {
14+
15+
@Bean
16+
OAuth2AuthorizedClientManager withingsAuthorizedClientManager(
17+
ClientRegistrationRepository clientRegistrationRepository,
18+
OAuth2AuthorizedClientRepository authorizedClientRepository,
19+
WithingsRefreshTokenResponseClient refreshTokenResponseClient) {
20+
21+
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
22+
clientRegistrationRepository, authorizedClientRepository);
23+
24+
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
25+
.authorizationCode()
26+
.refreshToken(configurer -> configurer.accessTokenResponseClient(refreshTokenResponseClient))
27+
.build();
28+
29+
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
30+
31+
return authorizedClientManager;
32+
}
33+
}

0 commit comments

Comments
 (0)