source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/openid/provider/renderinginterface/buffet/__init__.py @ 4775

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/openid/provider/renderinginterface/buffet/__init__.py@4775
Revision 4775, 5.6 KB checked in by pjkersha, 11 years ago (diff)
  • Moved StaticURLParser app for serving OpenID Provider static content from into a Paste ini file [composit:...] - for combined services unit tests and default and full paster templates
  • Added main_app factory class method to OpenIDProviderMiddleware to fit main_app function signature required for Paste ini file to run OpenID Provider as the main app rather than as a filter.
Line 
1"""NDG Security Pylons Buffet based Rendering Interface for
2OpenIDProviderMiddleware
3
4NERC Data Grid Project
5
6This software may be distributed under the terms of the Q Public License,
7version 1.0 or later.
8"""
9__author__ = "P J Kershaw"
10__date__ = "14/08/08"
11__copyright__ = "(C) 2009 Science and Technology Facilities Council"
12__contact__ = "Philip.Kershaw@stfc.ac.uk"
13__revision__ = "$Id$"
14import logging
15log = logging.getLogger(__name__)
16
17import httplib
18from pylons.templating import Buffet
19
20# Rendering classes for OpenID Provider must derive from generic render
21# interface
22from ndg.security.server.wsgi.openid.provider import RenderingInterface, \
23    AuthNInterfaceConfigError
24
25# Boiler plate to create renderer
26class OpenIDProviderRenderingBuffet(Buffet):
27    def _update_names(self, ns):
28        return ns
29
30class BuffetRendering(RenderingInterface):
31    """Provide Templating for OpenID Provider Middleware via Buffet
32    class"""
33
34    propNames = (
35        'templateType', 
36        'templateRoot',
37        'baseURL',
38        'leftLogo',
39        'leftAlt',
40        'ndgLink',
41        'ndgImage',
42        'disclaimer',
43        'stfcLink',
44        'stfcImage',
45        'helpIcon',
46    )
47       
48    def __init__(self, *arg, **opt):
49        '''Extend RenderingInterface to include config and set-up for Buffet
50        object
51       
52        @type *arg: tuple
53        @param *arg: RenderingInterface parent class arguments
54        @type **opt: dict
55        @param **opt: additional keywords to set-up Buffet rendering'''
56        super(BuffetRendering, self).__init__(*arg, **opt)
57       
58        try:
59            for i in opt:
60                setattr(self, i, opt[i])
61        except KeyError, e:
62            raise AuthNInterfaceConfigError("Missing property: %s" % e)   
63         
64        self._buffet = OpenIDProviderRenderingBuffet(self.templateType, 
65                                            template_root=self.templateRoot)
66       
67        self.title = ''
68        self.xml = ''
69        self.headExtras = ''
70        self.loginStatus = True
71       
72    def _render(self, templateName, c=None, **kw):
73        '''Wrapper for Buffet.render'''
74        if c is None:
75            c = self
76           
77        kw['c'] = c
78       
79        rendering = self._buffet.render(template_name=templateName, 
80                                        namespace=kw)
81        return rendering
82   
83    def login(self, environ, start_response, success_to=None, fail_to=None, 
84              msg=''):
85        """Set-up template for OpenID Provider Login"""
86        self.title = "OpenID Login"
87        self.success_to = success_to or self.urls['url_mainpage']
88        self.fail_to = fail_to or self.urls['url_mainpage'] 
89        self.xml = msg
90       
91        response = self._render('ndg.security.login')
92        start_response('200 OK', 
93                       [('Content-type', 'text/html'+self.charset),
94                        ('Content-length', str(len(response)))])
95        self.xml = ''
96        return response
97       
98       
99    def mainPage(self, environ, start_response):
100        """Set-up template for OpenID Provider Login"""
101        self.title = "OpenID Provider"
102        self.headExtras = '<meta http-equiv="x-xrds-location" content="%s"/>'%\
103                        self.urls['url_serveryadis']
104   
105        response = self._render('ndg.security.mainPage')
106        start_response('200 OK', 
107                       [('Content-type', 'text/html'+self.charset),
108                        ('Content-length', str(len(response)))])
109        return response
110
111    def identityPage(self, environ, start_response):
112        """Render the user's Identity page"""
113        path = environ['PATH_INFO'].rstrip('/')
114        idPath = self.urls['url_id'].replace(self.base_url, '')
115        userIdentifier = path[len(idPath)+1:]
116        if not userIdentifier:
117            h.redirect_to(self.urls['url_mainpage'])
118           
119        self.title = "OpenID Identity Page"
120                       
121        link_tag = '<link rel="openid.server" href="%s"/>' % \
122                    self.urls['url_openidserver']
123             
124        yadis_loc_tag = '<meta http-equiv="x-xrds-location" content="%s"/>' % \
125            (self.urls['url_yadis']+'/'+userIdentifier)
126           
127        self.headExtras = link_tag + yadis_loc_tag
128        identityURL = self.base_url + path
129        self.xml = "<b><pre>%s</pre></b>" % identityURL
130       
131        response = self._render('ndg.security.identityPage')   
132        start_response("200 OK", 
133                       [('Content-type', 'text/html'+self.charset),
134                        ('Content-length', str(len(response)))])
135        self.xml = ''
136        return response
137
138   
139    def decidePage(self, environ, start_response, oidRequest):
140        """Handle user interaction required before final submit back to Relying
141        Party"""
142        self.title = 'Approve OpenID Request?'
143        self.trust_root = oidRequest.trust_root
144        self.oidRequest = oidRequest
145        self.environ = environ
146       
147        response = self._render('ndg.security.decidePage')
148        start_response("200 OK", 
149                       [('Content-type', 'text/html'+self.charset),
150                        ('Content-length', str(len(response)))])
151        return response
152
153       
154    def errorPage(self, environ, start_response, msg, code=500):
155        '''Display error information'''
156        self.title = 'Error with OpenID Provider'
157        self.xml = msg
158        response = self._render('ndg.security.error')
159        start_response('%d %s' % (code, httplib.responses[code]), 
160                       [('Content-type', 'text/html'+self.charset),
161                        ('Content-length', str(len(response)))])
162        self.xml = ''
163        return response
Note: See TracBrowser for help on using the repository browser.