fix session cookies and catalog pricing visibility

This commit is contained in:
wlx committed 2026-09-25 23:09:59 +08:00
1 parent 05ed1fc9a4
commit 8a65109c80
9 files changed
+71 -25

No files matched your search

@@ -74,7 +74,9 @@ public class AuthController {
HttpServletResponse response
) {
AuthService.AuthResult result = authService.login(input.account(), input.password(), meta(request));
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), Duration.ofHours(sessionTtlHours)).toString());
// Keep the browser session cookie ephemeral. The server-side session still
// has its normal TTL, but a closed browser must require a fresh login.
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
return Map.of("user", result.user());
}
@@ -88,7 +90,7 @@ public class AuthController {
AuthService.AuthResult result = authService.register(
input.invitationCode(), input.account(), input.password(), input.displayName(), meta(request)
);
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), Duration.ofHours(sessionTtlHours)).toString());
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("user", result.user()));
}
@@ -155,13 +157,13 @@ public class AuthController {
}
private ResponseCookie sessionCookie(String value, Duration maxAge) {
return ResponseCookie.from(SESSION_COOKIE, value)
var builder = ResponseCookie.from(SESSION_COOKIE, value)
.httpOnly(true)
.secure(secureCookie)
.sameSite("Lax")
.path("/")
.maxAge(maxAge)
.build();
.path("/");
if (maxAge != null) builder.maxAge(maxAge);
return builder.build();
}
public static String sessionToken(HttpServletRequest request) {
@@ -473,7 +473,12 @@ public class CatalogService {
model.put("inputTypes", readList(resultSet.getString("input_types")));
model.put("outputTypes", readList(resultSet.getString("output_types")));
model.put("capabilities", readMap(resultSet.getString("capabilities")));
putIfNotNull(model, "pricing", readOptionalMap(resultSet.getString("pricing")));
Map<String, Object> pricing = readOptionalMap(resultSet.getString("pricing"));
putIfNotNull(model, "pricing", pricing);
model.put("pricingConfigured", pricing != null
&& "CNY / 1M tokens".equals(String.valueOf(pricing.get("unit")))
&& pricing.get("input") != null
&& pricing.get("output") != null);
putIfNotNull(model, "priority", resultSet.getObject("priority"));
putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency"));
model.put("status", resultSet.getString("status"));
@@ -75,6 +75,7 @@ class AuthPostgresTest {
var registered = http.perform(post("/api/auth/register").contentType("application/json").content(json.writeValueAsBytes(registration)))
.andExpect(status().isCreated()).andExpect(jsonPath("$.user.role").value("user"))
.andExpect(cookie().httpOnly("moyi_session", true)).andExpect(cookie().secure("moyi_session", true))
.andExpect(cookie().maxAge("moyi_session", -1))
.andReturn().getResponse();
UUID memberId = UUID.fromString(json.readTree(registered.getContentAsString()).get("user").get("id").asText());
Cookie memberCookie = registered.getCookie("moyi_session");
@@ -106,7 +107,7 @@ class AuthPostgresTest {
private Cookie login(MockMvc http, String account, String role) throws Exception {
return http.perform(post("/api/auth/login").contentType("application/json")
.content("{\"account\":\"" + account + "\",\"password\":\"test-password\"}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.user.role").value(role))
.andExpect(status().isOk()).andExpect(jsonPath("$.user.role").value(role)).andExpect(cookie().maxAge("moyi_session", -1))
.andReturn().getResponse().getCookie("moyi_session");
}
}