Skip to content

Commit 3b1755d

Browse files
author
tailor
committed
[project @ Consumer._verifyDiscoveryResults: fall back to OpenID 1.0 type if 1.1 endpoint cannot be found]
1 parent 8bad9e2 commit 3b1755d

3 files changed

Lines changed: 69 additions & 34 deletions

File tree

‎openid/consumer/consumer.py‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -921,18 +921,20 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
921921
# request.
922922
if not endpoint:
923923
oidutil.log('No pre-discovered information supplied.')
924-
endpoint = self._discoverAndVerify(to_match)
924+
endpoint = self._discoverAndVerify(to_match.claimed_id, [to_match])
925925
else:
926926
# The claimed ID matches, so we use the endpoint that we
927927
# discovered in initiation. This should be the most common
928928
# case.
929929
try:
930930
self._verifyDiscoverySingle(endpoint, to_match)
931931
except ProtocolError, e:
932-
oidutil.log("Error attempting to use stored discovery information: " +
933-
str(e))
932+
oidutil.log(
933+
"Error attempting to use stored discovery information: " +
934+
str(e))
934935
oidutil.log("Attempting discovery to verify endpoint")
935-
endpoint = self._discoverAndVerify(to_match)
936+
endpoint = self._discoverAndVerify(
937+
to_match.claimed_id, [to_match])
936938

937939
# The endpoint we return should have the claimed ID from the
938940
# message we just verified, fragment and all.
@@ -979,10 +981,7 @@ def _verifyDiscoveryResultsOpenID1(self, resp_msg, endpoint):
979981
return endpoint
980982

981983
# Endpoint is either bad (failed verification) or None
982-
try:
983-
return self._discoverAndVerify(to_match)
984-
except TypeURIMismatch:
985-
return self._discoverAndVerify(to_match_1_0)
984+
return self._discoverAndVerify(claimed_id, [to_match, to_match_1_0])
986985

987986
def _verifyDiscoverySingle(self, endpoint, to_match):
988987
"""Verify that the given endpoint matches the information
@@ -1032,7 +1031,7 @@ def _verifyDiscoverySingle(self, endpoint, to_match):
10321031
raise ProtocolError('OP Endpoint mismatch. Expected %s, got %s' %
10331032
(to_match.server_url, endpoint.server_url))
10341033

1035-
def _discoverAndVerify(self, to_match):
1034+
def _discoverAndVerify(self, claimed_id, to_match_endpoints):
10361035
"""Given an endpoint object created from the information in an
10371036
OpenID response, perform discovery and verify the discovery
10381037
results, returning the matching endpoint that is the result of
@@ -1047,38 +1046,41 @@ def _discoverAndVerify(self, to_match):
10471046
10481047
@raises DiscoveryFailure: when discovery fails.
10491048
"""
1050-
oidutil.log('Performing discovery on %s' % (to_match.claimed_id,))
1051-
_, services = self._discover(to_match.claimed_id)
1049+
oidutil.log('Performing discovery on %s' % (claimed_id,))
1050+
_, services = self._discover(claimed_id)
10521051
if not services:
10531052
raise DiscoveryFailure('No OpenID information found at %s' %
1054-
(to_match.claimed_id,), None)
1055-
return self._verifyDiscoveredServices(services, to_match)
1053+
(claimed_id,), None)
1054+
return self._verifyDiscoveredServices(claimed_id, services,
1055+
to_match_endpoints)
10561056

10571057

1058-
def _verifyDiscoveredServices(self, services, to_match):
1058+
def _verifyDiscoveredServices(self, claimed_id, services, to_match_endpoints):
10591059
"""See @L{_discoverAndVerify}"""
10601060

10611061
# Search the services resulting from discovery to find one
10621062
# that matches the information from the assertion
10631063
failure_messages = []
10641064
for endpoint in services:
1065-
try:
1066-
self._verifyDiscoverySingle(endpoint, to_match)
1067-
except ProtocolError, why:
1068-
failure_messages.append(str(why))
1069-
else:
1070-
# It matches, so discover verification has
1071-
# succeeded. Return this endpoint.
1072-
return endpoint
1065+
for to_match_endpoint in to_match_endpoints:
1066+
try:
1067+
self._verifyDiscoverySingle(
1068+
endpoint, to_match_endpoint)
1069+
except ProtocolError, why:
1070+
failure_messages.append(str(why))
1071+
else:
1072+
# It matches, so discover verification has
1073+
# succeeded. Return this endpoint.
1074+
return endpoint
10731075
else:
10741076
oidutil.log('Discovery verification failure for %s' %
1075-
(to_match.claimed_id,))
1077+
(claimed_id,))
10761078
for failure_message in failure_messages:
10771079
oidutil.log(' * Endpoint mismatch: ' + failure_message)
10781080

10791081
raise DiscoveryFailure(
10801082
'No matching endpoint found after discovering %s'
1081-
% (to_match.claimed_id,), None)
1083+
% (claimed_id,), None)
10821084

10831085
def _checkAuth(self, message, server_url):
10841086
"""Make a check_authentication request to verify this message.

‎openid/test/test_consumer.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def test_idResMissingField(self):
467467
def test_idResURLMismatch(self):
468468
class VerifiedError(Exception): pass
469469

