• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/ZSI/auth.py

00001 #! /usr/bin/env python
00002 # $Header$
00003 '''Simple CGI dispatching.
00004 '''
00005 
00006 from ZSI import *
00007 from ZSI import _copyright
00008 import base64, os
00009 
00010 _b64_decode = base64.decodestring
00011 
00012 # Typecode to parse a ZSI BasicAuth header.
00013 _auth_tc = TC.Struct(None,
00014                         [ TC.String('Name'), TC.String('Password') ],
00015                         extras=1)
00016 
00017 class AUTH:
00018     '''Constants for authentication mechanisms.
00019     '''
00020     none = 0
00021     httpbasic = 1
00022     zsibasic = 2
00023     httpdigest = 4
00024 
00025 class ClientBinding:
00026     '''Information about the client that is connected to us.
00027     '''
00028 
00029     def __init__(self, ps):
00030         self.ps, self.auth = \
00031             ps, None
00032         self.environ = os.environ.copy()
00033         self.environ['CONTENT_LENGTH'] = str(0)
00034 
00035     def GetAuth(self):
00036         '''Return a tuple containing client authentication data.
00037         '''
00038         if self.auth: return self.auth
00039         for elt in self.ps.GetMyHeaderElements():
00040             if elt.localName == 'BasicAuth' \
00041             and elt.namespaceURI == ZSI_SCHEMA_URI:
00042                 d = _auth_tc.parse(elt, self.ps)
00043                 self.auth = (AUTH.zsibasic, d['Name'], d['Password'])
00044                 return self.auth
00045         ba = self.environ.get('HTTP_AUTHENTICATION')
00046         if ba:
00047             ba = ba.split(' ')
00048             if len(ba) == 2 and ba[0].lower() == 'basic':
00049                 ba = _b64_decode(ba[1])
00050                 self.auth = (AUTH.httpbasic,) + tuple(ba.split(':'))
00051                 return self.auth
00052         self.auth = (AUTH.none,)
00053         return self.auth
00054 
00055     def GetNS(self):
00056         '''Return namespace for the top main request element.
00057         '''
00058         return self.ps.body_root.namespaceURI or ''
00059 
00060     def GetRequest(self):
00061         '''Return the ParsedSoap request.
00062         '''
00063         return self.ps
00064 
00065 if __name__ == '__main__': print _copyright

Generated on Wed Oct 20 2010 11:12:15 for APBS by  doxygen 1.7.2