src/test/java/edu/ucla/library/iiif/auth/services/AccessCookieServiceTest.java
@Test
    @SuppressWarnings("checkstyle:BooleanExpressionComplexity")
    public final void testInvalidateTamperedCookie(final Vertx aVertx, final VertxTestContext aContext) {
        final String clientIpAddress = LOCALHOST;
        final boolean isCampusNetwork = false;
        final boolean isDegradedAllowed = false;

        myServiceProxy.generateCookie(clientIpAddress, isCampusNetwork, isDegradedAllowed).compose(cookie -> {
            final JsonObject decodedCookie = new JsonObject(new String(Base64.getDecoder().decode(cookie.getBytes())));
            final byte[] secret = decodedCookie.getBinary(CookieJsonKeys.SECRET).clone();
            final String tamperedCookie;

            // Flip some bits in the secret
            secret[new Random().nextInt(secret.length)] += 1;

            decodedCookie.put(CookieJsonKeys.SECRET, secret);
            tamperedCookie = Base64.getEncoder().encodeToString(decodedCookie.encode().getBytes());

            return myServiceProxy.decryptCookie(tamperedCookie, clientIpAddress);
        }).onFailure(details -> {
            final ServiceException error = (ServiceException) details;

            assertEquals(AccessCookieServiceError.INVALID_COOKIE, AccessCookieServiceImpl.getError(error));

            aContext.completeNow();
        }).onSuccess(decryptedCookie -> {
            // Somehow we still got syntactically valid JSON; make sure the structure is not semantically valid
            if (decryptedCookie.containsKey(CookieJsonKeys.CLIENT_IP_ADDRESS) &&
                    decryptedCookie.getString(CookieJsonKeys.CLIENT_IP_ADDRESS).equals(clientIpAddress) &&
                    decryptedCookie.containsKey(CookieJsonKeys.CAMPUS_NETWORK) &&
                    decryptedCookie.getBoolean(CookieJsonKeys.CAMPUS_NETWORK) == isCampusNetwork &&
                    decryptedCookie.containsKey(CookieJsonKeys.DEGRADED_ALLOWED) &&
                    decryptedCookie.getBoolean(CookieJsonKeys.DEGRADED_ALLOWED) == isDegradedAllowed) {
                aContext.failNow(StringUtils.format(MessageCodes.AUTH_009, decryptedCookie));
            } else {
                aContext.completeNow();
            }
        });
    }
/**
     * Tests that a tampered cookie will not be accepted.
     *
     * @param aVertx A Vert.x instance
     * @param aContext A test context
     */
Tests that a tampered cookie will not be accepted.
@param aVertx A Vert.x instance
@param aContext A test context
Tests that a tampered cookie will not be accepted . @param aVertx A Vert . x instance @param aContext A test context
@Test
    @SuppressWarnings("checkstyle:BooleanExpressionComplexity")
    public final void testInvalidateTamperedCookie(final Vertx aVertx, final VertxTestContext aContext) {
        final String clientIpAddress = LOCALHOST;
        final boolean isCampusNetwork = false;
        final boolean isDegradedAllowed = false;
        myServiceProxy.generateCookie(clientIpAddress, isCampusNetwork, isDegradedAllowed).compose(cookie -> {
            final JsonObject decodedCookie = new JsonObject(new String(Base64.getDecoder().decode(cookie.getBytes())));
            final byte[] secret = decodedCookie.getBinary(CookieJsonKeys.SECRET).clone();
            final String tamperedCookie;
            secret[new Random().nextInt(secret.length)] += 1;
            decodedCookie.put(CookieJsonKeys.SECRET, secret);
            tamperedCookie = Base64.getEncoder().encodeToString(decodedCookie.encode().getBytes());
            return myServiceProxy.decryptCookie(tamperedCookie, clientIpAddress);
        }).onFailure(details -> {
            final ServiceException error = (ServiceException) details;
            assertEquals(AccessCookieServiceError.INVALID_COOKIE, AccessCookieServiceImpl.getError(error));
            aContext.completeNow();
        }).onSuccess(decryptedCookie -> {
            if (decryptedCookie.containsKey(CookieJsonKeys.CLIENT_IP_ADDRESS) &&
                    decryptedCookie.getString(CookieJsonKeys.CLIENT_IP_ADDRESS).equals(clientIpAddress) &&
                    decryptedCookie.containsKey(CookieJsonKeys.CAMPUS_NETWORK) &&
                    decryptedCookie.getBoolean(CookieJsonKeys.CAMPUS_NETWORK) == isCampusNetwork &&
                    decryptedCookie.containsKey(CookieJsonKeys.DEGRADED_ALLOWED) &&
                    decryptedCookie.getBoolean(CookieJsonKeys.DEGRADED_ALLOWED) == isDegradedAllowed) {
                aContext.failNow(StringUtils.format(MessageCodes.AUTH_009, decryptedCookie));
            } else {
                aContext.completeNow();
            }
        });
    }
@ Test @ SuppressWarnings ( "checkstyle:BooleanExpressionComplexity" ) public final void testInvalidateTamperedCookie ( final Vertx aVertx , final VertxTestContext aContext ) { final String clientIpAddress = LOCALHOST ; final boolean isCampusNetwork = false ; final boolean isDegradedAllowed = false ; myServiceProxy . generateCookie ( clientIpAddress , isCampusNetwork , isDegradedAllowed ) . compose ( cookie -> { final JsonObject decodedCookie = new JsonObject ( new String ( Base64 . getDecoder ( ) . decode ( cookie . getBytes ( ) ) ) ) ; final byte [ ] secret = decodedCookie . getBinary ( CookieJsonKeys . SECRET ) . clone ( ) ; final String tamperedCookie ; secret [ new Random ( ) . nextInt ( secret . length ) ] += 1 ; decodedCookie . put ( CookieJsonKeys . SECRET , secret ) ; tamperedCookie = Base64 . getEncoder ( ) . encodeToString ( decodedCookie . encode ( ) . getBytes ( ) ) ; return myServiceProxy . decryptCookie ( tamperedCookie , clientIpAddress ) ; } ) . onFailure ( details -> { final ServiceException error = ( ServiceException ) details ; assertEquals ( AccessCookieServiceError . INVALID_COOKIE , AccessCookieServiceImpl . getError ( error ) ) ; aContext . completeNow ( ) ; } ) . onSuccess ( decryptedCookie -> { if ( decryptedCookie . containsKey ( CookieJsonKeys . CLIENT_IP_ADDRESS ) && decryptedCookie . getString ( CookieJsonKeys . CLIENT_IP_ADDRESS ) . equals ( clientIpAddress ) && decryptedCookie . containsKey ( CookieJsonKeys . CAMPUS_NETWORK ) && decryptedCookie . getBoolean ( CookieJsonKeys . CAMPUS_NETWORK ) == isCampusNetwork && decryptedCookie . containsKey ( CookieJsonKeys . DEGRADED_ALLOWED ) && decryptedCookie . getBoolean ( CookieJsonKeys . DEGRADED_ALLOWED ) == isDegradedAllowed ) { aContext . failNow ( StringUtils . format ( MessageCodes . AUTH_009 , decryptedCookie ) ) ; } else { aContext . completeNow ( ) ; } } ) ; }
Tests that a tampered cookie will not be accepted.
Tests that a tampered cookie will not be accepted .
// Flip some bits in the secret // Somehow we still got syntactically valid JSON; make sure the structure is not semantically valid