Skip to content

Commit ae76daf

Browse files
authored
Merge pull request #48565 from ikorennoy/korennoy/form_auth_cookie_domain
Support Form Auth cookie domain property
2 parents e5d3607 + 1b8d193 commit ae76daf

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

extensions/security-webauthn/runtime/src/main/java/io/quarkus/security/webauthn/WebAuthnRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public WebAuthnAuthenticationMechanism get() {
7979
config.sessionTimeout().toMillis(),
8080
config.newCookieInterval().toMillis(), false, config.cookieSameSite().name(),
8181
config.cookiePath().orElse(null),
82-
config.cookieMaxAge().map(Duration::toSeconds).orElse(-1L));
82+
config.cookieMaxAge().map(Duration::toSeconds).orElse(-1L), null);
8383
String loginPage = config.loginPage().startsWith("/") ? config.loginPage() : "/" + config.loginPage();
8484
return new WebAuthnAuthenticationMechanism(loginManager, loginPage);
8585
}

extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/FormAuthRuntimeConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ enum CookieSameSite {
106106
@WithDefault("/")
107107
Optional<String> cookiePath();
108108

109+
/**
110+
* Cookie domain parameter value which, if set, will be used for the session and location cookies.
111+
*/
112+
Optional<String> cookieDomain();
113+
109114
/**
110115
* Set the HttpOnly attribute to prevent access to the cookie via JavaScript.
111116
*/

extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/FormAuthenticationMechanism.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class FormAuthenticationMechanism implements HttpAuthenticationMechanism
6565
private final boolean redirectToLoginPage;
6666
private final CookieSameSite cookieSameSite;
6767
private final String cookiePath;
68+
private final String cookieDomain;
6869
private final boolean isFormAuthEventObserver;
6970
private final PersistentLoginManager loginManager;
7071
private final Event<FormAuthenticationEvent> formAuthEvent;
@@ -97,7 +98,8 @@ public class FormAuthenticationMechanism implements HttpAuthenticationMechanism
9798
FormAuthRuntimeConfig runtimeForm = httpConfig.auth().form();
9899
this.loginManager = new PersistentLoginManager(key, runtimeForm.cookieName(), runtimeForm.timeout().toMillis(),
99100
runtimeForm.newCookieInterval().toMillis(), runtimeForm.httpOnlyCookie(), runtimeForm.cookieSameSite().name(),
100-
runtimeForm.cookiePath().orElse(null), runtimeForm.cookieMaxAge().map(Duration::toSeconds).orElse(-1L));
101+
runtimeForm.cookiePath().orElse(null), runtimeForm.cookieMaxAge().map(Duration::toSeconds).orElse(-1L),
102+
runtimeForm.cookieDomain().orElse(null));
101103
this.loginPage = startWithSlash(runtimeForm.loginPage().orElse(null));
102104
this.errorPage = startWithSlash(runtimeForm.errorPage().orElse(null));
103105
this.landingPage = startWithSlash(runtimeForm.landingPage().orElse(null));
@@ -106,6 +108,7 @@ public class FormAuthenticationMechanism implements HttpAuthenticationMechanism
106108
this.passwordParameter = runtimeForm.passwordParameter();
107109
this.locationCookie = runtimeForm.locationCookie();
108110
this.cookiePath = runtimeForm.cookiePath().orElse(null);
111+
this.cookieDomain = runtimeForm.cookieDomain().orElse(null);
109112
boolean redirectAfterLogin = runtimeForm.redirectAfterLogin();
110113
this.redirectToLandingPage = landingPage != null && redirectAfterLogin;
111114
this.redirectToLoginPage = loginPage != null;
@@ -119,7 +122,7 @@ public class FormAuthenticationMechanism implements HttpAuthenticationMechanism
119122
public FormAuthenticationMechanism(String loginPage, String postLocation,
120123
String usernameParameter, String passwordParameter, String errorPage, String landingPage,
121124
boolean redirectAfterLogin, String locationCookie, String cookieSameSite, String cookiePath,
122-
PersistentLoginManager loginManager) {
125+
String cookieDomain, PersistentLoginManager loginManager) {
123126
this.loginPage = loginPage;
124127
this.postLocation = postLocation;
125128
this.usernameParameter = usernameParameter;
@@ -132,6 +135,7 @@ public FormAuthenticationMechanism(String loginPage, String postLocation,
132135
this.redirectToErrorPage = errorPage != null;
133136
this.cookieSameSite = CookieSameSite.valueOf(cookieSameSite);
134137
this.cookiePath = cookiePath;
138+
this.cookieDomain = cookieDomain;
135139
this.loginManager = loginManager;
136140
this.isFormAuthEventObserver = false;
137141
this.formAuthEvent = null;
@@ -239,8 +243,12 @@ protected void verifyRedirectBackLocation(String requestURIString, String redire
239243
}
240244

241245
protected void storeInitialLocation(final RoutingContext exchange) {
242-
exchange.response().addCookie(Cookie.cookie(locationCookie, exchange.request().absoluteURI())
243-
.setPath(cookiePath).setSameSite(cookieSameSite).setSecure(exchange.request().isSSL()));
246+
Cookie cookie = Cookie.cookie(locationCookie, exchange.request().absoluteURI())
247+
.setPath(cookiePath).setSameSite(cookieSameSite).setSecure(exchange.request().isSSL());
248+
if (cookieDomain != null) {
249+
cookie.setDomain(cookieDomain);
250+
}
251+
exchange.response().addCookie(cookie);
244252
}
245253

246254
protected void servePage(final RoutingContext exchange, final String location) {

extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/PersistentLoginManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ public class PersistentLoginManager {
4040
private final CookieSameSite cookieSameSite;
4141
private final String cookiePath;
4242
private final long maxAgeSeconds;
43+
private final String cookieDomain;
4344

4445
public PersistentLoginManager(String encryptionKey, String cookieName, long timeoutMillis, long newCookieIntervalMillis,
45-
boolean httpOnlyCookie, String cookieSameSite, String cookiePath, long maxAgeSeconds) {
46+
boolean httpOnlyCookie, String cookieSameSite, String cookiePath, long maxAgeSeconds, String cookieDomain) {
4647
this.cookieName = cookieName;
4748
this.newCookieIntervalMillis = newCookieIntervalMillis;
4849
this.timeoutMillis = timeoutMillis;
4950
this.httpOnlyCookie = httpOnlyCookie;
5051
this.cookieSameSite = CookieSameSite.valueOf(cookieSameSite);
5152
this.cookiePath = cookiePath;
5253
this.maxAgeSeconds = maxAgeSeconds;
54+
this.cookieDomain = cookieDomain;
5355
try {
5456
if (encryptionKey == null) {
5557
this.secretKey = KeyGenerator.getInstance("AES").generateKey();
@@ -150,6 +152,9 @@ public void save(String value, RoutingContext context, String cookieName, Restor
150152
if (maxAgeSeconds >= 0) {
151153
cookie.setMaxAge(maxAgeSeconds);
152154
}
155+
if (cookieDomain != null) {
156+
cookie.setDomain(cookieDomain);
157+
}
153158
context.addCookie(cookie);
154159
} catch (Exception e) {
155160
throw new RuntimeException(e);

0 commit comments

Comments
 (0)