1 # Copyright (C) 2015 Ipsilon project Contributors, for license see COPYING
4 sys.stdout = sys.stderr
10 from openid.consumer import consumer
11 from openid.extensions import sreg, ax
12 from openid_teams import teams
15 class OpenIDApp(object):
16 def index(self, extensions):
17 self.extensions = extensions == 'YES'
18 oidconsumer = consumer.Consumer(dict(), None)
20 request = oidconsumer.begin('http://127.0.0.10:45080/idp1/')
21 except Exception as ex:
22 return 'ERROR: %s' % ex
25 return 'ERROR: No request'
27 # Attach extensions here
29 request.addExtension(sreg.SRegRequest(
30 required=['nickname', 'email', 'timezone']))
31 ax_req = ax.FetchRequest()
32 ax_req_name = ax.AttrInfo('http://schema.openid.net/namePerson')
33 ax_req.add(ax_req_name)
34 request.addExtension(ax_req)
35 username = pwd.getpwuid(os.getuid())[0]
36 request.addExtension(teams.TeamsRequest(requested=[username]))
38 # Build and send final request
39 trust_root = cherrypy.url()
40 return_to = trust_root + 'finish'
41 if request.shouldSendRedirect():
42 redirect_url = request.redirectURL(
43 trust_root, return_to)
44 raise cherrypy.HTTPRedirect(redirect_url)
46 return request.htmlMarkup(
47 trust_root, return_to)
50 def finish(self, **args):
51 oidconsumer = consumer.Consumer(dict(), None)
52 info = oidconsumer.complete(cherrypy.request.params, cherrypy.url())
53 display_identifier = info.getDisplayIdentifier()
55 if info.status == consumer.FAILURE and display_identifier:
56 return 'ERROR:Verification of %s failed: %s' % (
57 display_identifier, info.message)
58 elif info.status == consumer.CANCEL:
59 return 'ERROR: Cancelled'
60 elif info.status == consumer.SUCCESS:
61 username = pwd.getpwuid(os.getuid())[0]
62 expected_identifier = 'http://127.0.0.10:45080/idp1/openid/id/%s/'\
64 if expected_identifier != display_identifier:
65 return 'ERROR: Wrong id returned: %s != %s' % (
70 sreg_resp = sreg.SRegResponse.fromSuccessResponse(info)
71 teams_resp = teams.TeamsResponse.fromSuccessResponse(info)
72 ax_resp = ax.FetchResponse.fromSuccessResponse(info)
75 return 'ERROR: No sreg!'
76 elif teams_resp is None:
77 return 'ERROR: No teams!'
79 return 'ERROR: No AX!'
82 expected_name = 'Test User %s' % username
83 expected_email = '%s@example.com' % username
85 ax_name = ax_resp.data[
86 'http://schema.openid.net/namePerson'][0]
87 sreg_email = sreg_resp.data['email']
89 if ax_name != expected_name:
90 return 'ERROR: Wrong name returned: %s != %s' % (
94 if sreg_email != expected_email:
95 return 'ERROR: Wrong email returned: %s != %s' % (
99 if username not in teams_resp.teams:
100 return 'ERROR: User not in self-named group (%s not in %s)' %\
101 (username, teams_resp.teams)
104 return 'SUCCESS, WITH EXTENSIONS'
106 return 'SUCCESS, WITHOUT EXTENSIONS'
108 return 'ERROR: Strange error: %s' % info.message
109 finish.exposed = True
112 cherrypy.config['environment'] = 'embedded'
114 application = cherrypy.Application(OpenIDApp(),
115 script_name=None, config=None)