Skip to content

Commit eed2af5

Browse files
author
Kevin Turner
committed
[project @ message: raise an InvalidOpenIDNamespace error instead of a ValueError]
1 parent e2260c0 commit eed2af5

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎openid/message.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class UndefinedOpenIDNamespace(ValueError):
5959
"""Raised if the generic OpenID namespace is accessed when there
6060
is no OpenID namespace set for this message."""
6161

62+
class InvalidOpenIDNamespace(ValueError):
63+
"""Raised if openid.ns is not a recognized value.
64+
65+
For recognized values, see L{Message.allowed_openid_namespaces}
66+
"""
67+
6268
# Sentinel used for Message implementation to indicate that getArg
6369
# should raise an exception instead of returning a default.
6470
no_default = object()
@@ -113,7 +119,11 @@ class Message(object):
113119
allowed_openid_namespaces = [OPENID1_NS, OPENID2_NS]
114120

115121
def __init__(self, openid_namespace=None):
116-
"""Create an empty Message"""
122+
"""Create an empty Message.
123+
124+
@raises InvalidOpenIDNamespace: if openid_namespace is not in
125+
L{Message.allowed_openid_namespaces}
126+
"""
117127
self.args = {}
118128
self.namespaces = NamespaceMap()
119129
if openid_namespace is None:
@@ -122,7 +132,9 @@ def __init__(self, openid_namespace=None):
122132
self.setOpenIDNamespace(openid_namespace)
123133

124134
def fromPostArgs(cls, args):
125-
"""Construct a Message containing a set of POST arguments"""
135+
"""Construct a Message containing a set of POST arguments.
136+
137+
"""
126138
self = cls()
127139

128140
# Partition into "openid." args and bare args
@@ -150,7 +162,11 @@ def fromPostArgs(cls, args):
150162
fromPostArgs = classmethod(fromPostArgs)
151163

152164
def fromOpenIDArgs(cls, openid_args):
153-
"""Construct a Message from a parsed KVForm message"""
165+
"""Construct a Message from a parsed KVForm message.
166+
167+
@raises InvalidOpenIDNamespace: if openid.ns is not in
168+
L{Message.allowed_openid_namespaces}
169+
"""
154170
self = cls()
155171
self._fromOpenIDArgs(openid_args)
156172
return self
@@ -203,11 +219,16 @@ def _fromOpenIDArgs(self, openid_args):
203219
self.setArg(ns_uri, ns_key, value)
204220

205221
def setOpenIDNamespace(self, openid_ns_uri):
222+
"""Set the OpenID namespace URI used in this message.
223+
224+
@raises InvalidOpenIDNamespace: if the namespace is not in
225+
L{Message.allowed_openid_namespaces}
226+
"""
206227
if openid_ns_uri == THE_OTHER_OPENID1_NS:
207228
# It's better this way, really.
208229
openid_ns_uri = OPENID1_NS
209230
if openid_ns_uri not in self.allowed_openid_namespaces:
210-
raise ValueError('Invalid null namespace: %r' % (openid_ns_uri,))
231+
raise InvalidOpenIDNamespace(openid_ns_uri)
211232

212233
self.namespaces.addAlias(openid_ns_uri, NULL_NAMESPACE)
213234
self._openid_ns_uri = openid_ns_uri

‎openid/server/server.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
from openid.store.nonce import mkNonce
126126
from openid.server.trustroot import TrustRoot, verifyReturnTo
127127
from openid.association import Association, default_negotiator, getSecretSize
128-
from openid.message import Message, OPENID_NS, OPENID1_NS, \
128+
from openid.message import Message, InvalidOpenIDNamespace, \
129+
OPENID_NS, OPENID1_NS, \
129130
OPENID2_NS, IDENTIFIER_SELECT, OPENID1_URL_LIMIT
130131

131132
HTTP_OK = 200
@@ -1368,7 +1369,10 @@ def decode(self, query):
13681369
if not query:
13691370
return None
13701371

1371-
message = Message.fromPostArgs(query)
1372+
try:
1373+
message = Message.fromPostArgs(query)
1374+
except InvalidOpenIDNamespace, err:
1375+
raise ProtocolError(None, str(err))
13721376

13731377
mode = message.getArg(OPENID_NS, 'mode')
13741378
if not mode:

0 commit comments

Comments
 (0)