Skip to content

Commit 982cf42

Browse files
author
Kevin Turner
committed
[project @ [API] extensions.ax.FetchRequest.fromOpenIDRequest, FetchResponse.fromSuccessResponse: return None instead of raising AXError on non-AX messages.]
ax.NotAXMessage: new exception raised by parseExtensionArgs.
1 parent fcf2398 commit 982cf42

2 files changed

Lines changed: 98 additions & 10 deletions

File tree

‎openid/extensions/ax.py‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class AXError(ValueError):
4141
specification"""
4242

4343

44+
class NotAXMessage(AXError):
45+
"""Raised when there is no Attribute Exchange mode in the message."""
46+
47+
def __repr__(self):
48+
return self.__class__.__name__
49+
50+
def __str__(self):
51+
return self.__class__.__name__
52+
53+
4454
class AXMessage(extension.Extension):
4555
"""Abstract class containing common code for attribute exchange messages
4656
@@ -64,12 +74,17 @@ def _checkMode(self, ax_args):
6474
"""Raise an exception if the mode in the attribute exchange
6575
arguments does not match what is expected for this class.
6676
67-
@raises ValueError: When the mode does not match
77+
@raises NotAXMessage: When there is no mode value in ax_args at all.
78+
79+
@raises AXError: When mode does not match.
6880
"""
6981
mode = ax_args.get('mode')
7082
if mode != self.mode:
71-
raise AXError(
72-
'Expected mode %r; got %r' % (self.mode, mode))
83+
if not mode:
84+
raise NotAXMessage()
85+
else:
86+
raise AXError(
87+
'Expected mode %r; got %r' % (self.mode, mode))
7388

7489
def _newArgs(self):
7590
"""Return a set of attribute exchange arguments containing the
@@ -274,8 +289,9 @@ def fromOpenIDRequest(cls, openid_request):
274289
containing the attribute fetch request
275290
@type openid_request: C{L{openid.server.server.CheckIDRequest}}
276291
277-
@rtype: C{L{FetchRequest}}
278-
@returns: The FetchRequest extracted from the message
292+
@rtype: C{L{FetchRequest}} or C{None}
293+
@returns: The FetchRequest extracted from the message or None, if
294+
the message contained no AX extension.
279295
280296
@raises KeyError: if the AuthRequest is not consistent in its use
281297
of namespace aliases.
@@ -287,7 +303,10 @@ def fromOpenIDRequest(cls, openid_request):
287303
message = openid_request.message
288304
ax_args = message.getArgs(cls.ns_uri)
289305
self = cls()
290-
self.parseExtensionArgs(ax_args)
306+
try:
307+
self.parseExtensionArgs(ax_args)
308+
except NotAXMessage, err:
309+
return None
291310

292311
if self.update_url:
293312
# Update URL must match the openid.realm of the underlying
@@ -318,6 +337,9 @@ def parseExtensionArgs(self, ax_args):
318337
@raises KeyError: if the message is not consistent in its use
319338
of namespace aliases.
320339
340+
@raises NotAXMessage: If ax_args does not include an Attribute Exchange
341+
mode.
342+
321343
@raises AXError: If the data to be parsed does not follow the
322344
attribute exchange specification. At least when
323345
'if_available' or 'required' is not specified for a
@@ -678,13 +700,20 @@ def fromSuccessResponse(cls, success_response, signed=True):
678700
@type signed: bool
679701
680702
@returns: A FetchResponse containing the data from the OpenID
681-
message
703+
message, or None if the SuccessResponse did not contain AX
704+
extension data.
705+
706+
@raises AXError: when the AX data cannot be parsed.
682707
"""
683708
self = cls()
684709
ax_args = success_response.extensionResponse(self.ns_uri, signed)
685710

686-
self.parseExtensionArgs(ax_args)
687-
return self
711+
try:
712+
self.parseExtensionArgs(ax_args)
713+
except NotAXMessage, err:
714+
return None
715+
else:
716+
return self
688717

689718
fromSuccessResponse = classmethod(fromSuccessResponse)
690719

