Skip to content

Commit 7adb57d

Browse files
author
Mark A. Matney, Jr
authored
Allow requests from all client origins to obtain access cookie (#51)
1 parent 273b820 commit 7adb57d

File tree

14 files changed

+27
-242
lines changed

14 files changed

+27
-242
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<freelib.utils.version>3.3.0</freelib.utils.version>
4949
<cidr.ip.version>1.0.1</cidr.ip.version>
5050
<commons.codec.version>1.15</commons.codec.version>
51-
<vertx.version>4.3.8</vertx.version>
51+
<vertx.version>4.4.2</vertx.version>
5252

5353
<!-- Build plugin versions -->
5454
<clean.plugin.version>3.1.0</clean.plugin.version>

src/main/java/edu/ucla/library/iiif/auth/CookieJsonKeys.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
* <pre>
2020
* {
2121
* "clientIpAddress": "127.0.0.1",
22-
* "campusNetwork": false,
23-
* "degradedAllowed": true
22+
* "campusNetwork": false
2423
* }
2524
* </pre>
2625
*/
@@ -51,11 +50,6 @@ public final class CookieJsonKeys {
5150
*/
5251
public static final String CAMPUS_NETWORK = "campusNetwork";
5352

54-
/**
55-
* The JSON key for whether degraded content is available at the origin for which the cookie applies.
56-
*/
57-
public static final String DEGRADED_ALLOWED = "degradedAllowed";
58-
5953
/**
6054
* Private constructor for utility class.
6155
*/

src/main/java/edu/ucla/library/iiif/auth/TemplateKeys.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ public final class TemplateKeys {
2121
*/
2222
public static final String CLIENT_IP_ADDRESS = "clientIpAddress";
2323

24-
/**
25-
* The degraded allowed key.
26-
*/
27-
public static final String DEGRADED_ALLOWED = "degradedAllowed";
28-
2924
/**
3025
* The window close delay key.
3126
*/

src/main/java/edu/ucla/library/iiif/auth/handlers/AccessCookieHandler.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
import edu.ucla.library.iiif.auth.Param;
2121
import edu.ucla.library.iiif.auth.TemplateKeys;
2222
import edu.ucla.library.iiif.auth.services.AccessCookieService;
23-
import edu.ucla.library.iiif.auth.services.DatabaseService;
2423
import edu.ucla.library.iiif.auth.utils.MediaType;
2524

2625
import info.freelibrary.util.HTTP;
2726
import info.freelibrary.util.Logger;
2827
import info.freelibrary.util.LoggerFactory;
2928

30-
import io.vertx.core.Future;
3129
import io.vertx.core.Handler;
3230
import io.vertx.core.Vertx;
3331
import io.vertx.core.http.Cookie;
@@ -56,11 +54,6 @@ public class AccessCookieHandler implements Handler<RoutingContext> {
5654
*/
5755
private final JsonObject myConfig;
5856

59-
/**
60-
* The service proxy for accessing the database.
61-
*/
62-
private final DatabaseService myDatabaseServiceProxy;
63-
6457
/**
6558
* The template engine for rendering the response.
6659
*/
@@ -94,7 +87,6 @@ public class AccessCookieHandler implements Handler<RoutingContext> {
9487
*/
9588
public AccessCookieHandler(final Vertx aVertx, final JsonObject aConfig) {
9689
myConfig = aConfig;
97-
myDatabaseServiceProxy = DatabaseService.createProxy(aVertx);
9890
myHtmlTemplateEngine = HandlebarsTemplateEngine.create(aVertx);
9991
myCampusNetworkSubnets = new Cidr4Trie<>();
10092
myAccessCookieService = AccessCookieService.createProxy(aVertx);
@@ -136,32 +128,26 @@ public void handle(final RoutingContext aContext) {
136128

137129
isOnCampusNetwork = isOnNetwork(clientIpAddress, myCampusNetworkSubnets);
138130

139-
myDatabaseServiceProxy.getDegradedAllowed(origin.toString()).compose(isDegradedAllowed -> {
140-
final Future<String> cookieGeneration = myAccessCookieService.generateCookie(clientIpAddress.getAddress(),
141-
isOnCampusNetwork, isDegradedAllowed);
142-
143-
return cookieGeneration.compose(cookieValue -> {
144-
final Cookie cookie =
145-
Cookie.cookie(CookieNames.HAUTH, cookieValue).setSameSite(CookieSameSite.NONE).setSecure(true);
131+
myAccessCookieService.generateCookie(clientIpAddress.getAddress(), isOnCampusNetwork).compose(cookieValue -> {
132+
final Cookie cookie =
133+
Cookie.cookie(CookieNames.HAUTH, cookieValue).setSameSite(CookieSameSite.NONE).setSecure(true);
146134

147-
// Along with the origin, pass all the cookie data to the HTML template
148-
final JsonObject templateData = new JsonObject().put(TemplateKeys.ORIGIN, origin)
149-
.put(TemplateKeys.VERSION, myConfig.getString(Config.HAUTH_VERSION))
150-
.put(TemplateKeys.CLIENT_IP_ADDRESS, clientIpAddress)
151-
.put(TemplateKeys.CAMPUS_NETWORK, isOnCampusNetwork)
152-
.put(TemplateKeys.DEGRADED_ALLOWED, isDegradedAllowed);
135+
// Along with the origin, pass all the cookie data to the HTML template
136+
final JsonObject templateData = new JsonObject().put(TemplateKeys.ORIGIN, origin)
137+
.put(TemplateKeys.VERSION, myConfig.getString(Config.HAUTH_VERSION))
138+
.put(TemplateKeys.CLIENT_IP_ADDRESS, clientIpAddress)
139+
.put(TemplateKeys.CAMPUS_NETWORK, isOnCampusNetwork);
153140

154-
myWindowCloseDelay.ifPresent(delay -> {
155-
if (delay >= 0) {
156-
templateData.put(TemplateKeys.WINDOW_CLOSE_DELAY, delay);
157-
}
158-
});
159-
myCookieDomain.ifPresent(cookie::setDomain);
141+
myWindowCloseDelay.ifPresent(delay -> {
142+
if (delay >= 0) {
143+
templateData.put(TemplateKeys.WINDOW_CLOSE_DELAY, delay);
144+
}
145+
});
146+
myCookieDomain.ifPresent(cookie::setDomain);
160147

161-
response.addCookie(cookie);
148+
response.addCookie(cookie);
162149

163-
return myHtmlTemplateEngine.render(templateData, "templates/cookie.hbs");
164-
});
150+
return myHtmlTemplateEngine.render(templateData, "templates/cookie.hbs");
165151
}).onSuccess(renderedHtmlTemplate -> {
166152
response.setStatusCode(HTTP.OK).end(renderedHtmlTemplate);
167153
}).onFailure(error -> {

src/main/java/edu/ucla/library/iiif/auth/services/AccessCookieService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ static AccessCookieService createProxy(final Vertx aVertx) {
6060
*
6161
* @param aClientIpAddress The IP address of the client
6262
* @param aIsOnCampusNetwork If the client is on a campus network subnet
63-
* @param aIsDegradedAllowed If the origin allows degraded access to content
6463
* @return A Future that resolves to a value that can be used to create a cookie with
6564
* {@link Cookie#cookie(String, String)}
6665
*/
67-
Future<String> generateCookie(String aClientIpAddress, boolean aIsOnCampusNetwork, boolean aIsDegradedAllowed);
66+
Future<String> generateCookie(String aClientIpAddress, boolean aIsOnCampusNetwork);
6867

6968
/**
7069
* Decrypts an access cookie value.

src/main/java/edu/ucla/library/iiif/auth/services/AccessCookieServiceImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,9 @@ public Future<Void> close() {
166166
}
167167

168168
@Override
169-
public Future<String> generateCookie(final String aClientIpAddress, final boolean aIsOnCampusNetwork,
170-
final boolean aIsDegradedAllowed) {
169+
public Future<String> generateCookie(final String aClientIpAddress, final boolean aIsOnCampusNetwork) {
171170
final JsonObject cookieData = new JsonObject().put(CookieJsonKeys.CLIENT_IP_ADDRESS, aClientIpAddress)
172-
.put(CookieJsonKeys.CAMPUS_NETWORK, aIsOnCampusNetwork)
173-
.put(CookieJsonKeys.DEGRADED_ALLOWED, aIsDegradedAllowed);
171+
.put(CookieJsonKeys.CAMPUS_NETWORK, aIsOnCampusNetwork);
174172
final byte[] encryptedCookieData;
175173
final JsonObject unencodedCookie;
176174
final String cookie;

src/main/java/edu/ucla/library/iiif/auth/services/DatabaseService.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,4 @@ static DatabaseService createProxy(final Vertx aVertx) {
7676
* @return A Future that resolves once the items have been set
7777
*/
7878
Future<Void> setItems(JsonArray aItems);
79-
80-
/**
81-
* Gets the "degraded allowed" for content hosted at the given origin.
82-
*
83-
* @param aOrigin The origin
84-
* @return A Future that resolves to the degraded allowed once it's been fetched
85-
*/
86-
Future<Boolean> getDegradedAllowed(String aOrigin);
87-
88-
/**
89-
* Sets the given "degraded allowed" for content hosted at the given origin.
90-
*
91-
* @param aOrigin The origin
92-
* @param aDegradedAllowed The degraded allowed to set for the origin
93-
* @return A Future that resolves once the degraded allowed has been set
94-
*/
95-
Future<Void> setDegradedAllowed(String aOrigin, boolean aDegradedAllowed);
9679
}

src/main/java/edu/ucla/library/iiif/auth/services/DatabaseServiceImpl.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ public class DatabaseServiceImpl implements DatabaseService {
6161
private static final String UPSERT_ACCESS_MODE = String.join(SPACE, "INSERT INTO items VALUES ($1, $2)",
6262
"ON CONFLICT (uid) DO", "UPDATE SET access_mode = EXCLUDED.access_mode");
6363

64-
/**
65-
* The PreparedQuery template for selecting an origin's "degraded allowed".
66-
*/
67-
private static final String SELECT_DEGRADED_ALLOWED = "SELECT degraded_allowed FROM origins WHERE url = $1";
68-
69-
/**
70-
* The PreparedQuery template for upserting an origin's "degraded allowed".
71-
*/
72-
private static final String UPSERT_DEGRADED_ALLOWED = String.join(SPACE, "INSERT INTO origins VALUES ($1, $2)",
73-
"ON CONFLICT (url) DO", "UPDATE SET degraded_allowed = EXCLUDED.degraded_allowed");
74-
7564
/**
7665
* The database's default hostname.
7766
*/
@@ -163,29 +152,6 @@ public Future<Void> setItems(final JsonArray aItems) {
163152
}).compose(result -> Future.succeededFuture());
164153
}
165154

166-
@Override
167-
public Future<Boolean> getDegradedAllowed(final String aOrigin) {
168-
return myDbConnectionPool.withConnection(connection -> {
169-
return connection.preparedQuery(SELECT_DEGRADED_ALLOWED).execute(Tuple.of(aOrigin));
170-
}).recover(error -> {
171-
return Future.failedFuture(new ServiceException(INTERNAL_ERROR, error.getMessage()));
172-
}).compose(select -> {
173-
if (hasSingleRow(select)) {
174-
return Future.succeededFuture(select.iterator().next().getBoolean("degraded_allowed"));
175-
}
176-
return Future.failedFuture(new ServiceException(NOT_FOUND_ERROR, aOrigin));
177-
});
178-
}
179-
180-
@Override
181-
public Future<Void> setDegradedAllowed(final String aOrigin, final boolean aDegradedAllowed) {
182-
return myDbConnectionPool.withConnection(connection -> {
183-
return connection.preparedQuery(UPSERT_DEGRADED_ALLOWED).execute(Tuple.of(aOrigin, aDegradedAllowed));
184-
}).recover(error -> {
185-
return Future.failedFuture(new ServiceException(INTERNAL_ERROR, error.getMessage()));
186-
}).compose(result -> Future.succeededFuture());
187-
}
188-
189155
/**
190156
* Gets the options for the database connection pool.
191157
*

src/main/resources/templates/cookie.hbs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
hosted at: <em id="origin">{{origin}}</em>
1212
<p>
1313
<p>
14-
{{#if degradedAllowed}}
15-
Degraded versions of the content are accessible
16-
{{else}}
17-
The content is not accessible
18-
{{/if}}
19-
20-
to users outside of the Campus Network.
14+
Degraded versions of the content are accessible to users outside of the Campus Network.
2115
</p>
2216
<p>
2317
Your current IP address: <em id="client-ip-address">{{clientIpAddress}}</em>

src/test/java/edu/ucla/library/iiif/auth/handlers/AbstractHandlerIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ public void setUp(final Vertx aVertx, final VertxTestContext aContext) {
149149
final DatabaseService db = DatabaseService.create(aVertx, config);
150150
@SuppressWarnings("rawtypes")
151151
final List<Future> dbOps = List.of(db.setAccessMode(TEST_ID_OPEN_ACCESS, 0),
152-
db.setAccessMode(TEST_ID_TIERED_ACCESS, 1), db.setAccessMode(TEST_ID_ALL_OR_NOTHING_ACCESS, 2),
153-
db.setDegradedAllowed(TEST_ORIGIN, true));
152+
db.setAccessMode(TEST_ID_TIERED_ACCESS, 1), db.setAccessMode(TEST_ID_ALL_OR_NOTHING_ACCESS, 2));
154153

155154
myConfig = config;
156155
myWebClient = WebClient.create(aVertx);

0 commit comments

Comments
 (0)