source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/openidrelyingparty/serverapp.py @ 4907

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/openidrelyingparty/serverapp.py@4907
Revision 4907, 2.7 KB checked in by pjkersha, 11 years ago (diff)
  • Single Sign On Service logout controller: fixed WSGI Session Manager client instantiation - added Session Manager environ key arg
  • Completed OpenIDRelyingPartyMiddleware - wraps Authkit OpenID RP code adding a custom signin template and logout capability.
Line 
1#!/usr/bin/env python
2"""NDG Security test harness for OpenID Provider Relying Party middleware
3
4NERC DataGrid Project
5
6"""
7__author__ = "P J Kershaw"
8__date__ = "20/11/08"
9__copyright__ = "(C) 2009 Science and Technology Facilities Council"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id$"
12import os
13from os.path import dirname, abspath, join
14
15class OpenIDRelyingPartyTestMiddleware(object):
16    method = {
17"/": 'default',
18"/test_401": "test_401",
19    }
20
21    def __init__(self, app, globalConfig, **localConfig):
22        self.app = app
23           
24    def __call__(self, environ, start_response):
25       
26        methodName = self.method.get(environ['PATH_INFO'], '').rstrip()
27        if methodName:
28            action = getattr(self, methodName)
29            return action(environ, start_response)
30        elif self.app is not None:
31            return self.app(environ, start_response)
32        else:
33            start_response('404 Not Found', [('Content-type', 'text/plain')])
34            return "OpenID Relying Party Unit tests: invalid URI"
35           
36    def default(self, environ, start_response):
37        start_response('200 OK', [('Content-type', 'text/plain')])
38        return "NDG Security OpenID Relying Party Unit Tests"
39
40    def test_401(self, environ, start_response):
41        if 'REMOTE_USER' in environ:
42            response = """<html>
43    <head/>
44    <body>
45        <p>Authenticated!</p>
46        <p><a href="/logout">logout</a></p>
47    </body>
48</html>"""
49            start_response('200 OK', 
50                           [('Content-type', 'text/html'),
51                            ('Content-length', str(len(response)))])
52        else:
53            response = "Trigger OpenID Relying Party..."
54            start_response('401 Unauthorized', 
55                           [('Content-type', 'text/plain'),
56                            ('Content-length', str(len(response)))])
57        return response
58
59def app_factory(globalConfig, **localConfig):
60    return OpenIDRelyingPartyTestMiddleware(None, globalConfig, **localConfig)
61
62def filter_app_factory(app, globalConfig, **localConfig):
63    return OpenIDRelyingPartyTestMiddleware(app, globalConfig, **localConfig)
64   
65# To start run
66# $ paster serve services.ini or run this file as a script
67# $ ./serverapp.py [port #]
68if __name__ == '__main__':
69    import sys
70    import logging
71    logging.basicConfig(level=logging.DEBUG)
72
73    if len(sys.argv) > 1:
74        port = int(sys.argv[1])
75    else:
76        port = 5600
77       
78    cfgFilePath = os.path.join(dirname(abspath(__file__)), 'services.ini')
79       
80    from paste.httpserver import serve
81    from paste.deploy import loadapp
82   
83    app = loadapp('config:%s' % cfgFilePath)
84    serve(app, host='0.0.0.0', port=port)
Note: See TracBrowser for help on using the repository browser.