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