Skip to content

Commit

Permalink
simplify withings oauth config part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Jul 15, 2023
1 parent 2cbbe12 commit bbf5eb7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ResponseEntity<RepresentationModel<?>> handleClientAuthorizationRequired(
WithingsAuthorizationException ex) {

Link oauth2LogLink = WebMvcLinkBuilder
.linkTo(WebMvcLinkBuilder.methodOn(WithingsController.class).authorize(null))
.linkTo(WebMvcLinkBuilder.methodOn(WithingsController.class).authorize(null, null, null))
.withRel("oauth2Login");

RepresentationModel<?> model = RepresentationModel.of(null).add(oauth2LogLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.security.RolesAllowed;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -35,15 +33,9 @@ public void sync(
Authentication principal,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest
.withClientRegistrationId(WithingsClient.id)
.principal(principal)
.attribute(HttpServletRequest.class.getName(), servletRequest)
.attribute(HttpServletResponse.class.getName(), servletResponse)
.build();

try {
OAuth2AuthorizedClient authorizedClient = withingsAuthorizedClientManager.authorize(authorizeRequest);
OAuth2AuthorizedClient authorizedClient = getAuthorizedClient(principal, servletRequest, servletResponse);
if (!weightService.getTodayWeight().isPresent()) {
withingsService.getTodayWeight(authorizedClient).ifPresent(weightService::saveWeight);
}
Expand All @@ -55,7 +47,22 @@ public void sync(

@GetMapping("/authorize")
public RedirectView authorize(
@Parameter(hidden = true) @RegisteredOAuth2AuthorizedClient(WithingsClient.id) OAuth2AuthorizedClient withingsAuthorizedClient) {
Authentication principal,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
getAuthorizedClient(principal, servletRequest, servletResponse);
return new RedirectView("/");
}

private OAuth2AuthorizedClient getAuthorizedClient(Authentication principal, HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest
.withClientRegistrationId(WithingsClient.id)
.principal(principal)
.attribute(HttpServletRequest.class.getName(), servletRequest)
.attribute(HttpServletResponse.class.getName(), servletResponse)
.build();
OAuth2AuthorizedClient authorizedClient = withingsAuthorizedClientManager.authorize(authorizeRequest);
return authorizedClient;
}
}

0 comments on commit bbf5eb7

Please sign in to comment.