source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/pylons/container/lib/openid_provider_util.py @ 4404

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/pylons/container/lib/openid_provider_util.py@4404
Revision 4404, 7.0 KB checked in by pjkersha, 13 years ago (diff)

Updated contact e-mail address

Line 
1"""NDG Security OpenID Provider Pylons utilities
2
3Includes Pylons based rendering interface for OpenIDProviderMiddleware
4
5NERC Data Grid Project
6
7This software may be distributed under the terms of the Q Public License,
8version 1.0 or later.
9"""
10__author__ = "P J Kershaw"
11__date__ = "14/08/08"
12__copyright__ = "(C) 2008 STFC & NERC"
13__contact__ = "Philip.Kershaw@stfc.ac.uk"
14__revision__ = "$Id$"
15import pylons
16from pylons.templating import Buffet
17from pylons import config
18import ndg.security.server.sso.sso.lib.helpers as h
19import logging
20log = logging.getLogger(__name__)
21
22# Boiler plate to create renderer
23class MyBuffet(Buffet):
24    def _update_names(self, ns):
25        return ns
26
27def_eng = config['buffet.template_engines'][0]
28buffet = MyBuffet(
29    def_eng['engine'],
30    template_root=def_eng['template_root'],
31    **def_eng['template_options']
32)
33
34for e in config['buffet.template_engines'][1:]:
35    buffet.prepare(
36        e['engine'],
37        template_root=e['template_root'],
38        alias=e['alias'],
39        **e['template_options']
40    )
41
42class State:
43    '''Convenience class for passing parameters from rendering interface to
44    individual template files'''
45    def __init__(self, urls={}, session={}):
46        self.title = ''
47        self.xml = ''
48        self.headExtras = ''
49        self.session = session
50        self.loginStatus = True
51        self.urls = urls
52
53def _render(templateName, **kw):
54    '''Wrapper for Buffet.render'''
55    rendering = buffet.render(template_name=templateName, namespace=kw)
56    return rendering
57
58
59# Rendering classes for OpenID Provider must derive from generic render
60# interface
61from ndg.security.server.wsgi.openid_provider import RenderingInterface
62
63class OpenIDProviderKidRendering(RenderingInterface):
64    """Provide Kid Templating for OpenID Provider Middleware"""
65   
66    def renderLogin(self, environ, success_to=None, fail_to=None, msg=''):
67        """Set-up Kid template for OpenID Provider Login"""
68        c = State(urls=self.urls, session=self.session)
69        c.title = "OpenID Login"
70        c.success_to = success_to or self.urls['url_mainpage']
71        c.fail_to = fail_to or self.urls['url_mainpage'] 
72        c.xml = msg
73        return _render('ndg.security.login', c=c, g=config, h=h)
74       
75       
76    def renderMainPage(self, environ):
77        """Set-up Kid template for OpenID Provider Login"""
78        c = State(urls=self.urls, session=self.session)
79        c.title = "OpenID Provider"
80        c.headExtras = '<meta http-equiv="x-xrds-location" content="%s"/>' % \
81                        self.urls['url_serveryadis']
82   
83        return _render('ndg.security.mainPage', c=c, g=config, h=h)
84
85
86    def renderIdentityPage(self, environ):
87        """Identity page"""
88        path = environ['PATH_INFO'].rstrip('/')
89        idPath = self.urls['url_id'].replace(self.base_url, '')
90        username = path[len(idPath)+1:]
91        if not username:
92            h.redirect_to(self.urls['url_mainpage'])
93           
94        c = State(urls=self.urls, session=self.session)
95        c.title = "OpenID Identity Page"
96                       
97        link_tag = '<link rel="openid.server" href="%s"/>' % \
98              self.urls['url_openidserver']
99             
100        yadis_loc_tag = '<meta http-equiv="x-xrds-location" content="%s"/>' % \
101            (self.urls['url_yadis']+'/'+username)
102           
103        c.headExtras = link_tag + yadis_loc_tag
104        identityURL = self.base_url + path
105        c.xml = "<b><pre>%s</pre></b>" % identityURL
106       
107        return _render('ndg.security.identityPage', c=c, g=config, h=h)   
108
109   
110    def renderDecidePage(self, environ, oidRequest):
111        """Handle user interaction required before final submit back to Relying
112        Party"""
113        c = State(urls=self.urls, session=self.session)
114        c.title = 'Approve OpenID Request?'
115        c.trust_root = oidRequest.trust_root
116        c.oidRequest = oidRequest
117       
118        return _render('ndg.security.decidePage', c=c,g=config,h=h)
119
120       
121    def renderErrorPage(self, environ, msg):
122        c = State(urls=self.urls, session=self.session)
123        c.title = 'Error with OpenID Provider'
124        c.xml = msg
125        return _render('ndg.security.error', c=c, g=config, h=h)
126
127# Earth System Grid interoperability tests
128
129#esgAxAttr = {'urn:esg.security.gateway': 'BADC',
130#        'urn:esg.security.authority': 'group_IPCC_role_default',
131#        'http://axschema.org/namePerson/last': 'UserLastName',
132#        'http://axschema.org/contact/country/home': 'UK',
133#        'http://axschema.org/namePerson/middle': 'UserMiddleName',
134#        'urn:esg.security.uuid': '0123456789abcdef',
135#        'http://axschema.org/namePerson/first': 'UserFirstName',
136#        'http://axschema.org/namePerson/friendly': '',
137#        'http://axschema.org/contact/email': 'tester@test.com',
138#        'urn:esg.security.organization': 'British Atmospheric Data Centre',
139#}
140
141esgAxAttr = {
142 'http://openid.net/schema/contact/state/home': 'Oxfordshire', 
143 'http://openid.net/schema/namePerson/middle': 'George', 
144 'http://openid.net/schema/contact/city/home': 'Didcot', 
145 'http://openid.net/schema/person/guid': '0123456789abcdef', 
146 'http://openid.net/schema/namePerson/friendly': 'username', 
147 'http://openid.net/schema/company/name': 'The British Atmospheric Data Centre', 
148 'http://openid.net/schema/contact/country/home': 'UK', 
149 'http://openid.net/schema/namePerson/first': 'John', 
150 'http://openid.net/schema/namePerson/last': 'Smith', 
151 'http://openid.net/schema/contact/internet/email': 'testABC@rl.ac.uk',
152 'http://www.earthsystemgrid.org/authority': 'group_IPCC_role_default',
153 'http://www.earthsystemgrid.org/gateway': 'BADC',
154}
155esgAxAlias = {
156 'http://openid.net/schema/contact/state/home': 'state', 
157 'http://openid.net/schema/namePerson/middle': 'middlename', 
158 'http://openid.net/schema/contact/city/home': 'city', 
159 'http://openid.net/schema/person/guid': 'uuid', 
160 'http://openid.net/schema/namePerson/friendly': 'username', 
161 'http://openid.net/schema/company/name': 'organization', 
162 'http://openid.net/schema/contact/country/home': 'country', 
163 'http://openid.net/schema/namePerson/first': 'firstname', 
164 'http://openid.net/schema/namePerson/last': 'lastname', 
165 'http://openid.net/schema/contact/internet/email': 'email',
166 'http://www.earthsystemgrid.org/authority': 'authority',
167 'http://www.earthsystemgrid.org/gateway': 'gateway',
168              }
169
170esgSRegAttr = {
171    'nickname':'',
172    'email':'E-mail Address',
173    'country':'UK',
174    'language':'English',
175    'timezone':'BST',
176    }
177
178
179def esgSRegResponseHandler(username):
180    """Interface function to OpenIdProviderMiddleware to set custom attributes
181    """
182    attr = esgSRegAttr.copy()
183#    attr['username'] = username
184    attr['nickname'] = username
185    return attr
186
187def esgAXResponseHandler(axReq, axResp, username): 
188    """Respond to attributes requested by Relying Party via the Attribute
189    Exchange interface"""
190    attr = esgAxAttr.copy()
191    attr['http://openid.net/schema/namePerson/friendly'] = username
192   
193    for typeURI, attrInfo in axReq.requested_attributes.items():
194        # Value input must be list type
195        axResp.setValues(typeURI, [attr[typeURI]])
Note: See TracBrowser for help on using the repository browser.