Skip to content

Commit 68ab79f

Browse files
author
Roland Hedberg
committed
Editorial
1 parent e66e2cf commit 68ab79f

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

Diff for: src/saml2/validate.py

+52-30
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
XSI_NIL = '{%s}nil' % XSI_NAMESPACE
1111
# ---------------------------------------------------------
1212

13+
1314
class NotValid(Exception):
1415
pass
1516

17+
1618
class OutsideCardinality(Exception):
1719
pass
1820

21+
1922
class MustValueError(ValueError):
2023
pass
2124

25+
2226
class ShouldValueError(ValueError):
2327
pass
2428

@@ -27,6 +31,7 @@ class ShouldValueError(ValueError):
2731

2832
NCNAME = re.compile("(?P<NCName>[a-zA-Z_](\w|[_.-])*)")
2933

34+
3035
def valid_ncname(name):
3136
match = NCNAME.match(name)
3237
if not match:
@@ -45,7 +50,7 @@ def valid_any_uri(item):
4550
except Exception:
4651
raise NotValid("AnyURI")
4752

48-
if part[0] == "urn" and part[1] == "": # A urn
53+
if part[0] == "urn" and part[1] == "": # A urn
4954
return True
5055
# elif part[1] == "localhost" or part[1] == "127.0.0.1":
5156
# raise NotValid("AnyURI")
@@ -83,6 +88,7 @@ def validate_on_or_after(not_on_or_after, slack):
8388
else:
8489
return False
8590

91+
8692
def validate_before(not_before, slack):
8793
if not_before:
8894
now = time_util.utc_now()
@@ -92,11 +98,13 @@ def validate_before(not_before, slack):
9298

9399
return True
94100

101+
95102
def valid_address(address):
96103
if not (valid_ipv4(address) or valid_ipv6(address)):
97104
raise NotValid("address")
98105
return True
99106

107+
100108
def valid_ipv4(address):
101109
parts = address.split(".")
102110
if len(parts) != 4:
@@ -137,24 +145,28 @@ def valid_ipv4(address):
137145
$
138146
""", re.VERBOSE | re.IGNORECASE | re.DOTALL)
139147

148+
140149
def valid_ipv6(address):
141150
"""Validates IPv6 addresses. """
142151
return IPV6_PATTERN.match(address) is not None
143152

153+
144154
def valid_boolean(val):
145155
vall = val.lower()
146156
if vall in ["true", "false", "0", "1"]:
147157
return True
148158
else:
149159
raise NotValid("boolean")
150160

161+
151162
def valid_duration(val):
152163
try:
153164
time_util.parse_duration(val)
154165
except Exception:
155166
raise NotValid("duration")
156167
return True
157168

169+
158170
def valid_string(val):
159171
""" Expects unicode
160172
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
@@ -167,16 +179,17 @@ def valid_string(val):
167179
raise NotValid("string")
168180
if char == 0x09 or char == 0x0A or char == 0x0D:
169181
continue
170-
elif char >= 0x20 and char <= 0xD7FF:
182+
elif 0x20 <= char <= 0xD7FF:
171183
continue
172-
elif char >= 0xE000 and char <= 0xFFFD:
184+
elif 0xE000 <= char <= 0xFFFD:
173185
continue
174-
elif char >= 0x10000 and char <= 0x10FFFF:
186+
elif 0x10000 <= char <= 0x10FFFF:
175187
continue
176188
else:
177189
raise NotValid("string")
178190
return True
179191

192+
180193
def valid_unsigned_short(val):
181194
try:
182195
struct.pack("H", int(val))
@@ -187,6 +200,7 @@ def valid_unsigned_short(val):
187200

188201
return True
189202

203+
190204
def valid_non_negative_integer(val):
191205
try:
192206
integer = int(val)
@@ -197,20 +211,23 @@ def valid_non_negative_integer(val):
197211
raise NotValid("non negative integer")
198212
return True
199213

214+
200215
def valid_integer(val):
201216
try:
202217
int(val)
203218
except ValueError:
204219
raise NotValid("integer")
205220
return True
206221

222+
207223
def valid_base64(val):
208224
try:
209225
base64.b64decode(val)
210226
except Exception:
211227
raise NotValid("base64")
212228
return True
213229

230+
214231
def valid_qname(val):
215232
""" A qname is either
216233
NCName or
@@ -223,6 +240,7 @@ def valid_qname(val):
223240
except ValueError:
224241
return valid_ncname(val)
225242

243+
226244
def valid_anytype(val):
227245
""" Goes through all known type validators
228246
@@ -261,9 +279,11 @@ def valid_anytype(val):
261279

262280
# -----------------------------------------------------------------------------
263281

