extension.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import warnings
  2. from openid import message as message_module
  3. class Extension(object):
  4. """An interface for OpenID extensions.
  5. @ivar ns_uri: The namespace to which to add the arguments for this
  6. extension
  7. """
  8. ns_uri = None
  9. ns_alias = None
  10. def getExtensionArgs(self):
  11. """Get the string arguments that should be added to an OpenID
  12. message for this extension.
  13. @returns: A dictionary of completely non-namespaced arguments
  14. to be added. For example, if the extension's alias is
  15. 'uncle', and this method returns {'meat':'Hot Rats'}, the
  16. final message will contain {'openid.uncle.meat':'Hot Rats'}
  17. """
  18. raise NotImplementedError()
  19. def toMessage(self, message=None):
  20. """Add the arguments from this extension to the provided
  21. message, or create a new message containing only those
  22. arguments.
  23. @returns: The message with the extension arguments added
  24. """
  25. if message is None:
  26. warnings.warn(
  27. 'Passing None to Extension.toMessage is deprecated. '
  28. 'Creating a message assuming you want OpenID 2.',
  29. DeprecationWarning,
  30. stacklevel=2)
  31. message = message_module.Message(message_module.OPENID2_NS)
  32. implicit = message.isOpenID1()
  33. try:
  34. message.namespaces.addAlias(
  35. self.ns_uri, self.ns_alias, implicit=implicit)
  36. except KeyError:
  37. if message.namespaces.getAlias(self.ns_uri) != self.ns_alias:
  38. raise
  39. message.updateArgs(self.ns_uri, self.getExtensionArgs())
  40. return message