Skip to content

Commit 657d7b8

Browse files
Merge pull request #134 from wafflestudio/develop
소셜로그인 고쳐서 prod 배포
2 parents 690cc39 + 7bf325a commit 657d7b8

6 files changed

Lines changed: 73 additions & 14 deletions

File tree

hangsha/src/main/kotlin/com/team1/hangsha/auth/service/AuthService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class AuthService(
103103
}
104104

105105
private fun getKakaoProfile(code: String): SocialUserProfile {
106-
println(">>> DEBUG: ID=$kakaoClientId, SECRET=$kakaoClientSecret")
107106
val tokenUrl = "https://kauth.kakao.com/oauth/token"
108107

109108
val headers = HttpHeaders().apply { contentType = MediaType.APPLICATION_FORM_URLENCODED }

hangsha/src/main/kotlin/com/team1/hangsha/config/WebConfig.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ class WebConfig(
2323

2424
override fun addCorsMappings(registry: CorsRegistry) {
2525
registry.addMapping("/**") // 모든 경로에 대해
26-
.allowedOrigins(
26+
.allowedOriginPatterns(
2727
"http://localhost:3000", // 로컬 프론트엔드
2828
"http://localhost:5173", // 로컬 프론트엔드 (Vite)
2929
"http://localhost:5174", // 로컬 프론트엔드 (Vite 대체 포트)
3030
"https://hangsha-dev.wafflestudio.com", // Dev 프론트엔드
31-
"https://hangsha.wafflestudio.com" // Prod 프론트엔드
31+
"https://hangsha.wafflestudio.com", // Prod 프론트엔드
32+
"https://*.app.github.dev" // GitHub Codespaces 포워딩 도메인
3233
)
3334
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")
3435
.allowedHeaders("*")

hangsha/src/main/kotlin/com/team1/hangsha/user/AuthCookieSupport.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,36 @@ class AuthCookieSupport(
1010
private val secure: Boolean,
1111
@Value("\${auth.refresh-cookie.same-site}")
1212
private val sameSite: String,
13+
@Value("\${auth.refresh-cookie.domain:}")
14+
private val domain: String,
1315
) {
14-
fun buildRefreshCookie(token: String, maxAgeSeconds: Long): ResponseCookie =
15-
ResponseCookie.from("refreshToken", token)
16+
fun buildRefreshCookie(token: String, maxAgeSeconds: Long): ResponseCookie {
17+
val builder = ResponseCookie.from("refreshToken", token)
1618
.httpOnly(true)
1719
.secure(secure)
1820
.sameSite(sameSite)
1921
.path("/api/v1/auth")
2022
.maxAge(maxAgeSeconds)
21-
.build()
2223

23-
fun clearRefreshCookie(): ResponseCookie =
24-
ResponseCookie.from("refreshToken", "")
24+
if (domain.isNotBlank()) {
25+
builder.domain(domain)
26+
}
27+
28+
return builder.build()
29+
}
30+
31+
fun clearRefreshCookie(): ResponseCookie {
32+
val builder = ResponseCookie.from("refreshToken", "")
2533
.httpOnly(true)
2634
.secure(secure)
2735
.sameSite(sameSite)
2836
.path("/api/v1/auth")
2937
.maxAge(0)
30-
.build()
38+
39+
if (domain.isNotBlank()) {
40+
builder.domain(domain)
41+
}
42+
43+
return builder.build()
44+
}
3145
}

hangsha/src/main/kotlin/com/team1/hangsha/user/controller/AuthController.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import com.team1.hangsha.common.error.DomainException
1313
import com.team1.hangsha.common.error.ErrorCode
1414
import com.team1.hangsha.user.service.UserService
1515
import com.team1.hangsha.user.AuthCookieSupport
16+
import com.team1.hangsha.user.LoggedInUser
17+
import com.team1.hangsha.user.model.User
1618
import io.swagger.v3.oas.annotations.Operation
1719
import io.swagger.v3.oas.annotations.Parameter
1820
import org.springframework.http.HttpHeaders
@@ -57,6 +59,19 @@ class AuthController(
5759
.body(RefreshResponse(accessToken = issued.accessToken))
5860
}
5961

62+
@PostMapping("/session")
63+
fun establishSession(
64+
@Parameter(hidden = true)
65+
@LoggedInUser user: User?
66+
): ResponseEntity<Unit> {
67+
val authenticatedUser = user ?: throw DomainException(ErrorCode.AUTH_UNAUTHORIZED)
68+
val issued = userService.issueAfterSocialLogin(authenticatedUser.id!!)
69+
70+
return ResponseEntity.noContent()
71+
.header(HttpHeaders.SET_COOKIE, issued.refreshCookie.toString())
72+
.build()
73+
}
74+
6075
@PostMapping("/logout")
6176
@Operation(
6277
summary = "로그아웃",

hangsha/src/main/resources/application.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ app:
121121
auth:
122122
refresh-cookie:
123123
secure: true
124-
same-site: Lax
125-
domain: ".wafflestudio.com"
124+
# OAuth 콜백 302(cross-site 리다이렉트)에서 심기는 쿠키는 SameSite=Lax면 브라우저가 저장을 거부 : None + Secure로 설정 변경
125+
same-site: None
126126
---
127127
# ==========================================
128128
# prod
@@ -137,5 +137,5 @@ app:
137137
auth:
138138
refresh-cookie:
139139
secure: true
140-
same-site: Lax
141-
domain: ".wafflestudio.com"
140+
# OAuth 콜백 302(cross-site 리다이렉트)에서 심기는 쿠키는 SameSite=Lax면 브라우저가 저장을 거부 : None + Secure로 설정 변경
141+
same-site: None

hangsha/src/test/kotlin/com/team1/hangsha/AuthIntegrationTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,36 @@ class AuthIntegrationTest : IntegrationTestBase() {
178178
}
179179
}
180180

181+
@Test
182+
fun `session endpoint issues refresh cookie with valid access token`() {
183+
val email = "test_${UUID.randomUUID()}@example.com"
184+
val password = "Abcd1234!"
185+
186+
val (accessToken, _) = postRegister(email, password)
187+
188+
val res = mockMvc.post("/api/v1/auth/session") {
189+
secure = true
190+
header(HttpHeaders.AUTHORIZATION, bearer(accessToken))
191+
}.andExpect {
192+
status { isNoContent() }
193+
header { exists(HttpHeaders.SET_COOKIE) }
194+
}.andReturn()
195+
196+
val setCookie = res.response.getHeader(HttpHeaders.SET_COOKIE)
197+
?: fail("Expected Set-Cookie for refreshToken, but it was null")
198+
199+
assertTrue(setCookie.startsWith("refreshToken="), "Set-Cookie must include refreshToken")
200+
}
201+
202+
@Test
203+
fun `session endpoint fails without access token`() {
204+
mockMvc.post("/api/v1/auth/session") {
205+
secure = true
206+
}.andExpect {
207+
status { isUnauthorized() }
208+
}
209+
}
210+
181211
@Test
182212
fun `logout clears refresh cookie`() {
183213
val email = "test_${UUID.randomUUID()}@example.com"
@@ -322,4 +352,4 @@ class AuthIntegrationTest : IntegrationTestBase() {
322352
status { isUnauthorized() }
323353
}
324354
}
325-
}
355+
}

0 commit comments

Comments
 (0)