extension.py 1.6 KB

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