1
0
mirror of synced 2026-08-05 09:47:05 +00:00

Polish CookieServerCsrfTokenRepository

- Only do work if subscribed to
- use test naming conventions
- Refactor tests to avoid extracting
  - Uses String for member names which are not type safe
  - Uses long argument list which makes assertions difficult to read

Issue: gh-5083
This commit is contained in:
Rob Winch
2018-05-04 16:53:25 -05:00
parent 37b1136c0c
commit 3ba15a16bf
2 changed files with 171 additions and 201 deletions
@@ -21,8 +21,6 @@ import java.util.UUID;
import org.springframework.http.HttpCookie;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -70,30 +68,30 @@ public final class CookieServerCsrfTokenRepository implements ServerCsrfTokenRep
@Override
public Mono<Void> saveToken(ServerWebExchange exchange, CsrfToken token) {
Optional<String> tokenValue = Optional.ofNullable(token).map(CsrfToken::getToken);
return Mono.fromRunnable(() -> {
Optional<String> tokenValue = Optional.ofNullable(token).map(CsrfToken::getToken);
ResponseCookie cookie = ResponseCookie.from(this.cookieName, tokenValue.orElse(""))
.domain(this.cookieDomain)
.httpOnly(this.cookieHttpOnly)
.maxAge(tokenValue.map(val -> -1).orElse(0))
.path(Optional.ofNullable(this.cookiePath).orElseGet(() -> getRequestContext(exchange.getRequest())))
.secure(Optional.ofNullable(exchange.getRequest().getSslInfo()).map(sslInfo -> true).orElse(false))
.build();
ResponseCookie cookie = ResponseCookie.from(this.cookieName, tokenValue.orElse(""))
.domain(this.cookieDomain)
.httpOnly(this.cookieHttpOnly)
.maxAge(tokenValue.map(val -> -1).orElse(0))
.path(Optional.ofNullable(this.cookiePath).orElseGet(() -> getRequestContext(exchange.getRequest())))
.secure(Optional.ofNullable(exchange.getRequest().getSslInfo()).map(sslInfo -> true).orElse(false))
.build();
exchange.getResponse().addCookie(cookie);
return Mono.empty();
exchange.getResponse().addCookie(cookie);
});
}
@Override
public Mono<CsrfToken> loadToken(ServerWebExchange exchange) {
Optional<CsrfToken> token = Optional.ofNullable(exchange.getRequest())
.map(ServerHttpRequest::getCookies)
.map(cookiesMap -> cookiesMap.getFirst(this.cookieName))
.map(HttpCookie::getValue)
.map(this::createCsrfToken);
return Mono.justOrEmpty(token);
return Mono.fromCallable(() -> {
HttpCookie csrfCookie = exchange.getRequest().getCookies().getFirst(this.cookieName);
if (csrfCookie == null) {
return null;
}
return createCsrfToken(csrfCookie.getValue());
});
}
/**