ax.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. # -*- test-case-name: openid.test.test_ax -*-
  2. """Implements the OpenID Attribute Exchange specification, version 1.0.
  3. @since: 2.1.0
  4. """
  5. __all__ = [
  6. 'AttributeRequest',
  7. 'FetchRequest',
  8. 'FetchResponse',
  9. 'StoreRequest',
  10. 'StoreResponse',
  11. ]
  12. from openid import extension
  13. from openid.server.trustroot import TrustRoot
  14. from openid.message import NamespaceMap, OPENID_NS
  15. # Use this as the 'count' value for an attribute in a FetchRequest to
  16. # ask for as many values as the OP can provide.
  17. UNLIMITED_VALUES = "unlimited"
  18. # Minimum supported alias length in characters. Here for
  19. # completeness.
  20. MINIMUM_SUPPORTED_ALIAS_LENGTH = 32
  21. def checkAlias(alias):
  22. """
  23. Check an alias for invalid characters; raise AXError if any are
  24. found. Return None if the alias is valid.
  25. """
  26. if ',' in alias:
  27. raise AXError("Alias %r must not contain comma" % (alias, ))
  28. if '.' in alias:
  29. raise AXError("Alias %r must not contain period" % (alias, ))
  30. class AXError(ValueError):
  31. """Results from data that does not meet the attribute exchange 1.0
  32. specification"""
  33. class NotAXMessage(AXError):
  34. """Raised when there is no Attribute Exchange mode in the message."""
  35. def __repr__(self):
  36. return self.__class__.__name__
  37. def __str__(self):
  38. return self.__class__.__name__
  39. class AXMessage(extension.Extension):
  40. """Abstract class containing common code for attribute exchange messages
  41. @cvar ns_alias: The preferred namespace alias for attribute
  42. exchange messages
  43. @cvar mode: The type of this attribute exchange message. This must
  44. be overridden in subclasses.
  45. """
  46. # This class is abstract, so it's OK that it doesn't override the
  47. # abstract method in Extension:
  48. #
  49. #pylint:disable-msg=W0223
  50. ns_alias = 'ax'
  51. ns_uri = 'http://openid.net/srv/ax/1.0'
  52. mode = None # NOTE mode is only ever set to a str value, see below
  53. def _checkMode(self, ax_args):
  54. """Raise an exception if the mode in the attribute exchange
  55. arguments does not match what is expected for this class.
  56. @raises NotAXMessage: When there is no mode value in ax_args at all.
  57. @raises AXError: When mode does not match.
  58. """
  59. mode = ax_args.get('mode')
  60. if isinstance(mode, bytes):
  61. mode = str(mode, encoding="utf-8")
  62. if mode != self.mode:
  63. if not mode:
  64. raise NotAXMessage()
  65. else:
  66. raise AXError('Expected mode %r; got %r' % (self.mode, mode))
  67. def _newArgs(self):
  68. """Return a set of attribute exchange arguments containing the
  69. basic information that must be in every attribute exchange
  70. message.
  71. """
  72. return {'mode': self.mode}
  73. class AttrInfo(object):
  74. """Represents a single attribute in an attribute exchange
  75. request. This should be added to an AXRequest object in order to
  76. request the attribute.
  77. @ivar required: Whether the attribute will be marked as required
  78. when presented to the subject of the attribute exchange
  79. request.
  80. @type required: bool
  81. @ivar count: How many values of this type to request from the
  82. subject. Defaults to one.
  83. @type count: int
  84. @ivar type_uri: The identifier that determines what the attribute
  85. represents and how it is serialized. For example, one type URI
  86. representing dates could represent a Unix timestamp in base 10
  87. and another could represent a human-readable string.
  88. @type type_uri: str
  89. @ivar alias: The name that should be given to this alias in the
  90. request. If it is not supplied, a generic name will be
  91. assigned. For example, if you want to call a Unix timestamp
  92. value 'tstamp', set its alias to that value. If two attributes
  93. in the same message request to use the same alias, the request
  94. will fail to be generated.
  95. @type alias: str or NoneType
  96. """
  97. # It's OK that this class doesn't have public methods (it's just a
  98. # holder for a bunch of attributes):
  99. #
  100. #pylint:disable-msg=R0903
  101. def __init__(self, type_uri, count=1, required=False, alias=None):
  102. self.required = required
  103. self.count = count
  104. self.type_uri = type_uri
  105. self.alias = alias
  106. if self.alias is not None:
  107. checkAlias(self.alias)
  108. def wantsUnlimitedValues(self):
  109. """
  110. When processing a request for this attribute, the OP should
  111. call this method to determine whether all available attribute
  112. values were requested. If self.count == UNLIMITED_VALUES,
  113. this returns True. Otherwise this returns False, in which
  114. case self.count is an integer.
  115. """
  116. return self.count == UNLIMITED_VALUES
  117. def toTypeURIs(namespace_map, alias_list_s):
  118. """Given a namespace mapping and a string containing a
  119. comma-separated list of namespace aliases, return a list of type
  120. URIs that correspond to those aliases.
  121. @param namespace_map: The mapping from namespace URI to alias
  122. @type namespace_map: openid.message.NamespaceMap
  123. @param alias_list_s: The string containing the comma-separated
  124. list of aliases. May also be None for convenience.
  125. @type alias_list_s: str or NoneType
  126. @returns: The list of namespace URIs that corresponds to the
  127. supplied list of aliases. If the string was zero-length or
  128. None, an empty list will be returned.
  129. @raise KeyError: If an alias is present in the list of aliases but
  130. is not present in the namespace map.
  131. """
  132. uris = []
  133. if alias_list_s:
  134. for alias in alias_list_s.split(','):
  135. type_uri = namespace_map.getNamespaceURI(alias)
  136. if type_uri is None:
  137. raise KeyError('No type is defined for attribute name %r' %
  138. (alias, ))
  139. else:
  140. uris.append(type_uri)
  141. return uris
  142. class FetchRequest(AXMessage):
  143. """An attribute exchange 'fetch_request' message. This message is
  144. sent by a relying party when it wishes to obtain attributes about
  145. the subject of an OpenID authentication request.
  146. @ivar requested_attributes: The attributes that have been
  147. requested thus far, indexed by the type URI.
  148. @type requested_attributes: {str:AttrInfo}
  149. @ivar update_url: A URL that will accept responses for this
  150. attribute exchange request, even in the absence of the user
  151. who made this request.
  152. """
  153. mode = 'fetch_request'
  154. def __init__(self, update_url=None):
  155. AXMessage.__init__(self)
  156. self.requested_attributes = {}
  157. self.update_url = update_url
  158. def add(self, attribute):
  159. """Add an attribute to this attribute exchange request.
  160. @param attribute: The attribute that is being requested
  161. @type attribute: C{L{AttrInfo}}
  162. @returns: None
  163. @raise KeyError: when the requested attribute is already
  164. present in this fetch request.
  165. """
  166. if attribute.type_uri in self.requested_attributes:
  167. raise KeyError('The attribute %r has already been requested' %
  168. (attribute.type_uri, ))
  169. self.requested_attributes[attribute.type_uri] = attribute
  170. def getExtensionArgs(self):
  171. """Get the serialized form of this attribute fetch request.
  172. @returns: The fetch request message parameters
  173. @rtype: {unicode:unicode}
  174. """
  175. aliases = NamespaceMap()
  176. required = []
  177. if_available = []
  178. ax_args = self._newArgs()
  179. for type_uri, attribute in self.requested_attributes.items():
  180. if attribute.alias is None:
  181. alias = aliases.add(type_uri)
  182. else:
  183. # This will raise an exception when the second
  184. # attribute with the same alias is added. I think it
  185. # would be better to complain at the time that the
  186. # attribute is added to this object so that the code
  187. # that is adding it is identified in the stack trace,
  188. # but it's more work to do so, and it won't be 100%
  189. # accurate anyway, since the attributes are
  190. # mutable. So for now, just live with the fact that
  191. # we'll learn about the error later.
  192. #
  193. # The other possible approach is to hide the error and
  194. # generate a new alias on the fly. I think that would
  195. # probably be bad.
  196. alias = aliases.addAlias(type_uri, attribute.alias)
  197. if attribute.required:
  198. required.append(alias)
  199. else:
  200. if_available.append(alias)
  201. if attribute.count != 1:
  202. ax_args['count.' + alias] = str(attribute.count)
  203. ax_args['type.' + alias] = type_uri
  204. if required:
  205. ax_args['required'] = ','.join(required)
  206. if if_available:
  207. ax_args['if_available'] = ','.join(if_available)
  208. return ax_args
  209. def getRequiredAttrs(self):
  210. """Get the type URIs for all attributes that have been marked
  211. as required.
  212. @returns: A list of the type URIs for attributes that have
  213. been marked as required.
  214. @rtype: [str]
  215. """
  216. required = []
  217. for type_uri, attribute in self.requested_attributes.items():
  218. if attribute.required:
  219. required.append(type_uri)
  220. return required
  221. def fromOpenIDRequest(cls, openid_request):
  222. """Extract a FetchRequest from an OpenID message
  223. @param openid_request: The OpenID authentication request
  224. containing the attribute fetch request
  225. @type openid_request: C{L{openid.server.server.CheckIDRequest}}
  226. @rtype: C{L{FetchRequest}} or C{None}
  227. @returns: The FetchRequest extracted from the message or None, if
  228. the message contained no AX extension.
  229. @raises KeyError: if the AuthRequest is not consistent in its use
  230. of namespace aliases.
  231. @raises AXError: When parseExtensionArgs would raise same.
  232. @see: L{parseExtensionArgs}
  233. """
  234. message = openid_request.message
  235. ax_args = message.getArgs(cls.ns_uri)
  236. self = cls()
  237. try:
  238. self.parseExtensionArgs(ax_args)
  239. except NotAXMessage as err:
  240. return None
  241. if self.update_url:
  242. # Update URL must match the openid.realm of the underlying
  243. # OpenID 2 message.
  244. realm = message.getArg(OPENID_NS, 'realm',
  245. message.getArg(OPENID_NS, 'return_to'))
  246. if not realm:
  247. raise AXError(
  248. ("Cannot validate update_url %r " + "against absent realm")
  249. % (self.update_url, ))
  250. tr = TrustRoot.parse(realm)
  251. if not tr.validateURL(self.update_url):
  252. raise AXError(
  253. "Update URL %r failed validation against realm %r" %
  254. (self.update_url, realm, ))
  255. return self
  256. fromOpenIDRequest = classmethod(fromOpenIDRequest)
  257. def parseExtensionArgs(self, ax_args):
  258. """Given attribute exchange arguments, populate this FetchRequest.
  259. @param ax_args: Attribute Exchange arguments from the request.
  260. As returned from L{Message.getArgs<openid.message.Message.getArgs>}.
  261. @type ax_args: dict
  262. @raises KeyError: if the message is not consistent in its use
  263. of namespace aliases.
  264. @raises NotAXMessage: If ax_args does not include an Attribute Exchange
  265. mode.
  266. @raises AXError: If the data to be parsed does not follow the
  267. attribute exchange specification. At least when
  268. 'if_available' or 'required' is not specified for a
  269. particular attribute type.
  270. """
  271. # Raises an exception if the mode is not the expected value
  272. self._checkMode(ax_args)
  273. aliases = NamespaceMap()
  274. for key, value in ax_args.items():
  275. if key.startswith('type.'):
  276. alias = key[5:]
  277. type_uri = value
  278. aliases.addAlias(type_uri, alias)
  279. count_key = 'count.' + alias
  280. count_s = ax_args.get(count_key)
  281. if count_s:
  282. try:
  283. count = int(count_s)
  284. if count <= 0:
  285. raise AXError(
  286. "Count %r must be greater than zero, got %r" %
  287. (count_key, count_s, ))
  288. except ValueError:
  289. if count_s != UNLIMITED_VALUES:
  290. raise AXError("Invalid count value for %r: %r" %
  291. (count_key, count_s, ))
  292. count = count_s
  293. else:
  294. count = 1
  295. self.add(AttrInfo(type_uri, alias=alias, count=count))
  296. required = toTypeURIs(aliases, ax_args.get('required'))
  297. for type_uri in required:
  298. self.requested_attributes[type_uri].required = True
  299. if_available = toTypeURIs(aliases, ax_args.get('if_available'))
  300. all_type_uris = required + if_available
  301. for type_uri in aliases.iterNamespaceURIs():
  302. if type_uri not in all_type_uris:
  303. raise AXError('Type URI %r was in the request but not '
  304. 'present in "required" or "if_available"' %
  305. (type_uri, ))
  306. self.update_url = ax_args.get('update_url')
  307. def iterAttrs(self):
  308. """Iterate over the AttrInfo objects that are
  309. contained in this fetch_request.
  310. """
  311. return iter(self.requested_attributes.values())
  312. def __iter__(self):
  313. """Iterate over the attribute type URIs in this fetch_request
  314. """
  315. return iter(self.requested_attributes)
  316. def has_key(self, type_uri):
  317. """Is the given type URI present in this fetch_request?
  318. """
  319. return type_uri in self.requested_attributes
  320. __contains__ = has_key
  321. class AXKeyValueMessage(AXMessage):
  322. """An abstract class that implements a message that has attribute
  323. keys and values. It contains the common code between
  324. fetch_response and store_request.
  325. """
  326. # This class is abstract, so it's OK that it doesn't override the
  327. # abstract method in Extension:
  328. #
  329. #pylint:disable-msg=W0223
  330. def __init__(self):
  331. AXMessage.__init__(self)
  332. self.data = {}
  333. def addValue(self, type_uri, value):
  334. """Add a single value for the given attribute type to the
  335. message. If there are already values specified for this type,
  336. this value will be sent in addition to the values already
  337. specified.
  338. @param type_uri: The URI for the attribute
  339. @param value: The value to add to the response to the relying
  340. party for this attribute
  341. @type value: unicode
  342. @returns: None
  343. """
  344. try:
  345. values = self.data[type_uri]
  346. except KeyError:
  347. values = self.data[type_uri] = []
  348. values.append(value)
  349. def setValues(self, type_uri, values):
  350. """Set the values for the given attribute type. This replaces
  351. any values that have already been set for this attribute.
  352. @param type_uri: The URI for the attribute
  353. @param values: A list of values to send for this attribute.
  354. @type values: [unicode]
  355. """
  356. self.data[type_uri] = values
  357. def _getExtensionKVArgs(self, aliases=None):
  358. """Get the extension arguments for the key/value pairs
  359. contained in this message.
  360. @param aliases: An alias mapping. Set to None if you don't
  361. care about the aliases for this request.
  362. """
  363. if aliases is None:
  364. aliases = NamespaceMap()
  365. ax_args = {}
  366. for type_uri, values in self.data.items():
  367. alias = aliases.add(type_uri)
  368. ax_args['type.' + alias] = type_uri
  369. ax_args['count.' + alias] = str(len(values))
  370. for i, value in enumerate(values):
  371. key = 'value.%s.%d' % (alias, i + 1)
  372. ax_args[key] = value
  373. return ax_args
  374. def parseExtensionArgs(self, ax_args):
  375. """Parse attribute exchange key/value arguments into this
  376. object.
  377. @param ax_args: The attribute exchange fetch_response
  378. arguments, with namespacing removed.
  379. @type ax_args: {unicode:unicode}
  380. @returns: None
  381. @raises ValueError: If the message has bad values for
  382. particular fields
  383. @raises KeyError: If the namespace mapping is bad or required
  384. arguments are missing
  385. """
  386. self._checkMode(ax_args)
  387. aliases = NamespaceMap()
  388. for key, value in ax_args.items():
  389. if key.startswith('type.'):
  390. type_uri = value
  391. alias = key[5:]
  392. checkAlias(alias)
  393. aliases.addAlias(type_uri, alias)
  394. for type_uri, alias in aliases.items():
  395. try:
  396. count_s = ax_args['count.' + alias]
  397. except KeyError:
  398. value = ax_args['value.' + alias]
  399. if value == '':
  400. values = []
  401. else:
  402. values = [value]
  403. else:
  404. count = int(count_s)
  405. values = []
  406. for i in range(1, count + 1):
  407. value_key = 'value.%s.%d' % (alias, i)
  408. value = ax_args[value_key]
  409. values.append(value)
  410. self.data[type_uri] = values
  411. def getSingle(self, type_uri, default=None):
  412. """Get a single value for an attribute. If no value was sent
  413. for this attribute, use the supplied default. If there is more
  414. than one value for this attribute, this method will fail.
  415. @type type_uri: str
  416. @param type_uri: The URI for the attribute
  417. @param default: The value to return if the attribute was not
  418. sent in the fetch_response.
  419. @returns: The value of the attribute in the fetch_response
  420. message, or the default supplied
  421. @rtype: unicode or NoneType
  422. @raises ValueError: If there is more than one value for this
  423. parameter in the fetch_response message.
  424. @raises KeyError: If the attribute was not sent in this response
  425. """
  426. values = self.data.get(type_uri)
  427. if not values:
  428. return default
  429. elif len(values) == 1:
  430. return values[0]
  431. else:
  432. raise AXError('More than one value present for %r' % (type_uri, ))
  433. def get(self, type_uri):
  434. """Get the list of values for this attribute in the
  435. fetch_response.
  436. XXX: what to do if the values are not present? default
  437. parameter? this is funny because it's always supposed to
  438. return a list, so the default may break that, though it's
  439. provided by the user's code, so it might be okay. If no
  440. default is supplied, should the return be None or []?
  441. @param type_uri: The URI of the attribute
  442. @returns: The list of values for this attribute in the
  443. response. May be an empty list.
  444. @rtype: [unicode]
  445. @raises KeyError: If the attribute was not sent in the response
  446. """
  447. return self.data[type_uri]
  448. def count(self, type_uri):
  449. """Get the number of responses for a particular attribute in
  450. this fetch_response message.
  451. @param type_uri: The URI of the attribute
  452. @returns: The number of values sent for this attribute
  453. @raises KeyError: If the attribute was not sent in the
  454. response. KeyError will not be raised if the number of
  455. values was zero.
  456. """
  457. return len(self.get(type_uri))
  458. class FetchResponse(AXKeyValueMessage):
  459. """A fetch_response attribute exchange message
  460. """
  461. mode = 'fetch_response'
  462. def __init__(self, request=None, update_url=None):
  463. """
  464. @param request: When supplied, I will use namespace aliases
  465. that match those in this request. I will also check to
  466. make sure I do not respond with attributes that were not
  467. requested.
  468. @type request: L{FetchRequest}
  469. @param update_url: By default, C{update_url} is taken from the
  470. request. But if you do not supply the request, you may set
  471. the C{update_url} here.
  472. @type update_url: str
  473. """
  474. AXKeyValueMessage.__init__(self)
  475. self.update_url = update_url
  476. self.request = request
  477. def getExtensionArgs(self):
  478. """Serialize this object into arguments in the attribute
  479. exchange namespace
  480. @returns: The dictionary of unqualified attribute exchange
  481. arguments that represent this fetch_response.
  482. @rtype: {unicode;unicode}
  483. """
  484. aliases = NamespaceMap()
  485. zero_value_types = []
  486. if self.request is not None:
  487. # Validate the data in the context of the request (the
  488. # same attributes should be present in each, and the
  489. # counts in the response must be no more than the counts
  490. # in the request)
  491. for type_uri in self.data:
  492. if type_uri not in self.request:
  493. raise KeyError(
  494. 'Response attribute not present in request: %r' %
  495. (type_uri, ))
  496. for attr_info in self.request.iterAttrs():
  497. # Copy the aliases from the request so that reading
  498. # the response in light of the request is easier
  499. if attr_info.alias is None:
  500. aliases.add(attr_info.type_uri)
  501. else:
  502. aliases.addAlias(attr_info.type_uri, attr_info.alias)
  503. try:
  504. values = self.data[attr_info.type_uri]
  505. except KeyError:
  506. values = []
  507. zero_value_types.append(attr_info)
  508. if (attr_info.count != UNLIMITED_VALUES) and \
  509. (attr_info.count < len(values)):
  510. raise AXError(
  511. 'More than the number of requested values were '
  512. 'specified for %r' % (attr_info.type_uri, ))
  513. kv_args = self._getExtensionKVArgs(aliases)
  514. # Add the KV args into the response with the args that are
  515. # unique to the fetch_response
  516. ax_args = self._newArgs()
  517. # For each requested attribute, put its type/alias and count
  518. # into the response even if no data were returned.
  519. for attr_info in zero_value_types:
  520. alias = aliases.getAlias(attr_info.type_uri)
  521. kv_args['type.' + alias] = attr_info.type_uri
  522. kv_args['count.' + alias] = '0'
  523. update_url = ((self.request and self.request.update_url) or
  524. self.update_url)
  525. if update_url:
  526. ax_args['update_url'] = update_url
  527. ax_args.update(kv_args)
  528. return ax_args
  529. def parseExtensionArgs(self, ax_args):
  530. """@see: {Extension.parseExtensionArgs<openid.extension.Extension.parseExtensionArgs>}"""
  531. super(FetchResponse, self).parseExtensionArgs(ax_args)
  532. self.update_url = ax_args.get('update_url')
  533. def fromSuccessResponse(cls, success_response, signed=True):
  534. """Construct a FetchResponse object from an OpenID library
  535. SuccessResponse object.
  536. @param success_response: A successful id_res response object
  537. @type success_response: openid.consumer.consumer.SuccessResponse
  538. @param signed: Whether non-signed args should be
  539. processsed. If True (the default), only signed arguments
  540. will be processsed.
  541. @type signed: bool
  542. @returns: A FetchResponse containing the data from the OpenID
  543. message, or None if the SuccessResponse did not contain AX
  544. extension data.
  545. @raises AXError: when the AX data cannot be parsed.
  546. """
  547. self = cls()
  548. ax_args = success_response.extensionResponse(self.ns_uri, signed)
  549. try:
  550. self.parseExtensionArgs(ax_args)
  551. except NotAXMessage as err:
  552. return None
  553. else:
  554. return self
  555. fromSuccessResponse = classmethod(fromSuccessResponse)
  556. class StoreRequest(AXKeyValueMessage):
  557. """A store request attribute exchange message representation
  558. """
  559. mode = 'store_request'
  560. def __init__(self, aliases=None):
  561. """
  562. @param aliases: The namespace aliases to use when making this
  563. store request. Leave as None to use defaults.
  564. """
  565. super(StoreRequest, self).__init__()
  566. self.aliases = aliases
  567. def getExtensionArgs(self):
  568. """
  569. @see: L{Extension.getExtensionArgs<openid.extension.Extension.getExtensionArgs>}
  570. """
  571. ax_args = self._newArgs()
  572. kv_args = self._getExtensionKVArgs(self.aliases)
  573. ax_args.update(kv_args)
  574. return ax_args
  575. class StoreResponse(AXMessage):
  576. """An indication that the store request was processed along with
  577. this OpenID transaction.
  578. """
  579. SUCCESS_MODE = 'store_response_success'
  580. FAILURE_MODE = 'store_response_failure'
  581. def __init__(self, succeeded=True, error_message=None):
  582. AXMessage.__init__(self)
  583. if succeeded and error_message is not None:
  584. raise AXError('An error message may only be included in a '
  585. 'failing fetch response')
  586. if succeeded:
  587. self.mode = self.SUCCESS_MODE
  588. else:
  589. self.mode = self.FAILURE_MODE
  590. self.error_message = error_message
  591. def succeeded(self):
  592. """Was this response a success response?"""
  593. return self.mode == self.SUCCESS_MODE
  594. def getExtensionArgs(self):
  595. """@see: {Extension.getExtensionArgs<openid.extension.Extension.getExtensionArgs>}"""
  596. ax_args = self._newArgs()
  597. if not self.succeeded() and self.error_message:
  598. ax_args['error'] = self.error_message
  599. return ax_args