Skip to content

Commit

Permalink
[SERV-467] Enable configuration of access cookie domain (#43)
Browse files Browse the repository at this point in the history
* Enable configuration of access cookie domain
* Move env var usage information to pom.xml
  • Loading branch information
Mark A. Matney, Jr authored Jun 10, 2022
1 parent 05f08ae commit 9c61cb1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This will spin up Hauth locally, along with the Redis, PostgreSQL, and Cantaloup

| Name | Default Value | Required |
| --- | --- | --- |
| ACCESS_COOKIE_DOMAIN | XXX | No
| ACCESS_COOKIE_WINDOW_CLOSE_DELAY | XXX | No
| ACCESS_TOKEN_EXPIRES_IN | XXX | No |
| API_KEY | XXX | Yes |
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,8 @@
<run>
<env>
<ACCESS_COOKIE_WINDOW_CLOSE_DELAY>0</ACCESS_COOKIE_WINDOW_CLOSE_DELAY>
<!-- It is recommended to use the most specific domain that Hauth shares with all of the content domains that it provides authentication services for -->
<ACCESS_COOKIE_DOMAIN>example.com</ACCESS_COOKIE_DOMAIN>
<ACCESS_TOKEN_EXPIRES_IN>1800</ACCESS_TOKEN_EXPIRES_IN>
</env>
</run>
Expand All @@ -818,6 +820,7 @@
<configuration>
<environmentVariables>
<ACCESS_COOKIE_WINDOW_CLOSE_DELAY>0</ACCESS_COOKIE_WINDOW_CLOSE_DELAY>
<ACCESS_COOKIE_DOMAIN>example.com</ACCESS_COOKIE_DOMAIN>
<ACCESS_TOKEN_EXPIRES_IN>1800</ACCESS_TOKEN_EXPIRES_IN>
</environmentVariables>
</configuration>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/edu/ucla/library/iiif/auth/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public final class Config {
*/
public static final String ACCESS_COOKIE_WINDOW_CLOSE_DELAY = "ACCESS_COOKIE_WINDOW_CLOSE_DELAY";

/**
* The optional ENV property for the host domain to which the access cookie will be sent.
* <p>
* If unset, the access cookie will be sent to whatever domain Hauth itself is hosted at.
*/
public static final String ACCESS_COOKIE_DOMAIN = "ACCESS_COOKIE_DOMAIN";

/**
* The optional ENV property for the number of seconds after which an access token will cease to be valid.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public class AccessCookieHandler implements Handler<RoutingContext> {
*/
private final AccessCookieService myAccessCookieService;

/**
* See {@link Config#ACCESS_COOKIE_DOMAIN}.
*/
private final Optional<String> myCookieDomain;

/**
* See {@link Config#ACCESS_COOKIE_WINDOW_CLOSE_DELAY}.
*/
Expand All @@ -93,6 +98,7 @@ public AccessCookieHandler(final Vertx aVertx, final JsonObject aConfig) {
myCampusNetworkSubnets = new Cidr4Trie<>();
myAccessCookieService = AccessCookieService.createProxy(aVertx);
myWindowCloseDelay = Optional.ofNullable(aConfig.getInteger(Config.ACCESS_COOKIE_WINDOW_CLOSE_DELAY));
myCookieDomain = Optional.ofNullable(aConfig.getString(Config.ACCESS_COOKIE_DOMAIN));

// Register the neq helper
((Handlebars) myHtmlTemplateEngine.unwrap()).registerHelpers(ConditionalHelpers.class);
Expand Down Expand Up @@ -146,6 +152,7 @@ public void handle(final RoutingContext aContext) {
templateData.put(TemplateKeys.WINDOW_CLOSE_DELAY, delay);
}
});
myCookieDomain.ifPresent(cookie::setDomain);

response.addCookie(cookie);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package edu.ucla.library.iiif.auth.handlers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -15,6 +17,7 @@
import info.freelibrary.util.HTTP;
import info.freelibrary.util.StringUtils;

import edu.ucla.library.iiif.auth.Config;
import edu.ucla.library.iiif.auth.utils.MediaType;

import io.vertx.core.Vertx;
Expand All @@ -41,13 +44,16 @@ public void testGetCookie(final boolean aReverseProxyDeployment, final Vertx aVe
final String requestURI =
StringUtils.format(GET_COOKIE_PATH, URLEncoder.encode(TEST_ORIGIN, StandardCharsets.UTF_8));
final HttpRequest<?> getCookie = myWebClient.get(myPort, Constants.INADDR_ANY, requestURI);
final String explicitCookieDomain = myConfig.getString(Config.ACCESS_COOKIE_DOMAIN);

if (aReverseProxyDeployment) {
getCookie.putHeader(X_FORWARDED_FOR, FORWARDED_IP_ADDRESSES);
}

getCookie.send().onSuccess(response -> {
aContext.verify(() -> {
final String cookie;

assertEquals(HTTP.OK, response.statusCode());
assertEquals(MediaType.TEXT_HTML.toString(), response.headers().get(HttpHeaders.CONTENT_TYPE));
assertEquals(1, response.cookies().size());
Expand All @@ -57,6 +63,14 @@ public void testGetCookie(final boolean aReverseProxyDeployment, final Vertx aVe
Jsoup.parse(response.bodyAsString()).getElementById("client-ip-address").text());
}

cookie = response.cookies().get(0);

if (explicitCookieDomain != null) {
assertTrue(cookie.contains(StringUtils.format("Domain={}", explicitCookieDomain)));
} else {
assertFalse(cookie.contains("Domain="));
}

aContext.completeNow();
});
}).onFailure(aContext::failNow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
public final class AccessTokenHandlerIT extends AbstractAccessTokenHandlerIT {

private static final String SEMICOLON = ";";

/**
* The invalid cookie to test with.
*/
Expand Down Expand Up @@ -71,7 +73,7 @@ public void testGetTokenBrowser(final boolean aReverseProxyDeployment, final Ver

getCookie.send().compose(result -> {
final String cookieHeader = result.cookies().get(0);
final String cookieValue = cookieHeader.split(EQUALS)[1];
final String cookieValue = cookieHeader.split(SEMICOLON)[0].split(EQUALS)[1];
final String clientIpAddress;

if (aReverseProxyDeployment) {
Expand Down Expand Up @@ -146,7 +148,7 @@ public void testGetTokenNonBrowser(final boolean aReverseProxyDeployment, final

getCookie.send().compose(result -> {
final String cookieHeader = result.cookies().get(0);
final String cookieValue = cookieHeader.split(EQUALS)[1];
final String cookieValue = cookieHeader.split(SEMICOLON)[0].split(EQUALS)[1];
final String clientIpAddress;

if (aReverseProxyDeployment) {
Expand Down

0 comments on commit 9c61cb1

Please sign in to comment.