282+
264283
def validate_value_type(value, spec):
265284
"""
266-
c_value_type = {'base': 'string', 'enumeration': ['Permit', 'Deny', 'Indeterminate']}
285+
c_value_type = {'base': 'string', 'enumeration': ['Permit', 'Deny',
286+
'Indeterminate']}
267287
{'member': 'anyURI', 'base': 'list'}
268288
{'base': 'anyURI'}
269289
{'base': 'NCName'}
@@ -278,14 +298,15 @@ def validate_value_type(value, spec):
278298
raise NotValid("value not in enumeration")
279299
else:
280300
return valid_string(value)
281-
elif spec["base"] == "list": #comma separated list of values
301+
elif spec["base"] == "list": # comma separated list of values
282302
for val in [v.strip() for v in value.split(",")]:
283303
valid(spec["member"], val)
284304
else:
285305
return valid(spec["base"], value)
286306

287307
return True
288308

309+
289310
def valid(typ, value):
290311
try:
291312
return VALIDATOR[typ](value)
@@ -297,33 +318,34 @@ def valid(typ, value):
297318
typ = "string"
298319
return VALIDATOR[typ](value)
299320

321+
300322
def _valid_instance(instance, val):
301323
try:
302324
val.verify()
303325
except NotValid, exc:
304-
raise NotValid("Class '%s' instance: %s" % \
305-
(instance.__class__.__name__,
306-
exc.args[0]))
326+
raise NotValid("Class '%s' instance: %s" % (
327+
instance.__class__.__name__, exc.args[0]))
307328
except OutsideCardinality, exc:
308329
raise NotValid(
309-
"Class '%s' instance cardinality error: %s" % \
310-
(instance.__class__.__name__, exc.args[0]))
330+
"Class '%s' instance cardinality error: %s" % (
331+
instance.__class__.__name__, exc.args[0]))
311332

312333
ERROR_TEXT = "Wrong type of value '%s' on attribute '%s' expected it to be %s"
313334

335+
314336
def valid_instance(instance):
315337
instclass = instance.__class__
316338
class_name = instclass.__name__
317339

318-
if instance.text:
319-
_has_val = True
320-
else:
321-
_has_val = False
340+
# if instance.text:
341+
# _has_val = True
342+
# else:
343+
# _has_val = False
322344

323345
if instclass.c_value_type and instance.text:
324346
try:
325347
validate_value_type(instance.text.strip(),
326-
instclass.c_value_type)
348+
instclass.c_value_type)
327349
except NotValid, exc:
328350
raise NotValid("Class '%s' instance: %s" % (class_name,
329351
exc.args[0]))
@@ -340,15 +362,14 @@ def valid_instance(instance):
340362
if typ.c_value_type:
341363
spec = typ.c_value_type
342364
else:
343-
spec = {"base": "string"} # doI need a default
365+
spec = {"base": "string"} # do I need a default
344366

345367
validate_value_type(value, spec)
346368
else:
347369
valid(typ, value)
348370
except (NotValid, ValueError), exc:
349371
txt = ERROR_TEXT % (value, name, exc.args[0])
350-
raise NotValid(
351-
"Class '%s' instance: %s" % (class_name, txt))
372+
raise NotValid("Class '%s' instance: %s" % (class_name, txt))
352373

353374
for (name, _spec) in instclass.c_children.values():
354375
value = getattr(instance, name, '')
@@ -367,7 +388,7 @@ def valid_instance(instance):
367388
_cmin = _cmax = _card = None
368389

369390
if value:
370-
_has_val = True
391+
#_has_val = True
371392
if isinstance(value, list):
372393
_list = True
373394
vlen = len(value)
@@ -378,14 +399,14 @@ def valid_instance(instance):
378399
if _card:
379400
if _cmin is not None and _cmin > vlen:
380401
raise NotValid(
381-
"Class '%s' instance cardinality error: %s" % \
382-
(class_name, "less then min (%s<%s)" % (vlen,
383-
_cmin)))
402+
"Class '%s' instance cardinality error: %s" % (
403+
class_name, "less then min (%s<%s)" % (vlen,
404+
_cmin)))
384405
if _cmax is not None and vlen > _cmax:
385406
raise NotValid(
386-
"Class '%s' instance cardinality error: %s" % \
387-
(class_name, "more then max (%s>%s)" % (vlen,
388-
_cmax)))
407+
"Class '%s' instance cardinality error: %s" % (
408+
class_name, "more then max (%s>%s)" % (vlen,
409+
_cmax)))
389410

390411
if _list:
391412
for val in value:
@@ -396,8 +417,8 @@ def valid_instance(instance):
396417
else:
397418
if _cmin:
398419
raise NotValid(
399-
"Class '%s' instance cardinality error: %s" % \
400-
(class_name, "too few values on %s" % name))
420+
"Class '%s' instance cardinality error: %s" % (
421+
class_name, "too few values on %s" % name))
401422

402423
# if not _has_val:
403424
# if class_name != "RequestedAttribute":
@@ -407,9 +428,10 @@ def valid_instance(instance):
407428

408429
return True
409430

431+
410432
def valid_domain_name(dns_name):
411433
m = re.match(
412-
"^[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$",
413-
dns_name, "ix")
434+
"^[a-z0-9]+([-.]{ 1 }[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$",
435+
dns_name, "ix")
414436
if not m:
415437
raise ValueError("Not a proper domain name")

0 commit comments

Comments
 (0)