23
23
from saml2 import saml , extension_elements_to_elements , SAMLError
24
24
from saml2 .saml import NAME_FORMAT_URI , NAME_FORMAT_UNSPECIFIED
25
25
26
+ import logging
27
+ logger = logging .getLogger (__name__ )
28
+
26
29
27
30
class UnknownNameFormat (SAMLError ):
28
31
pass
@@ -98,26 +101,30 @@ def ac_factory_II(path):
98
101
return ac_factory (path )
99
102
100
103
101
- def ava_fro (acs , statement ):
102
- """ Translates attributes according to their name_formats into the local
103
- names.
104
-
105
- :param acs: AttributeConverter instances
106
- :param statement: A SAML statement
107
- :return: A dictionary with attribute names replaced with local names.
108
- """
109
- if not statement :
110
- return {}
111
-
112
- acsdic = dict ([(ac .name_format , ac ) for ac in acs ])
113
- acsdic [None ] = acsdic [NAME_FORMAT_URI ]
114
- return dict ([acsdic [a .name_format ].ava_from (a ) for a in statement ])
104
+ # def ava_fro(acs, statement):
105
+ # """ Translates attributes according to their name_formats into the local
106
+ # names.
107
+ #
108
+ # :param acs: AttributeConverter instances
109
+ # :param statement: A SAML statement
110
+ # :return: A dictionary with attribute names replaced with local names.
111
+ # """
112
+ # if not statement:
113
+ # return {}
114
+ #
115
+ # acsdic = dict([(ac.name_format, ac) for ac in acs])
116
+ # acsdic[None] = acsdic[NAME_FORMAT_URI]
117
+ # return dict([acsdic[a.name_format].ava_from(a) for a in statement])
115
118
116
119
117
- def to_local (acs , statement ):
120
+ def to_local (acs , statement , allow_unknown_attributes = False ):
118
121
""" Replaces the attribute names in a attribute value assertion with the
119
122
equivalent name from a local name format.
120
123
124
+ :param acs: List of Attribute Converters
125
+ :param statement: The Attribute Statement
126
+ :param allow_unknown_attributes: If unknown attributes are allowed
127
+ :return: A key,values dictionary
121
128
"""
122
129
if not acs :
123
130
acs = [AttributeConverter ()]
@@ -128,9 +135,26 @@ def to_local(acs, statement):
128
135
ava = {}
129
136
for attr in statement .attribute :
130
137
try :
131
- key , val = acsd [attr .name_format ].ava_from ( attr )
138
+ _func = acsd [attr .name_format ].ava_from
132
139
except KeyError :
133
- key , val = acs [0 ].lcd_ava_from (attr )
140
+ if attr .name_format == NAME_FORMAT_UNSPECIFIED or \
141
+ allow_unknown_attributes :
142
+ _func = acs [0 ].lcd_ava_from
143
+ else :
144
+ logger .info ("Unsupported attribute name format: %s" % (
145
+ attr .name_format ,))
146
+ continue
147
+
148
+ try :
149
+ key , val = _func (attr )
150
+ except KeyError :
151
+ if allow_unknown_attributes :
152
+ key , val = acs [0 ].lcd_ava_from (attr )
153
+ else :
154
+ logger .info ("Unknown attribute name: %s" % (attr ,))
155
+ continue
156
+ except AttributeError :
157
+ continue
134
158
135
159
try :
136
160
ava [key ].extend (val )
@@ -245,7 +269,7 @@ def lcd_ava_from(self, attribute):
245
269
"""
246
270
In nothing else works, this should
247
271
248
- :param attribute:
272
+ :param attribute: An Attribute Instance
249
273
:return:
250
274
"""
251
275
try :
@@ -287,14 +311,19 @@ def fail_safe_fro(self, statement):
287
311
result [name ].append (value .text .strip ())
288
312
return result
289
313
290
- def ava_from (self , attribute ):
314
+ def ava_from (self , attribute , allow_unknown = False ):
291
315
try :
292
316
attr = self ._fro [attribute .name .strip ().lower ()]
293
- except (AttributeError , KeyError ):
294
- try :
295
- attr = attribute .friendly_name .strip ().lower ()
296
- except AttributeError :
297
- attr = attribute .name .strip ().lower ()
317
+ except AttributeError :
318
+ attr = attribute .friendly_name .strip ().lower ()
319
+ except KeyError :
320
+ if allow_unknown :
321
+ try :
322
+ attr = attribute .name .strip ().lower ()
323
+ except AttributeError :
324
+ attr = attribute .friendly_name .strip ().lower ()
325
+ else :
326
+ raise
298
327
299
328
val = []
300
329
for value in attribute .attribute_value :
@@ -333,8 +362,12 @@ def fro(self, statement):
333
362
attribute .name_format != self .name_format :
334
363
continue
335
364
336
- (key , val ) = self .ava_from (attribute )
337
- result [key ] = val
365
+ try :
366
+ (key , val ) = self .ava_from (attribute )
367
+ except (KeyError , AttributeError ):
368
+ pass
369
+ else :
370
+ result [key ] = val
338
371
339
372
return result
340
373
0 commit comments