|
| 1 | +package org.clokey.global.security; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServletRequest; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 6 | +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; |
| 7 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; |
| 8 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
| 9 | + |
| 10 | +@RequiredArgsConstructor |
| 11 | +public class AppleAwareOAuth2AuthorizationRequestResolver |
| 12 | + implements OAuth2AuthorizationRequestResolver { |
| 13 | + |
| 14 | + private final DefaultOAuth2AuthorizationRequestResolver delegate; |
| 15 | + |
| 16 | + public AppleAwareOAuth2AuthorizationRequestResolver( |
| 17 | + ClientRegistrationRepository clientRegistrationRepository, |
| 18 | + String authorizationBaseUri) { |
| 19 | + this.delegate = |
| 20 | + new DefaultOAuth2AuthorizationRequestResolver( |
| 21 | + clientRegistrationRepository, authorizationBaseUri); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public OAuth2AuthorizationRequest resolve(HttpServletRequest request) { |
| 26 | + OAuth2AuthorizationRequest authorizationRequest = delegate.resolve(request); |
| 27 | + return customizeIfApple(request, authorizationRequest); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public OAuth2AuthorizationRequest resolve( |
| 32 | + HttpServletRequest request, String clientRegistrationId) { |
| 33 | + OAuth2AuthorizationRequest authorizationRequest = |
| 34 | + delegate.resolve(request, clientRegistrationId); |
| 35 | + if (authorizationRequest == null) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + return "apple".equals(clientRegistrationId) |
| 39 | + ? OAuth2AuthorizationRequest.from(authorizationRequest) |
| 40 | + .additionalParameters(params -> params.put("response_mode", "form_post")) |
| 41 | + .build() |
| 42 | + : authorizationRequest; |
| 43 | + } |
| 44 | + |
| 45 | + public void setAuthorizationRequestCustomizer( |
| 46 | + java.util.function.Consumer<OAuth2AuthorizationRequest.Builder> |
| 47 | + authorizationRequestCustomizer) { |
| 48 | + delegate.setAuthorizationRequestCustomizer(authorizationRequestCustomizer); |
| 49 | + } |
| 50 | + |
| 51 | + private OAuth2AuthorizationRequest customizeIfApple( |
| 52 | + HttpServletRequest request, OAuth2AuthorizationRequest authorizationRequest) { |
| 53 | + if (authorizationRequest == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + String requestUri = request.getRequestURI(); |
| 57 | + if (requestUri != null && requestUri.endsWith("/apple")) { |
| 58 | + return OAuth2AuthorizationRequest.from(authorizationRequest) |
| 59 | + .additionalParameters(params -> params.put("response_mode", "form_post")) |
| 60 | + .build(); |
| 61 | + } |
| 62 | + return authorizationRequest; |
| 63 | + } |
| 64 | +} |
0 commit comments