1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
"""Internal forward middleware for manual authentication handling
For an example of the use of this middleware read the main AuthKit manual or
look at the Pylons example at http://pylonshq.com/project/pylonshq/wiki/PylonsWithAuthKitForward
"""
from paste.recursive import RecursiveMiddleware, ForwardRequestException, \
CheckForRecursionMiddleware
from authkit.authenticate.multi import MultiHandler, status_checker
from authkit.authenticate import AuthKitConfigError
import warnings
class Redirect(object):
def __init__(self, app, forward_signin):
self.app = app
self.signin_path = forward_signin
def __call__(self, environ, start_response):
raise ForwardRequestException(self.signin_path)
class MyRecursive(object):
def __init__(self, app):
self.application = app
def __call__(self, environ, start_response):
try:
result = []
app_iter = self.application(environ, start_response)
for data in app_iter:
result.append(data)
if hasattr(app_iter, 'close'):
app_iter.close()
return result
except ForwardRequestException, e:
return CheckForRecursionMiddleware(e.factory(self), environ)(environ, start_response)
def make_forward_handler(
app,
auth_conf,
app_conf=None,
global_conf=None,
prefix='authkit.forward',
):
signin_path = None
if auth_conf.has_key('internalpath'):
warnings.warn(
'The %sinternalpath key is deprecated. Please use '
'%ssigninpath.'%(prefix, prefix),
DeprecationWarning,
2
)
signin_path = auth_conf['internalpath']
elif auth_conf.has_key('signinpath'):
signin_path = auth_conf['signinpath']
else:
raise AuthKitConfigError("No %ssigninpath key specified"%prefix)
app = MultiHandler(app)
app.add_method(
'forward',
Redirect,
signin_path
)
app.add_checker('forward', status_checker)
app = MyRecursive(RecursiveMiddleware(app))
return app
|