Skip to content

Commit

Permalink
remove /authenticate in favor of custom request cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed Apr 10, 2023
1 parent 36abe6d commit 310e31a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 39 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- Remove filter in favor of /withings/authenticate endpoint
- Create database for stats
- Sync stats in `/status`
- Create scheduled db backups
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.savedrequest.CookieRequestCache;

import io.github.mucsi96.kubetools.security.KubetoolsSecurityConfigurer;
import mucsi96.traininglog.core.RedirectToHomeRequestCache;
import mucsi96.traininglog.oauth.AccessTokenResponseClient;
import mucsi96.traininglog.oauth.AuthorizedClientManager;
import mucsi96.traininglog.oauth.RefreshTokenResponseClient;
Expand All @@ -30,7 +30,7 @@ SecurityFilterChain securityFilterChain(
KubetoolsSecurityConfigurer kubetoolsSecurityConfigurer,
AccessTokenResponseClient accessTokenResponseClient) throws Exception {
return kubetoolsSecurityConfigurer.configure(http)
.requestCache(configurer -> configurer.requestCache(new CookieRequestCache()))
.requestCache(configurer -> configurer.requestCache(new RedirectToHomeRequestCache()))
.oauth2Client(configurer -> configurer
.authorizationCodeGrant(customizer -> customizer
.accessTokenResponseClient(accessTokenResponseClient)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
import org.springframework.hateoas.RepresentationModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.ClientAuthorizationException;
import org.springframework.security.oauth2.client.ClientAuthorizationRequiredException;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@ControllerAdvice
public class AppControllerAdvice {
@ExceptionHandler({ClientAuthorizationRequiredException.class, ClientAuthorizationException.class})
public ResponseEntity<RepresentationModel> handleClientAuthorizationRequired() {
@ExceptionHandler(ClientAuthorizationRequiredException.class)
public ResponseEntity<RepresentationModel> handleClientAuthorizationRequired(
ClientAuthorizationRequiredException ex) {
String oauth2LoginUrl = ServletUriComponentsBuilder.fromCurrentServletMapping().path(
OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/"
+ ex.getClientRegistrationId())
.build().toString();
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED)
.body(RepresentationModel
.of(null)
.add(Link.of("/withings/authenticate").withRel("oauth2Login")));
.add(Link.of(oauth2LoginUrl).withRel("oauth2Login")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mucsi96.traininglog.core;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

public class RedirectToHomeRequestCache implements RequestCache {

@Override
public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse response) {
String redirectUrl = ServletUriComponentsBuilder
.fromCurrentServletMapping()
.build()
.toUri()
.resolve("/")
.toString();
return new RedirectToHomeSavedRequest(redirectUrl);
}

@Override
public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
}

@Override
public HttpServletRequest getMatchingRequest(HttpServletRequest request, HttpServletResponse response) {
return null;
}

@Override
public void removeRequest(HttpServletRequest request, HttpServletResponse response) {
}

}

@RequiredArgsConstructor
class RedirectToHomeSavedRequest implements SavedRequest {
private final String redirectUrl;

@Override
public String getRedirectUrl() {
return redirectUrl;
}

@Override
public List<Cookie> getCookies() {
return null;
}

@Override
public String getMethod() {
return null;
}

@Override
public List<String> getHeaderValues(String name) {
return null;
}

@Override
public Collection<String> getHeaderNames() {
return null;
}

@Override
public List<Locale> getLocales() {
return null;
}

@Override
public String[] getParameterValues(String name) {
return null;
}

@Override
public Map<String, String[]> getParameterMap() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
package mucsi96.traininglog.withings;

import java.io.IOException;

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.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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.withings.data.WeightResponse;
import mucsi96.traininglog.withings.oauth.WithingsClient;
Expand All @@ -31,29 +22,6 @@ public class WithingsController {

private final WithingsService withingsService;

@GetMapping(value = "/authenticate")
void authenticate(
HttpServletRequest servletRequest,
HttpServletResponse servletResponse,
Authentication principal,
OAuth2AuthorizedClientManager authorizedClientManager) throws IOException {
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest
.withClientRegistrationId(WithingsClient.id)
.principal(principal)
.attribute(HttpServletRequest.class.getName(), servletRequest)
.attribute(HttpServletResponse.class.getName(), servletResponse)
.build();

try {
authorizedClientManager.authorize(authorizeRequest);
} catch (ClientAuthorizationRequiredException authorizationRequiredException) {
servletResponse.sendRedirect(
OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/" + WithingsClient.id);
}

servletResponse.sendRedirect("/");
}

@GetMapping(value = "/weight", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
WeightResponse weight(
Expand Down

0 comments on commit 310e31a

Please sign in to comment.