-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom withings authorization endpoint
- Loading branch information
Showing
8 changed files
with
69 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
- Adjust the UI | ||
- Create separate E2E test project which locally tests agains running angular + Spring API using Selnium. On CI it uses test containers of both images. Adding authontication headers with own server. | ||
- Create dedicated AuthorizedClientManager for withings and get rid of AccessTokenResponseClient, RefreshTokenResponseClient | ||
- Try using WebTestClient https://docs.spring.io/spring-framework/reference/testing/webtestclient.html#webtestclient-json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 0 additions & 88 deletions
88
server/src/main/java/mucsi96/traininglog/core/RedirectToHomeRequestCache.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
server/src/main/java/mucsi96/traininglog/withings/WithingsAuthorizationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package mucsi96.traininglog.withings; | ||
|
||
public class WithingsAuthorizationException extends RuntimeException { | ||
} |
40 changes: 34 additions & 6 deletions
40
server/src/main/java/mucsi96/traininglog/withings/WithingsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,61 @@ | ||
package mucsi96.traininglog.withings; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.client.ClientAuthorizationRequiredException; | ||
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; | ||
import lombok.RequiredArgsConstructor; | ||
import mucsi96.traininglog.weight.WeightService; | ||
import mucsi96.traininglog.withings.oauth.WithingsClient; | ||
|
||
@RestController | ||
@RequestMapping(value = "/withings", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@RequestMapping("/withings") | ||
@RequiredArgsConstructor | ||
@RolesAllowed("user") | ||
public class WithingsController { | ||
|
||
private final WithingsService withingsService; | ||
private final WeightService weightService; | ||
private final OAuth2AuthorizedClientManager authorizedClientManager; | ||
|
||
@PostMapping("/sync") | ||
void sync( | ||
@Parameter(hidden = true) @RegisteredOAuth2AuthorizedClient(WithingsClient.id) OAuth2AuthorizedClient withingsAuthorizedClient) { | ||
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(); | ||
|
||
if (!weightService.getTodayWeight().isPresent()) { | ||
withingsService.getTodayWeight(withingsAuthorizedClient).ifPresent(weightService::saveWeight); | ||
try { | ||
OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest); | ||
if (!weightService.getTodayWeight().isPresent()) { | ||
withingsService.getTodayWeight(authorizedClient).ifPresent(weightService::saveWeight); | ||
} | ||
} catch (ClientAuthorizationRequiredException ex) { | ||
throw new WithingsAuthorizationException(); | ||
} | ||
|
||
} | ||
|
||
@GetMapping("/authorize") | ||
public RedirectView authorize( | ||
@Parameter(hidden = true) @RegisteredOAuth2AuthorizedClient(WithingsClient.id) OAuth2AuthorizedClient withingsAuthorizedClient) { | ||
return new RedirectView("/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters