services.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- test-case-name: openid.test.test_services -*-
  2. from openid.yadis.filters import mkFilter
  3. from openid.yadis.discover import discover, DiscoveryFailure
  4. from openid.yadis.etxrd import parseXRDS, iterServices, XRDSError
  5. def getServiceEndpoints(input_url, flt=None):
  6. """Perform the Yadis protocol on the input URL and return an
  7. iterable of resulting endpoint objects.
  8. @param flt: A filter object or something that is convertable to
  9. a filter object (using mkFilter) that will be used to generate
  10. endpoint objects. This defaults to generating BasicEndpoint
  11. objects.
  12. @param input_url: The URL on which to perform the Yadis protocol
  13. @return: The normalized identity URL and an iterable of endpoint
  14. objects generated by the filter function.
  15. @rtype: (str, [endpoint])
  16. @raises DiscoveryFailure: when Yadis fails to obtain an XRDS document.
  17. """
  18. result = discover(input_url)
  19. try:
  20. endpoints = applyFilter(result.normalized_uri,
  21. result.response_text, flt)
  22. except XRDSError, err:
  23. raise DiscoveryFailure(str(err), None)
  24. return (result.normalized_uri, endpoints)
  25. def applyFilter(normalized_uri, xrd_data, flt=None):
  26. """Generate an iterable of endpoint objects given this input data,
  27. presumably from the result of performing the Yadis protocol.
  28. @param normalized_uri: The input URL, after following redirects,
  29. as in the Yadis protocol.
  30. @param xrd_data: The XML text the XRDS file fetched from the
  31. normalized URI.
  32. @type xrd_data: str
  33. """
  34. flt = mkFilter(flt)
  35. et = parseXRDS(xrd_data)
  36. endpoints = []
  37. for service_element in iterServices(et):
  38. endpoints.extend(
  39. flt.getServiceEndpoints(normalized_uri, service_element))
  40. return endpoints