private void defendAgainstCSRFAttacks(RequestParser aRequestParser) throws BadRequestException {
if( requestNeedsDefendingAgainstCSRFAttacks(aRequestParser) ) {
Id postedTokenValue = aRequestParser.toId(fCSRF_REQ_PARAM);
if ( FAILS == toIncludeCsrfTokenWithForm(postedTokenValue) ){
fLogger.severe("CSRF token not included in POSTed request. Rejecting this request, since it is likely an attack.");
throw new BadRequestException(HttpServletResponse.SC_BAD_REQUEST);
}
if( FAILS == matchCurrentCSRFToken(aRequestParser, postedTokenValue) ) {
if( FAILS == matchPreviousCSRFToken(aRequestParser, postedTokenValue) ) {
fLogger.severe("CSRF token does not match the expected value. Rejecting this request, since it is likely an attack.");
throw new BadRequestException(HttpServletResponse.SC_BAD_REQUEST);
}
}
fLogger.fine("Success: no CSRF problem detected.");
}
}