summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-16 23:33:12 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-16 23:33:12 +0000
commit0f1844f2a3cae84db365b02c617daa3626c8b582 (patch)
treeb365ad73437535d53c1f1ea8b3919a483186b6fa
parent6a590374197a7aebf4b55585132aa6368ea7de67 (diff)
downloadchromium_src-0f1844f2a3cae84db365b02c617daa3626c8b582.zip
chromium_src-0f1844f2a3cae84db365b02c617daa3626c8b582.tar.gz
chromium_src-0f1844f2a3cae84db365b02c617daa3626c8b582.tar.bz2
Added hostdir.py - a simple Chromoting Directory client.
TEST=None BUG=None Review URL: http://codereview.chromium.org/3133014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56263 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--remoting/tools/gaia_auth.py4
-rwxr-xr-xremoting/tools/hostdir.py227
-rwxr-xr-xremoting/tools/register_host.py6
3 files changed, 232 insertions, 5 deletions
diff --git a/remoting/tools/gaia_auth.py b/remoting/tools/gaia_auth.py
index da364e0..392f2f3 100644
--- a/remoting/tools/gaia_auth.py
+++ b/remoting/tools/gaia_auth.py
@@ -6,10 +6,10 @@ import getpass
import os
import urllib
-default_gaia_url = "https://www.google.com:443/accounts/ClientLogin"
+DEFAULT_GAIA_URL = "https://www.google.com:443/accounts/ClientLogin"
class GaiaAuthenticator:
- def __init__(self, service, url = default_gaia_url):
+ def __init__(self, service, url = DEFAULT_GAIA_URL):
self._service = service
self._url = url
diff --git a/remoting/tools/hostdir.py b/remoting/tools/hostdir.py
new file mode 100755
index 0000000..d4905f4
--- /dev/null
+++ b/remoting/tools/hostdir.py
@@ -0,0 +1,227 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Chromoting Directory API client implementation. Used for testing/debugging
+# purposes. Requires Python 2.6: json module is not available in earlier
+# versions.
+
+import os
+import httplib
+import json
+import urllib
+import urllib2
+import random
+import sys
+
+DEFAULT_DIRECTORY_SERVER = 'www-googleapis-test.sandbox.google.com'
+
+auth_filepath = os.path.join(os.path.expanduser('~'),
+ '.chromotingDirectoryAuthToken')
+
+def random_uuid():
+ return ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x" %
+ tuple(map(lambda x: random.randrange(0,65536), range(8))))
+
+class Host:
+ def __init__(self, parameters=None):
+ if parameters != None:
+ self.host_id = parameters[u"hostId"]
+ self.host_name = parameters[u"hostName"]
+ self.public_key = parameters[u"publicKey"]
+ # Following fields may be missing, use get() for them.
+ self.jabber_id = parameters.get(u"jabberId")
+ self.created_time = parameters.get(u"createdTime")
+ self.updated_time = parameters.get(u"updatedTime")
+ self.status = parameters.get(u"status")
+ else:
+ self.host_id = random_uuid()
+ import socket
+ self.host_name = socket.gethostname()
+ self.public_key = None
+ self.jabber_id = None
+ self.created_time = None
+ self.updated_time = None
+ self.status = None
+
+class HostDirectoryError(Exception):
+ def __init__(self, message, response):
+ Exception.__init__(self, message)
+ print response
+ self._response = response
+
+class HostDirectory:
+ def __init__(self, username, auth_token, server=DEFAULT_DIRECTORY_SERVER):
+ self._username = username
+ self._auth_token = auth_token
+ self._base_url = '/chromoting/v1/@me/hosts'
+
+ self._http = httplib.HTTPConnection(server)
+ self._headers = {"Authorization": "GoogleLogin auth=" + self._auth_token,
+ "Content-Type": "application/json" }
+
+ def add_host(self, host):
+ host_json = { 'data':
+ { 'hostId': host.host_id,
+ 'hostName': host.host_name,
+ 'publicKey': host.public_key,
+ }
+ }
+ if host.jabber_id:
+ host_json['data']['jabberId'] = host.jabber_id
+ post_data = json.dumps(host_json)
+ self._http.request("POST", self._base_url, post_data, self._headers)
+ response = self._http.getresponse()
+ if response.status != 200:
+ raise HostDirectoryError(response.reason, response.read())
+ data = response.read()
+
+ def get_hosts(self):
+ self._http.request("GET", self._base_url, headers=self._headers)
+ response = self._http.getresponse()
+ if response.status != 200:
+ raise HostDirectoryError(response.reason, response.read())
+ data = response.read()
+ data = json.loads(data)[u'data']
+ results = []
+ if data.has_key(u'items'):
+ for item in data[u'items']:
+ results.append(Host(item))
+ return results
+
+ def delete_host(self, host_id):
+ url = self._base_url + '/' + host_id
+ self._http.request("DELETE", url, headers=self._headers)
+ response = self._http.getresponse()
+ if response.status / 100 != 2: # Normally 204 is returned
+ raise HostDirectoryError(response.reason, response.read())
+ data = response.read()
+
+def usage():
+ sys.stderr.write(
+ ("Usage:\n" +
+ " Login: \t\t%(cmd)s login\n" +
+ " Register host: \t%(cmd)s insert --hostId=<hostId>" +
+ " --hostName=<hostName> \\\n" +
+ "\t\t\t --publicKey=<publicKey> --jabberId=<jabberId>\n" +
+ " List hosts: \t\t%(cmd)s list\n" +
+ " Delete a host: \t%(cmd)s delete <host_id>\n")
+ % {"cmd" : sys.argv[0]})
+ return 1
+
+class CommandError(Exception):
+ def __init__(self, message):
+ Exception.__init__(self, message)
+
+def load_auth_token():
+ try:
+ lines = open(auth_filepath).readlines()
+ except IOError as e:
+ raise CommandError("Can't open file (%s). Please run " +
+ "'%s login' and try again." %
+ (auth_filepath, sys.argv[0]))
+ if len(lines) != 2:
+ raise CommandError("Invalid auth file (%s). Please run " +
+ "'%s login' and try again." %
+ (auth_filepath, sys.argv[0]))
+ return map(lambda x: x.strip(), lines)
+
+def login_cmd(args):
+ """login command"""
+ if len(args) != 0:
+ return usage()
+
+ import getpass
+ import gaia_auth
+
+ print "Email:",
+ email = raw_input()
+ passwd = getpass.getpass("Password: ")
+
+ authenticator = gaia_auth.GaiaAuthenticator('chromoting');
+ auth_token = authenticator.authenticate(email, passwd)
+
+ # Set permission mask for created file.
+ os.umask(0066)
+ auth_file = open(auth_filepath, 'w')
+ auth_file.write(email)
+ auth_file.write('\n')
+ auth_file.write(auth_token)
+ auth_file.close()
+
+ print 'Auth token: ', auth_token
+ print '...saved in', auth_filepath
+
+def list_cmd(args):
+ """list command"""
+ if len(args) != 0:
+ return usage()
+ (username, token) = load_auth_token()
+ client = HostDirectory(username, token)
+ print '%36s %30s %s' % ("HOST ID", "HOST NAME", "JABBER ID")
+ for host in client.get_hosts():
+ print '%36s %30s %s' % (host.host_id, host.host_name, host.jabber_id)
+ return 0
+
+def insert_cmd(args):
+ """insert command"""
+ (username, token) = load_auth_token()
+ client = HostDirectory(username, token)
+
+ host = Host()
+ for arg in args:
+ if arg.startswith("--hostId="):
+ host.host_id = arg[len("--hostId="):]
+ elif arg.startswith("--hostName="):
+ host.host_name = arg[len("--hostName="):]
+ elif arg.startswith("--publicKey="):
+ host.public_key = arg[len("--publicKey="):]
+ elif arg.startswith("--jabberId="):
+ host.jabber_id = arg[len("--jabberId="):]
+ else:
+ return usage()
+
+ client.add_host(host)
+ return 0
+
+def delete_cmd(args):
+ """delete command"""
+ if len(args) != 1:
+ return usage()
+ host_id = args[0]
+ (username, token) = load_auth_token()
+ client = HostDirectory(username, token)
+ client.delete_host(host_id)
+ return 0
+
+def main():
+ import sys
+ args = sys.argv[1:]
+ if len(args) == 0:
+ return usage()
+ command = args[0]
+
+ try:
+ if command == "help":
+ usage()
+ elif command == "login":
+ return login_cmd(args[1:])
+ elif command == "list":
+ return list_cmd(args[1:])
+ elif command == "insert":
+ return insert_cmd(args[1:])
+ elif command == "delete":
+ return delete_cmd(args[1:])
+ else:
+ raise CommandError("Unknown command: %s" % command);
+
+ except CommandError as e:
+ sys.stderr.write("%s\n" % e.args[0])
+ return 1
+
+ return 0
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/remoting/tools/register_host.py b/remoting/tools/register_host.py
index 648e692..ccfe378 100755
--- a/remoting/tools/register_host.py
+++ b/remoting/tools/register_host.py
@@ -32,8 +32,8 @@ print "Email:",
email = raw_input()
password = getpass.getpass("Password: ")
-xapi_auth = gaia_auth.GaiaAuthenticator('chromoting')
-xapi_token = xapi_auth.authenticate(email, password)
+chromoting_auth = gaia_auth.GaiaAuthenticator('chromoting')
+auth_token = chromoting_auth.authenticate(email, password)
host_id = random_uuid()
print "HostId:", host_id
@@ -50,7 +50,7 @@ params = ('{"data":{' + \
'"publicKey": "%(publicKey)s"}}') % \
{'hostId': host_id, 'hostName': host_name,
'publicKey': public_key}
-headers = {"Authorization": "GoogleLogin auth=" + xapi_token,
+headers = {"Authorization": "GoogleLogin auth=" + auth_token,
"Content-Type": "application/json" }
request = urllib2.Request(url, params, headers)