|
1 | 1 | package mucsi96.traininglog.withings;
|
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Set; |
| 6 | + |
3 | 7 | import org.springframework.boot.context.properties.ConfigurationProperties;
|
| 8 | +import org.springframework.context.annotation.Bean; |
4 | 9 | import org.springframework.context.annotation.Configuration;
|
| 10 | +import org.springframework.core.convert.converter.Converter; |
| 11 | +import org.springframework.http.converter.FormHttpMessageConverter; |
| 12 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 13 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; |
| 14 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; |
| 15 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; |
| 16 | +import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient; |
| 17 | +import org.springframework.security.oauth2.client.endpoint.DefaultRefreshTokenTokenResponseClient; |
| 18 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 19 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; |
| 20 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter; |
| 21 | +import org.springframework.security.oauth2.client.endpoint.OAuth2RefreshTokenGrantRequest; |
| 22 | +import org.springframework.security.oauth2.client.endpoint.OAuth2RefreshTokenGrantRequestEntityConverter; |
| 23 | +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
| 24 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 25 | +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager; |
| 26 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 28 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 29 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 30 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 31 | +import org.springframework.security.web.SecurityFilterChain; |
| 32 | +import org.springframework.util.LinkedMultiValueMap; |
| 33 | +import org.springframework.util.MultiValueMap; |
| 34 | +import org.springframework.web.client.RestTemplate; |
| 35 | + |
| 36 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 37 | +import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 38 |
|
| 39 | +import io.github.mucsi96.kubetools.security.KubetoolsSecurityConfigurer; |
6 | 40 | import lombok.Data;
|
7 | 41 |
|
8 | 42 | @Data
|
9 | 43 | @Configuration
|
10 | 44 | @ConfigurationProperties(prefix = "withings")
|
11 | 45 | public class WithingsConfiguration {
|
| 46 | + public static final String registrationId = "withings-client"; |
| 47 | + |
12 | 48 | private WithingsApiConfiguration api;
|
13 | 49 |
|
| 50 | + @Bean |
| 51 | + SecurityFilterChain withingsSecurityFilterChain( |
| 52 | + HttpSecurity http, |
| 53 | + KubetoolsSecurityConfigurer kubetoolsSecurityConfigurer) throws Exception { |
| 54 | + return kubetoolsSecurityConfigurer.configure(http) |
| 55 | + .securityMatcher("/withings/**") |
| 56 | + .oauth2Client(configurer -> configurer |
| 57 | + .authorizationCodeGrant(customizer -> customizer |
| 58 | + .accessTokenResponseClient(withingsAccessTokenResponseClient()))) |
| 59 | + .build(); |
| 60 | + } |
| 61 | + |
| 62 | + @Bean |
| 63 | + OAuth2AuthorizedClientManager withingsAuthorizedClientManager( |
| 64 | + ClientRegistrationRepository clientRegistrationRepository, |
| 65 | + OAuth2AuthorizedClientRepository authorizedClientRepository) { |
| 66 | + |
| 67 | + DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( |
| 68 | + clientRegistrationRepository, authorizedClientRepository); |
| 69 | + |
| 70 | + OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder() |
| 71 | + .authorizationCode() |
| 72 | + .refreshToken(configurer -> configurer.accessTokenResponseClient(withingsRefreshTokenResponseClient())) |
| 73 | + .build(); |
| 74 | + |
| 75 | + authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 76 | + |
| 77 | + return authorizedClientManager; |
| 78 | + } |
| 79 | + |
| 80 | + OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> withingsAccessTokenResponseClient() { |
| 81 | + OAuth2AuthorizationCodeGrantRequestEntityConverter requestEntityConverter = new OAuth2AuthorizationCodeGrantRequestEntityConverter(); |
| 82 | + requestEntityConverter.addParametersConverter(withingsAccessTokenRequestParametersConverter()); |
| 83 | + |
| 84 | + OAuth2AccessTokenResponseHttpMessageConverter accessTokenResponseConverter = new OAuth2AccessTokenResponseHttpMessageConverter(); |
| 85 | + accessTokenResponseConverter.setAccessTokenResponseConverter(withingsAccessTokenResponseConverter()); |
| 86 | + |
| 87 | + RestTemplate restTemplate = new RestTemplate(Arrays.asList( |
| 88 | + new FormHttpMessageConverter(), |
| 89 | + accessTokenResponseConverter)); |
| 90 | + |
| 91 | + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
| 92 | + |
| 93 | + DefaultAuthorizationCodeTokenResponseClient accessTokenResponseClient = new DefaultAuthorizationCodeTokenResponseClient(); |
| 94 | + accessTokenResponseClient.setRequestEntityConverter(requestEntityConverter); |
| 95 | + accessTokenResponseClient.setRestOperations(restTemplate); |
| 96 | + return accessTokenResponseClient; |
| 97 | + } |
| 98 | + |
| 99 | + OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> withingsRefreshTokenResponseClient() { |
| 100 | + OAuth2RefreshTokenGrantRequestEntityConverter requestEntityConverter = new OAuth2RefreshTokenGrantRequestEntityConverter(); |
| 101 | + requestEntityConverter.addParametersConverter(withingsRefreshTokenRequestParametersConverter()); |
| 102 | + |
| 103 | + OAuth2AccessTokenResponseHttpMessageConverter accessTokenResponseConverter = new OAuth2AccessTokenResponseHttpMessageConverter(); |
| 104 | + accessTokenResponseConverter.setAccessTokenResponseConverter(withingsAccessTokenResponseConverter()); |
| 105 | + |
| 106 | + RestTemplate restTemplate = new RestTemplate(Arrays.asList( |
| 107 | + new FormHttpMessageConverter(), |
| 108 | + accessTokenResponseConverter)); |
| 109 | + |
| 110 | + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
| 111 | + |
| 112 | + DefaultRefreshTokenTokenResponseClient tokenResponseClient = new DefaultRefreshTokenTokenResponseClient(); |
| 113 | + tokenResponseClient.setRequestEntityConverter(requestEntityConverter); |
| 114 | + tokenResponseClient.setRestOperations(restTemplate); |
| 115 | + return tokenResponseClient; |
| 116 | + } |
| 117 | + |
| 118 | + Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> withingsAccessTokenRequestParametersConverter() { |
| 119 | + return request -> { |
| 120 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
| 121 | + parameters.add("action", "requesttoken"); |
| 122 | + parameters.add(OAuth2ParameterNames.CLIENT_ID, request.getClientRegistration().getClientId()); |
| 123 | + parameters.add(OAuth2ParameterNames.CLIENT_SECRET, request.getClientRegistration().getClientSecret()); |
| 124 | + return parameters; |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + Converter<Map<String, Object>, OAuth2AccessTokenResponse> withingsAccessTokenResponseConverter() { |
| 129 | + return rawResponse -> { |
| 130 | + ObjectMapper mapper = new ObjectMapper(); |
| 131 | + WithingsGetAccessTokenResponse response = mapper.convertValue(rawResponse, new TypeReference<>() { |
| 132 | + }); |
| 133 | + |
| 134 | + if (response.getStatus() != 0) { |
| 135 | + throw new WithingsTechnicalException(); |
| 136 | + } |
| 137 | + |
| 138 | + WithingsGetAccessTokenResponseBody body = response.getBody(); |
| 139 | + |
| 140 | + return OAuth2AccessTokenResponse |
| 141 | + .withToken(body.getAccessToken()) |
| 142 | + .refreshToken(body.getRefreshToken()) |
| 143 | + .expiresIn(body.getExpiresIn()) |
| 144 | + .scopes(Set.of(body.getScope())) |
| 145 | + .tokenType(OAuth2AccessToken.TokenType.BEARER) |
| 146 | + .additionalParameters(Map.of("userId", body.getUserid())) |
| 147 | + .build(); |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + Converter<OAuth2RefreshTokenGrantRequest, MultiValueMap<String, String>> withingsRefreshTokenRequestParametersConverter() { |
| 152 | + return request -> { |
| 153 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
| 154 | + parameters.add("action", "requesttoken"); |
| 155 | + parameters.add(OAuth2ParameterNames.CLIENT_ID, request.getClientRegistration().getClientId()); |
| 156 | + parameters.add(OAuth2ParameterNames.CLIENT_SECRET, request.getClientRegistration().getClientSecret()); |
| 157 | + return parameters; |
| 158 | + }; |
| 159 | + } |
| 160 | + |
14 | 161 | @Data
|
15 | 162 | public static class WithingsApiConfiguration {
|
16 | 163 | private String uri;
|
|
0 commit comments