‎openid/test/test_ax.py‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
from openid.extensions import ax
66
from openid.message import NamespaceMap, Message, OPENID2_NS
7+
from openid.consumer.consumer import SuccessResponse
78

89
class BogusAXMessage(ax.AXMessage):
910
mode = 'bogus'
@@ -20,7 +21,7 @@ def setUp(self):
2021

2122
def test_checkMode(self):
2223
check = self.bax._checkMode
23-
self.failUnlessRaises(ax.AXError, check, {})
24+
self.failUnlessRaises(ax.NotAXMessage, check, {})
2425
self.failUnlessRaises(ax.AXError, check, {'mode':'fetch_request'})
2526

2627
# does not raise an exception when the mode is right
@@ -398,6 +399,31 @@ def test_openidUpdateURLVerificationSuccessReturnTo(self):
398399

399400
fr = ax.FetchRequest.fromOpenIDRequest(DummyRequest(openid_req_msg))
400401

402+
def test_fromOpenIDRequestWithoutExtension(self):
403+
"""return None for an OpenIDRequest without AX paramaters."""
404+
openid_req_msg = Message.fromOpenIDArgs({
405+
'mode': 'checkid_setup',
406+
'ns': OPENID2_NS,
407+
})
408+
oreq = DummyRequest(openid_req_msg)
409+
r = ax.FetchRequest.fromOpenIDRequest(oreq)
410+
self.failUnless(r is None, "%s is not None" % (r,))
411+
412+
def test_fromOpenIDRequestWithoutData(self):
413+
"""return something for SuccessResponse with AX paramaters,
414+
even if it is the empty set."""
415+
openid_req_msg = Message.fromOpenIDArgs({
416+
'mode': 'checkid_setup',
417+
'realm': 'http://example.com/realm',
418+
'ns': OPENID2_NS,
419+
'ns.ax': ax.AXMessage.ns_uri,
420+
'ax.mode': 'fetch_request',
421+
})
422+
oreq = DummyRequest(openid_req_msg)
423+
r = ax.FetchRequest.fromOpenIDRequest(oreq)
424+
self.failUnless(r is not None)
425+
426+
401427
class FetchResponseTest(unittest.TestCase):
402428
def setUp(self):
403429
self.msg = ax.FetchResponse()
@@ -487,6 +513,39 @@ def test_getSingle_extra(self):
487513
def test_get(self):
488514
self.failUnlessRaises(KeyError, self.msg.get, self.type_a)
489515

516+
def test_fromSuccessResponseWithoutExtension(self):
517+
"""return None for SuccessResponse with no AX paramaters."""
518+
args = {
519+
'mode': 'id_res',
520+
'ns': OPENID2_NS,
521+
}
522+
sf = ['openid.' + i for i in args.keys()]
523+
msg = Message.fromOpenIDArgs(args)
524+
class Endpoint:
525+
claimed_id = 'http://invalid.'
526+
527+
oreq = SuccessResponse(Endpoint(), msg, signed_fields=sf)
528+
r = ax.FetchResponse.fromSuccessResponse(oreq)
529+
self.failUnless(r is None, "%s is not None" % (r,))
530+
531+
def test_fromSuccessResponseWithoutData(self):
532+
"""return something for SuccessResponse with AX paramaters,
533+
even if it is the empty set."""
534+
args = {
535+
'mode': 'id_res',
536+
'ns': OPENID2_NS,
537+
'ns.ax': ax.AXMessage.ns_uri,
538+
'ax.mode': 'fetch_response',
539+
}
540+
sf = ['openid.' + i for i in args.keys()]
541+
msg = Message.fromOpenIDArgs(args)
542+
class Endpoint:
543+
claimed_id = 'http://invalid.'
544+
545+
oreq = SuccessResponse(Endpoint(), msg, signed_fields=sf)
546+
r = ax.FetchResponse.fromSuccessResponse(oreq)
547+
self.failUnless(r is not None)
548+
490549

491550
class StoreRequestTest(unittest.TestCase):
492551
def setUp(self):

0 commit comments

Comments
 (0)