Skip to content

Commit 6c377fb

Browse files
author
Kevin Turner
committed
[project @ consumer.consumer: OpenID 2 draft 12 fragment handling]
1 parent e3fb4c2 commit 6c377fb

2 files changed

Lines changed: 55 additions & 11 deletions

File tree

‎openid/consumer/consumer.py‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190

191191
import cgi
192192
import copy
193-
from urlparse import urlparse
193+
from urlparse import urlparse, urldefrag
194194

195195
from openid import fetchers
196196

@@ -900,26 +900,36 @@ def _verifyDiscoveryResultsOpenID2(self, resp_msg, endpoint):
900900
elif to_match.claimed_id is None:
901901
return OpenIDServiceEndpoint.fromOPEndpointURL(to_match.server_url)
902902

903+
# Fragments do not influence discovery, so we can't compare a
904+
# claimed identifier with a fragment to discovered information.
905+
defragged_claimed_id, _ = urldefrag(to_match.claimed_id)
906+
903907
# The claimed ID doesn't match, so we have to do discovery
904908
# again. This covers not using sessions, OP identifier
905909
# endpoints and responses that didn't match the original
906910
# request.
907-
elif not endpoint:
911+
if not endpoint:
908912
oidutil.log('No pre-discovered information supplied.')
909-
return self._discoverAndVerify(to_match)
913+
endpoint = self._discoverAndVerify(to_match)
910914

911-
elif to_match.claimed_id != endpoint.claimed_id:
915+
elif defragged_claimed_id != endpoint.claimed_id:
912916
oidutil.log('Mismatched pre-discovered session data. '
913917
'Claimed ID in session=%s, in assertion=%s' %
914-
(endpoint.claimed_id, to_match.claimed_id))
915-
return self._discoverAndVerify(to_match)
918+
(endpoint.claimed_id, defragged_claimed_id))
919+
endpoint = self._discoverAndVerify(to_match)
916920

917921
# The claimed ID matches, so we use the endpoint that we
918922
# discovered in initiation. This should be the most common
919923
# case.
920924
else:
921925
self._verifyDiscoverySingle(endpoint, to_match)
922-
return endpoint
926+
927+
# The endpoint we return should have the claimed ID from the
928+
# message we just verified, fragment and all.
929+
if endpoint.claimed_id != to_match.claimed_id:
930+
endpoint = copy.copy(endpoint)
931+
endpoint.claimed_id = to_match.claimed_id
932+
return endpoint
923933

924934
def _verifyDiscoveryResultsOpenID1(self, resp_msg, endpoint):
925935
if endpoint is None:
@@ -967,11 +977,14 @@ def _verifyDiscoverySingle(self, endpoint, to_match):
967977
if not endpoint.usesExtension(type_uri):
968978
raise TypeURIMismatch(type_uri, endpoint)
969979

970-
if to_match.claimed_id != endpoint.claimed_id:
980+
# Fragments do not influence discovery, so we can't compare a
981+
# claimed identifier with a fragment to discovered information.
982+
defragged_claimed_id, _ = urldefrag(to_match.claimed_id)
983+
if defragged_claimed_id != endpoint.claimed_id:
971984
raise ProtocolError(
972985
'Claimed ID does not match (different subjects!), '
973986
'Expected %s, got %s' %
974-
(to_match.claimed_id, endpoint.claimed_id))
987+
(defragged_claimed_id, endpoint.claimed_id))
975988

976989
if to_match.getLocalID() != endpoint.getLocalID():
977990
raise ProtocolError('local_id mismatch. Expected %s, got %s' %

‎openid/test/test_verifydisco.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def setUp(self):
1818
CatchLogs.setUp(self)
1919
TestIdRes.setUp(self)
2020

21+
def tearDown(self):
22+
CatchLogs.tearDown(self)
23+
TestIdRes.tearDown(self)
24+
2125
def failUnlessProtocolError(self, prefix, callable, *args, **kwargs):
2226
try:
2327
result = callable(*args, **kwargs)
@@ -81,7 +85,8 @@ def test_openID2NoIdentifiers(self):
8185

8286
def test_openID2NoEndpointDoesDisco(self):
8387
op_endpoint = 'Phone Home'
84-
sentinel = object()
88+
sentinel = discover.OpenIDServiceEndpoint()
89+
sentinel.claimed_id = 'monkeysoft'
8590
self.consumer._discoverAndVerify = const(sentinel)
8691
msg = message.Message.fromOpenIDArgs(
8792
{'ns':message.OPENID2_NS,
@@ -98,7 +103,8 @@ def test_openID2MismatchedDoesDisco(self):
98103
mismatched.local_id = 'green cheese'
99104

100105
op_endpoint = 'Phone Home'
101-
sentinel = object()
106+
sentinel = discover.OpenIDServiceEndpoint()
107+
sentinel.claimed_id = 'monkeysoft'
102108
self.consumer._discoverAndVerify = const(sentinel)
103109
msg = message.Message.fromOpenIDArgs(
104110
{'ns':message.OPENID2_NS,
@@ -171,6 +177,31 @@ def test_openid1UsePreDiscoveredWrongType(self):
171177
self.consumer._verifyDiscoveryResults, msg, endpoint)
172178
self.failUnlessLogEmpty()
173179

180+
def test_openid2Fragment(self):
181+
claimed_id = "http://unittest.invalid/"
182+
claimed_id_frag = claimed_id + "#fragment"
183+
endpoint = discover.OpenIDServiceEndpoint()
184+
endpoint.local_id = 'my identity'
185+
endpoint.claimed_id = claimed_id
186+
endpoint.server_url = 'Phone Home'
187+
endpoint.type_uris = [discover.OPENID_2_0_TYPE]
188+
189+
msg = message.Message.fromOpenIDArgs(
190+
{'ns':message.OPENID2_NS,
191+
'identity':endpoint.local_id,
192+
'claimed_id': claimed_id_frag,
193+
'op_endpoint': endpoint.server_url})
194+
result = self.consumer._verifyDiscoveryResults(msg, endpoint)
195+
196+
self.failUnlessEqual(result.local_id, endpoint.local_id)
197+
self.failUnlessEqual(result.server_url, endpoint.server_url)
198+
self.failUnlessEqual(result.type_uris, endpoint.type_uris)
199+
200+
self.failUnlessEqual(result.claimed_id, claimed_id_frag)
201+
202+
self.failUnlessLogEmpty()
203+
204+
174205
# XXX: test the implementation of _discoverAndVerify
175206

176207

0 commit comments

Comments
 (0)