source: TI12-security/trunk/python/Tests/Echo/EchoServer.py @ 1696

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/Tests/Echo/EchoServer.py@1696
Revision 1696, 4.6 KB checked in by pjkersha, 14 years ago (diff)

Working test code for ZSI Web Service using Twisted:

  • for server, used wsdl2dispatch - but make sure to edit the return types

in AttAuthority_services_server server class stubs to return request, response
tuple as required by Twisted interface. Created .tac file which loads a
sub class of the wsdl2dispatch generated class.

  • tested with standard ZSI client using stubs generated from wsdl2py. Also

tested with Twisted client - stubs generated with wsdl2py including the -w
option.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#
3# How to build an echo server using the extended code generation
4#
5import sys
6
7# Import the ZSI stuff you'd need no matter what
8from ZSI.ServiceContainer import ServiceContainer, SOAPRequestHandler
9
10# This is a new method imported to show it's value
11from ZSI.ServiceContainer import GetSOAPContext
12
13# Import the generated Server Object
14import EchoServer_interface
15
16# Set security operation by request type
17from EchoServer_messages import *
18
19import wsSecurity
20
21psBodyChildName = lambda ps: \
22    ps.dom.childNodes[1].childNodes[1].childNodes[0].localName
23
24priKeyPwd = None
25certFilePath = '../webSphereTestcert.pem'
26priKeyFilePath = '../webSphereTestkey.pem'
27
28   
29# Create a Server implementation
30
31#_____________________________________________________________________________
32class EchoSOAPRequestHandler(SOAPRequestHandler):
33     """Add a do_GET method to return the WSDL on HTTP GET requests.
34     Please note that the path to the wsdl file is derived from what
35     the HTTP invocation delivers (which is put into the self.path
36     attribute), so you might want to change this addressing scheme.
37     """
38     def do_GET(self):
39         """Return the WSDL file."""         
40         self.send_xml(EchoServer_interface.EchoServer._wsdl)
41         
42     def do_POST(self):
43          """Fudge to get _Dispatch to pick up the correct address
44          - seems to be necessary when putting proxy redirect for port in
45          the wsdl e.g. http://glue.badc.rl.ac.uk/sessionMgr points to the
46          default port for the Session Manager."""
47          self.path = "/EchoServIn"
48          SOAPRequestHandler.do_POST(self)
49   
50# This using a derived server instead
51class EchoServIn(EchoServer_interface.EchoServer):
52    def __init__(self, post='', **kw):
53        EchoServer_interface.EchoServer.__init__(self, post, **kw)
54
55        # Fudge to copy methods of this class so that equivalent
56        # SimpleCAService class WS stub picks it up
57        self.impl = self
58       
59        self.signatureHandler = wsSecurity.SignatureHandler(\
60                                    certFilePath=certFilePath,
61                                    priKeyFilePath=priKeyFilePath,
62                                    priKeyPwd=priKeyPwd)
63
64        self.encryptionHandler = wsSecurity.EncryptionHandler(\
65                                    certFilePath=certFilePath,
66                                    priKeyFilePath=priKeyFilePath,
67                                    priKeyPwd=priKeyPwd)
68       
69       
70    def sign(self, sw):
71        '''\
72        Overrides ServiceInterface class method to allow digital signature'''
73        if isinstance(self.request, EchoRequest):
74            # Echo response applies digital signature
75            self.signatureHandler.sign(sw)
76       
77    def verify(self, ps):
78        '''\
79        Overrides ServiceInterface class method to allow signature
80        verification'''     
81        if isinstance(self.request, EchoRequest):
82            # Echo request checks digital signature
83            self.signatureHandler.verify(ps)
84
85    def encrypt(self, sw):
86        if isinstance(self.request, EchoEncrRequest):
87            # Assume EchoEncr response - apply encryption
88            self.encryptionHandler.encrypt(sw)
89           
90    def decrypt(self, ps):
91        if psBodyChildName(ps) == 'EncryptedData':
92            # Assume EchoEncr request
93            self.encryptionHandler.decrypt(ps)
94       
95#
96#       
97#    def authorize(self, auth_info, post, action):
98#        print "Authorizing INHERIT Echo"
99#        ctx = GetSOAPContext()
100#        print dir(ctx)
101#        print "Container: ", ctx.connection
102#        print "Parsed SOAP: ", ctx.parsedsoap
103#        print "Container: ", ctx.container
104#        print "HTTP Headers:\n", ctx.httpheaders
105#        print "----"
106#        print "XML Data:\n", ctx.xmldata
107#        return 1
108
109    def Echo(self, input):
110        return "Input message was: %s" % input
111
112    def EchoEncr(self, input):
113        return "Input secret was: %s" % input
114
115# Here we set up the server
116serviceContainer = ServiceContainer(('localhost', 7100),
117                                    RequestHandlerClass=EchoSOAPRequestHandler)
118
119# Create the TIE version of the server
120#hws = EchoServer()
121#hwsi = EchoServer_interface.EchoServer(impl=hws,
122#                                       auth_method_name="authorize")
123#serviceContainer.setNode(hwsi, url="/EchoServer")
124
125# Create the Inherited version of the server
126import sys
127hws2 = EchoServIn(tracefile=sys.stdout)
128serviceContainer.setNode(hws2, url="/EchoServIn")
129
130try:
131    # Run the service container
132    serviceContainer.serve_forever()
133except KeyboardInterrupt:
134    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.