|
3 | 3 | import java.util.List; |
4 | 4 | import java.util.Map; |
5 | 5 |
|
| 6 | +import org.springframework.beans.factory.annotation.Value; |
6 | 7 | import org.springframework.http.*; |
7 | 8 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
8 | 9 | import org.springframework.util.LinkedMultiValueMap; |
|
17 | 18 | import io.swagger.v3.oas.annotations.Hidden; |
18 | 19 | import io.swagger.v3.oas.annotations.Operation; |
19 | 20 | import io.swagger.v3.oas.annotations.tags.Tag; |
20 | | -import umc.codeplay.apiPayLoad.ApiResponse; |
| 21 | +import org.jetbrains.annotations.NotNull; |
21 | 22 | import umc.codeplay.apiPayLoad.code.status.ErrorStatus; |
22 | 23 | import umc.codeplay.apiPayLoad.exception.handler.GeneralHandler; |
23 | 24 | import umc.codeplay.config.properties.BaseOAuthProperties; |
24 | 25 | import umc.codeplay.config.properties.GoogleOAuthProperties; |
25 | 26 | import umc.codeplay.config.properties.KakaoOAuthProperties; |
26 | 27 | import umc.codeplay.domain.Member; |
27 | 28 | import umc.codeplay.domain.enums.SocialStatus; |
28 | | -import umc.codeplay.dto.MemberResponseDTO; |
29 | 29 | import umc.codeplay.jwt.JwtUtil; |
30 | 30 | import umc.codeplay.service.MemberService; |
31 | 31 |
|
|
36 | 36 | @Tag(name = "oauth-controller", description = "외부 소셜 로그인 서비스 연동 API, JWT 토큰 헤더 포함을 필요로 하지 않습니다.") |
37 | 37 | public class OAuthController { |
38 | 38 |
|
| 39 | + @Value("${frontend.url}") |
| 40 | + private static String targetOrigin; |
| 41 | + |
39 | 42 | private final JwtUtil jwtUtil; |
40 | 43 | private final RestTemplate restTemplate = new RestTemplate(); |
41 | 44 | private final GoogleOAuthProperties googleOAuthProperties; |
@@ -65,7 +68,7 @@ public RedirectView redirectToOAuth(@PathVariable("provider") String provider) { |
65 | 68 |
|
66 | 69 | @Hidden |
67 | 70 | @GetMapping("/callback/{provider}") |
68 | | - public ApiResponse<MemberResponseDTO.LoginResultDTO> OAuthCallback( |
| 71 | + public ResponseEntity<String> OAuthCallback( |
69 | 72 | @RequestParam("code") String code, @PathVariable("provider") String provider) { |
70 | 73 | BaseOAuthProperties properties = |
71 | 74 | switch (provider) { |
@@ -110,13 +113,41 @@ public ApiResponse<MemberResponseDTO.LoginResultDTO> OAuthCallback( |
110 | 113 | String serviceAccessToken = jwtUtil.generateToken(email, authorities); |
111 | 114 | String serviceRefreshToken = jwtUtil.generateRefreshToken(email, authorities); |
112 | 115 |
|
| 116 | + String html = getString(serviceAccessToken, serviceRefreshToken, email); |
| 117 | + |
| 118 | + return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(html); |
| 119 | + |
113 | 120 | // (6) 최종적으로 JWT(액세스/리프레시)를 프론트에 응답 |
114 | | - return ApiResponse.onSuccess( |
115 | | - MemberResponseDTO.LoginResultDTO.builder() |
116 | | - .email(email) |
117 | | - .token(serviceAccessToken) |
118 | | - .refreshToken(serviceRefreshToken) |
119 | | - .build()); |
| 121 | + // return ApiResponse.onSuccess( |
| 122 | + // MemberResponseDTO.LoginResultDTO.builder() |
| 123 | + // .email(email) |
| 124 | + // .token(serviceAccessToken) |
| 125 | + // .refreshToken(serviceRefreshToken) |
| 126 | + // .build()); |
| 127 | + } |
| 128 | + |
| 129 | + private static @NotNull String getString( |
| 130 | + String serviceAccessToken, String serviceRefreshToken, String email) { |
| 131 | + String jsonData = |
| 132 | + String.format( |
| 133 | + "{ \"accessToken\": \"%s\", \"refreshToken\": \"%s\", \"email\": \"%s\" }", |
| 134 | + serviceAccessToken, serviceRefreshToken, email); |
| 135 | + |
| 136 | + return """ |
| 137 | + <!DOCTYPE html> |
| 138 | + <html> |
| 139 | + <body> |
| 140 | + <script> |
| 141 | + (function() { |
| 142 | + var data = %s; |
| 143 | + window.opener.postMessage(data, "%s"); |
| 144 | + window.close(); |
| 145 | + })(); |
| 146 | + </script> |
| 147 | + </body> |
| 148 | + </html> |
| 149 | + """ |
| 150 | + .formatted(jsonData, targetOrigin); |
120 | 151 | } |
121 | 152 |
|
122 | 153 | private Map<String, Object> requestOAuthToken(String code, BaseOAuthProperties properties) { |
|
0 commit comments