associate 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. """Make an OpenID Assocition request against an endpoint
  3. and print the results."""
  4. import sys
  5. from openid.store.memstore import MemoryStore
  6. from openid.consumer import consumer
  7. from openid.consumer.discover import OpenIDServiceEndpoint
  8. from datetime import datetime
  9. def verboseAssociation(assoc):
  10. """A more verbose representation of an Association.
  11. """
  12. d = assoc.__dict__
  13. issued_date = datetime.fromtimestamp(assoc.issued)
  14. d['issued_iso'] = issued_date.isoformat()
  15. fmt = """ Type: %(assoc_type)s
  16. Handle: %(handle)s
  17. Issued: %(issued)s [%(issued_iso)s]
  18. Lifetime: %(lifetime)s
  19. Secret: %(secret)r
  20. """
  21. return fmt % d
  22. def main():
  23. if not sys.argv[1:]:
  24. print "Usage: %s ENDPOINT_URL..." % (sys.argv[0],)
  25. for endpoint_url in sys.argv[1:]:
  26. print "Associating with", endpoint_url
  27. # This makes it clear why j3h made AssociationManager when we
  28. # did the ruby port. We can't invoke requestAssociation
  29. # without these other trappings.
  30. store = MemoryStore()
  31. endpoint = OpenIDServiceEndpoint()
  32. endpoint.server_url = endpoint_url
  33. c = consumer.GenericConsumer(store)
  34. auth_req = c.begin(endpoint)
  35. if auth_req.assoc:
  36. print verboseAssociation(auth_req.assoc)
  37. else:
  38. print " ...no association."
  39. if __name__ == '__main__':
  40. main()