# Path: pylons/controllers/util.py
# def abort(status_code=None, detail="", headers=None, comment=None):
#     """Aborts the request immediately by returning an HTTP exception
#     
#     In the event that the status_code is a 300 series error, the detail
#     attribute will be used as the Location header should one not be
#     specified in the headers attribute.
#     
#     """
#     exc = status_map[status_code](detail=detail, headers=headers, 
#                                   comment=comment)
#     log.debug("Aborting request, status: %s, detail: %r, headers: %r, "
#               "comment: %r", status_code, detail, headers, comment)
#     raise exc.exception
# 
# def redirect(url, code=302):
#     """Raises a redirect exception to the specified URL
# 
#     Optionally, a code variable may be passed with the status code of
#     the redirect, ie::
# 
#         redirect(url(controller='home', action='index'), code=303)
# 
#     """
#     log.debug("Generating %s redirect" % code)
#     exc = status_map[code]
#     raise exc(location=url).exception


import logging
import urlparse
    import webhelpers.html.secure_form as secure_form
    import webhelpers.pylonslib.secure_form as secure_form
from decorator import decorator
from pylons.controllers.util import abort, redirect
from pylons.decorators.util import get_pylons
    "Cross-site request forgery detected, request denied. See "
    "http://en.wikipedia.org/wiki/Cross-site_request_forgery for more "
    "information.")

def authenticated_form(params):
    submitted_token = params.get(secure_form.token_key)
    return submitted_token is not None and \
        submitted_token == secure_form.authentication_token()


@decorator
def authenticate_form(func, *args, **kwargs):
    """Decorator for authenticating a form

    This decorator uses an authorization token stored in the client's
    session for prevention of certain Cross-site request forgery (CSRF)
    attacks (See
    http://en.wikipedia.org/wiki/Cross-site_request_forgery for more
    information).

    For use with the ``webhelpers.html.secure_form`` helper functions.

    """
    request = get_pylons(args).request
    if authenticated_form(request.POST):
        del request.POST[secure_form.token_key]
        return func(*args, **kwargs)
    else:
        log.warn('Cross-site request forgery detected, request denied: %r '
                 'REMOTE_ADDR: %s' % (request, request.remote_addr))

# Path: pylons/controllers/util.py
# def abort(status_code=None, detail="", headers=None, comment=None):
#     """Aborts the request immediately by returning an HTTP exception
#     
#     In the event that the status_code is a 300 series error, the detail
#     attribute will be used as the Location header should one not be
#     specified in the headers attribute.
#     
#     """
#     exc = status_map[status_code](detail=detail, headers=headers, 
#                                   comment=comment)
#     log.debug("Aborting request, status: %s, detail: %r, headers: %r, "
#               "comment: %r", status_code, detail, headers, comment)
#     raise exc.exception
# 
# def redirect(url, code=302):
#     """Raises a redirect exception to the specified URL
# 
#     Optionally, a code variable may be passed with the status code of
#     the redirect, ie::
# 
#         redirect(url(controller='home', action='index'), code=303)
# 
#     """
#     log.debug("Generating %s redirect" % code)
#     exc = status_map[code]
#     raise exc(location=url).exception


# Path: pylons/decorators/secure.py
import logging
import urlparse
    import webhelpers.html.secure_form as secure_form
    import webhelpers.pylonslib.secure_form as secure_form
from decorator import decorator
from pylons.controllers.util import abort, redirect
from pylons.decorators.util import get_pylons

    "Cross-site request forgery detected, request denied. See "
    "http://en.wikipedia.org/wiki/Cross-site_request_forgery for more "
    "information.")

def authenticated_form(params):
    submitted_token = params.get(secure_form.token_key)
    return submitted_token is not None and \
        submitted_token == secure_form.authentication_token()


@decorator
def authenticate_form(func, *args, **kwargs):
    """Decorator for authenticating a form

    This decorator uses an authorization token stored in the client's
    session for prevention of certain Cross-site request forgery (CSRF)
    attacks (See
    http://en.wikipedia.org/wiki/Cross-site_request_forgery for more
    information).

    For use with the ``webhelpers.html.secure_form`` helper functions.

    """
    request = get_pylons(args).request
    if authenticated_form(request.POST):
        del request.POST[secure_form.token_key]
        return func(*args, **kwargs)
    else:
        log.warn('Cross-site request forgery detected, request denied: %r '
                 'REMOTE_ADDR: %s' % (request, request.remote_addr))

        abort(403, detail=csrf_detected_message)