source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/integration/authz/securedapp.py @ 5037

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/integration/authz/securedapp.py@5039
Revision 5037, 2.3 KB checked in by pjkersha, 11 years ago (diff)

ndg.security.server.wsgi.authn: AuthNMiddleware and AuthNRedirectMiddleware catch HTTP 401 responses from a WSGI application stack to be protected and redirect to OpenID Relying Party middleware running on an application server running NDG Security services.

Line 
1class TestAuthNMiddleware(object):
2    '''Test Application for the Authentication handler to protect'''
3    response = "Test Authentication redirect application"
4       
5    def __init__(self, app_conf, **local_conf):
6        pass
7   
8    def __call__(self, environ, start_response):
9       
10        if environ['PATH_INFO'] == '/test_401WithNotLoggedIn':
11            status = "401 Unauthorized"
12           
13        else:
14            status = "404 Not found"
15               
16#        start_response(status,
17#                       [('Content-length',
18#                         str(len(TestAuthNMiddleware.response))),
19#                        ('Content-type', 'text/plain')])
20#        return [TestAuthNMiddleware.response]
21        return self._setResponse(environ, start_response)
22   
23    def _setResponse(self, environ, start_response):
24        if 'REMOTE_USER' in environ:
25            response = """<html>
26    <head/>
27    <body>
28        <p>Authenticated!</p>
29        <p><a href="/logout">logout</a></p>
30    </body>
31</html>"""
32            start_response('200 OK', 
33                           [('Content-type', 'text/html'),
34                            ('Content-length', str(len(response)))])
35        else:
36            response = "Trigger OpenID Relying Party..."
37            start_response('401 Unauthorized', 
38                           [('Content-type', 'text/plain'),
39                            ('Content-length', str(len(response)))])
40        return [response]
41   
42def app_factory(globalConfig, **localConfig):
43    return AuthZTestMiddleware(None, globalConfig, **localConfig)
44
45def filter_app_factory(app, globalConfig, **localConfig):
46    return AuthZTestMiddleware(app, globalConfig, **localConfig)
47   
48# To start run
49# $ paster serve services.ini or run this file as a script
50# $ ./securedapp.py [port #]
51if __name__ == '__main__':
52    import sys
53    import os
54    from os.path import dirname, abspath
55    import logging
56    logging.basicConfig(level=logging.DEBUG)
57
58    if len(sys.argv) > 1:
59        port = int(sys.argv[1])
60    else:
61        port = 7080
62       
63    cfgFilePath = os.path.join(dirname(abspath(__file__)), 'securedapp.ini')
64       
65    from paste.httpserver import serve
66    from paste.deploy import loadapp
67   
68    app = loadapp('config:%s' % cfgFilePath)
69    serve(app, host='0.0.0.0', port=port)
Note: See TracBrowser for help on using the repository browser.