Package ndg :: Package saml :: Package saml2 :: Package binding :: Package soap :: Package client :: Module attributequery
[hide private]

Source Code for Module ndg.saml.saml2.binding.soap.client.attributequery

  1  """SAML 2.0 client bindings module implements SOAP binding for attribute query 
  2   
  3  NERC DataGrid Project 
  4  """ 
  5  __author__ = "P J Kershaw" 
  6  __date__ = "02/09/09" 
  7  __copyright__ = "(C) 2009 Science and Technology Facilities Council" 
  8  __license__ = "http://www.apache.org/licenses/LICENSE-2.0" 
  9  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
 10  __revision__ = '$Id: attributequery.py 8049 2012-03-28 15:57:38Z pjkersha $' 
 11  import re 
 12  import logging 
 13  log = logging.getLogger(__name__) 
 14   
 15  from M2Crypto.m2urllib2 import HTTPSHandler 
 16   
 17  from ndg.saml.saml2.core import Attribute, AttributeQuery 
 18   
 19  from ndg.saml.utils import TypedList 
 20  from ndg.saml.saml2.binding.soap.client.subjectquery import ( 
 21                                                      SubjectQuerySOAPBinding, 
 22                                                      SubjectQueryResponseError) 
 23   
 24  # Prevent whole module breaking if this is not available - it's only needed for 
 25  # AttributeQuerySslSOAPBinding 
 26  try: 
 27      from ndg.saml.utils.m2crypto import SSLContextProxy 
 28      _sslContextProxySupport = True 
 29       
 30  except ImportError: 
 31      _sslContextProxySupport = False 
 32   
 33   