470-
def discoverAndVerify(_to_match):
470+
def discoverAndVerify(claimed_id, _to_match_endpoints):
471471
raise VerifiedError
472472

473473
self.consumer._discoverAndVerify = discoverAndVerify
@@ -1707,8 +1707,10 @@ def test_theGoodStuff(self):
17071707
def test_otherServer(self):
17081708
text = "verify failed"
17091709

1710-
def discoverAndVerify(to_match):
1711-
self.failUnlessEqual(self.identifier, to_match.claimed_id)
1710+
def discoverAndVerify(claimed_id, to_match_endpoints):
1711+
self.failUnlessEqual(claimed_id, self.identifier)
1712+
for to_match in to_match_endpoints:
1713+
self.failUnlessEqual(claimed_id, to_match.claimed_id)
17121714
raise ProtocolError(text)
17131715

17141716
self.consumer._discoverAndVerify = discoverAndVerify
@@ -1732,8 +1734,10 @@ def discoverAndVerify(to_match):
17321734
def test_foreignDelegate(self):
17331735
text = "verify failed"
17341736

1735-
def discoverAndVerify(to_match):
1736-
self.failUnlessEqual(self.identifier, to_match.claimed_id)
1737+
def discoverAndVerify(claimed_id, to_match_endpoints):
1738+
self.failUnlessEqual(claimed_id, self.identifier)
1739+
for to_match in to_match_endpoints:
1740+
self.failUnlessEqual(claimed_id, to_match.claimed_id)
17371741
raise ProtocolError(text)
17381742

17391743
self.consumer._discoverAndVerify = discoverAndVerify
@@ -1951,7 +1955,9 @@ def dummyDiscover(unused_identifier):
19511955
def failUnlessDiscoveryFailure(self):
19521956
self.failUnlessRaises(
19531957
DiscoveryFailure,
1954-
self.consumer._discoverAndVerify, self.to_match)
1958+
self.consumer._discoverAndVerify,
1959+
'http://claimed-id.com/',
1960+
[self.to_match])
19551961

19561962
def test_noServices(self):
19571963
"""Discovery returning no results results in a
@@ -1983,7 +1989,8 @@ def returnTrue(unused1, unused2):
19831989

19841990
# Since _verifyDiscoverySingle returns True, we should get the
19851991
# first endpoint that we passed in as a result.
1986-
result = self.consumer._discoverAndVerify(self.to_match)
1992+
result = self.consumer._discoverAndVerify(
1993+
'http://claimed.id/', [self.to_match])
19871994
self.failUnlessEqual(matching_endpoint, result)
19881995

19891996
from openid.extension import Extension

‎openid/test/test_verifydisco.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ def test_openid2UsePreDiscoveredWrongType(self):
133133
endpoint.server_url = 'Phone Home'
134134
endpoint.type_uris = [discover.OPENID_1_1_TYPE]
135135

136-
def discoverAndVerify(to_match):
137-
self.failUnlessEqual(endpoint.claimed_id, to_match.claimed_id)
136+
def discoverAndVerify(claimed_id, to_match_endpoints):
137+
self.failUnlessEqual(claimed_id, endpoint.claimed_id)
138+
for to_match in to_match_endpoints:
139+
self.failUnlessEqual(claimed_id, to_match.claimed_id)
138140
raise consumer.ProtocolError(text)
139141

140142
self.consumer._discoverAndVerify = discoverAndVerify
@@ -173,7 +175,7 @@ def test_openid1UsePreDiscovered(self):
173175
def test_openid1UsePreDiscoveredWrongType(self):
174176
class VerifiedError(Exception): pass
175177

176-
def discoverAndVerify(_to_match):
178+
def discoverAndVerify(claimed_id, _to_match):
177179
raise VerifiedError
178180

179181
self.consumer._discoverAndVerify = discoverAndVerify
@@ -219,6 +221,30 @@ def test_openid2Fragment(self):
219221

220222
self.failUnlessLogEmpty()
221223

224+
def test_openid1Fallback1_0(self):
225+
claimed_id = 'http://claimed.id/'
226+
endpoint = None
227+
resp_mesg = message.Message.fromOpenIDArgs({
228+
'ns': message.OPENID1_NS,
229+
'identity': claimed_id})
230+
# Pass the OpenID 1 claimed_id this way since we're passing
231+
# None for the endpoint.
232+
resp_mesg.setArg(message.BARE_NS, 'openid1_claimed_id', claimed_id)
233+
234+
# We expect the OpenID 1 discovery verification to try
235+
# matching the discovered endpoint against the 1.1 type and
236+
# fall back to 1.0.
237+
expected_endpoint = discover.OpenIDServiceEndpoint()
238+
expected_endpoint.type_uris = [discover.OPENID_1_0_TYPE]
239+
expected_endpoint.local_id = None
240+
expected_endpoint.claimed_id = claimed_id
241+
242+
discovered_services = [expected_endpoint]
243+
self.consumer._discover = lambda *args: ('unused', discovered_services)
244+
245+
actual_endpoint = self.consumer._verifyDiscoveryResults(
246+
resp_mesg, endpoint)
247+
self.failUnless(actual_endpoint is expected_endpoint)
222248

223249
# XXX: test the implementation of _discoverAndVerify
224250

0 commit comments

Comments
 (0)