Updating version for precise
[invirt/packages/invirt-vnc-server.git] / invirt-vnc-authtoken
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import hmac
6 import cPickle
7 import sha
8 import time
9 import base64
10 from invirt.vnc import getTokenKey
11 from invirt.config import structs as config
12
13 def getAuthToken(username, machine, lifetime=5*60):
14     data = {}
15     data['user'] = username
16     data['machine'] = machine
17     data['expires'] = time.time() + lifetime
18     data['connect_host'] = config.web.hostname
19     try:
20         data['connect_port'] = 10003 + [h.hostname for h in
21                                         config.hosts].index(os.uname()[1])
22     except:
23         data['connect_port'] = 5900
24     pickled_data = cPickle.dumps(data)
25     m = hmac.new(getTokenKey(), digestmod=sha)
26     m.update(pickled_data)
27     token = ".".join(map(base64.urlsafe_b64encode, (pickled_data, m.digest())))
28     return token
29
30 def main():
31     try:
32         username = os.environ['REMOTE_USER']
33     except KeyError:
34         username = None
35     machine = sys.argv[1]
36     print getAuthToken(username, machine)
37
38 if __name__ == '__main__':
39     main()