34 -class AttributeQueryResponseError(SubjectQueryResponseError):
35 """SAML Response error from Attribute Query"""
36 37
38 -class AttributeQuerySOAPBinding(SubjectQuerySOAPBinding):
39 """SAML Attribute Query SOAP Binding 40 """ 41 QUERY_ATTRIBUTES_ATTRNAME = 'queryAttributes' 42 LEN_QUERY_ATTRIBUTES_ATTRNAME = len(QUERY_ATTRIBUTES_ATTRNAME) 43 QUERY_ATTRIBUTES_PAT = re.compile(',\s*') 44 45 __PRIVATE_ATTR_PREFIX = "__" 46 __slots__ = ('__attributes',) 47 48 SERIALISE_KW = 'serialise' 49 DESERIALISE_KW = 'deserialise' 50 QUERY_TYPE = AttributeQuery 51
52 - def __init__(self, **kw):
53 '''Create SOAP Client for SAML Attribute Query''' 54 55 # Default to ElementTree based serialisation/deserialisation 56 if AttributeQuerySOAPBinding.SERIALISE_KW not in kw: 57 from ndg.saml.xml.etree import AttributeQueryElementTree 58 kw[AttributeQuerySOAPBinding.SERIALISE_KW 59 ] = AttributeQueryElementTree.toXML 60 61 if AttributeQuerySOAPBinding.DESERIALISE_KW not in kw: 62 from ndg.saml.xml.etree import ResponseElementTree 63 kw[AttributeQuerySOAPBinding.DESERIALISE_KW 64 ] = ResponseElementTree.fromXML 65 66 self.__attributes = TypedList(Attribute) 67 68 super(AttributeQuerySOAPBinding, self).__init__(**kw)
69
70 - def addQueryAttributes(self, query):
71 """Adds to a query attributes that are configured for 72 SubjectQuerySOAPBinding. 73 """ 74 super(AttributeQuerySOAPBinding, self).addQueryAttributes(query) 75 # Initialise the query attributes from those preset. 76 query.attributes = TypedList(Attribute) 77 query.attributes.extend(self.queryAttributes)
78
79 - def __setattr__(self, name, value):
80 """Enable setting of SAML query attribute objects via a comma separated 81 string suitable for use reading from an ini file. 82 """ 83 try: 84 super(AttributeQuerySOAPBinding, self).__setattr__(name, value) 85 86 except AttributeError: 87 if name.startswith( 88 AttributeQuerySOAPBinding.QUERY_ATTRIBUTES_ATTRNAME): 89 # Special handler for parsing string format settings 90 if not isinstance(value, basestring): 91 raise TypeError('Expecting string format for special ' 92 '%r attribute; got %r instead' % 93 (name, type(value))) 94 95 pat = AttributeQuerySOAPBinding.QUERY_ATTRIBUTES_PAT 96 attribute = Attribute() 97 98 (attribute.name, 99 attribute.friendlyName, 100 attribute.nameFormat) = pat.split(value) 101 102 self.queryAttributes.append(attribute) 103 else: 104 raise
105
106 - def _getQueryAttributes(self):
107 return self.__attributes
108
109 - def _setQueryAttributes(self, value):
110 if not isinstance(value, TypedList) and value.elementType != Attribute: 111 raise TypeError('Expecting TypedList(Attribute) type for ' 112 '"queryAttributes"; got %r instead' % type(value)) 113 114 # Remove all previously set items and add new ones 115 del self.__attributes[:] 116 for attribute in value: 117 self.__attributes.append(attribute)
118 119 queryAttributes = property(_getQueryAttributes, 120 _setQueryAttributes, 121 doc="List of attributes to query from the " 122 "Attribute Authority") 123 124
125 -class AttributeQuerySslSOAPBinding(AttributeQuerySOAPBinding):
126 """Specialisation of AttributeQuerySOAPbinding taking in the setting of 127 SSL parameters for mutual authentication 128 """ 129 SSL_CONTEXT_PROXY_SUPPORT = _sslContextProxySupport 130 __slots__ = ('__sslCtxProxy',) 131
132 - def __init__(self, **kw):
133 if not AttributeQuerySslSOAPBinding.SSL_CONTEXT_PROXY_SUPPORT: 134 raise ImportError("ndg.saml.utils.m2crypto import " 135 "failed - missing M2Crypto package?") 136 137 # Miss out default HTTPSHandler and set in send() instead 138 if 'handlers' in kw: 139 raise TypeError("__init__() got an unexpected keyword argument " 140 "'handlers'") 141 142 super(AttributeQuerySslSOAPBinding, self).__init__(handlers=(), **kw) 143 self.__sslCtxProxy = SSLContextProxy()
144
145 - def send(self, query, **kw):
146 """Override base class implementation to pass explicit SSL Context 147 """ 148 httpsHandler = HTTPSHandler(ssl_context=self.sslCtxProxy.createCtx()) 149 self.client.openerDirector.add_handler(httpsHandler) 150 return super(AttributeQuerySslSOAPBinding, self).send(query, **kw)
151
152 - def _getSslCtxProxy(self):
153 return self.__sslCtxProxy
154
155 - def _setSslCtxProxy(self, value):
156 if not isinstance(value, SSLContextProxy): 157 raise TypeError('Expecting %r type for "sslCtxProxy attribute; got ' 158 '%r' % type(value)) 159 160 self.__sslCtxProxy = value
161 162 sslCtxProxy = property(fget=_getSslCtxProxy, fset=_setSslCtxProxy, 163 doc="SSL Context Proxy object used for setting up " 164 "an SSL Context for queries") 165
166 - def __setattr__(self, name, value):
167 """Enable setting of SSLContextProxy attributes as if they were 168 attributes of this class. This is intended as a convenience for 169 making settings parameters read from a config file 170 """ 171 try: 172 super(AttributeQuerySslSOAPBinding, self).__setattr__(name, value) 173 174 except AttributeError, e: 175 # Coerce into setting SSL Context Proxy attributes 176 try: 177 setattr(self.sslCtxProxy, name, value) 178 except: 179 raise e
180