Commit dbed85c2 authored by Sriram Karra's avatar Sriram Karra

Use oAuth Service Account to try and access contact list.

This is not working; but committing existing code so we can refer
back to what was not working.

Basically service accounts cannot refer to the user's contacts.

It would have been great if this had worked beacuase the service
accoutn client id and key can be used as the username and
password in all of the ASynK code without further messing.

But looksl ike this is not possible and we will have to move to
full blown oAuth. And that f-ing sucks.
parent c8f58c81
## httplib2, oauth2client, PyOpenSSL
##
## Created : Thu Jul 07 14:47:54 IST 2011
##
......@@ -22,6 +24,7 @@
import datetime, getopt, logging, sys, time, utils
import atom, gdata.contacts.data, gdata.contacts.client, base64
from oauth2client.client import SignedJwtAssertionCredentials
from state import Config
from pimdb import PIMDB, GoutInvalidPropValueError
......@@ -47,8 +50,8 @@ class GCPIMDB(PIMDB):
def __init__ (self, config, user, pw):
PIMDB.__init__(self, config)
self.set_user(user)
self.set_pw(pw)
self.set_client_email(user)
self.set_private_key(pw)
self.gc_init()
self.set_folders()
......@@ -175,16 +178,16 @@ class GCPIMDB(PIMDB):
## Now the non-abstract methods and internal methods
##
def get_user (self):
def get_client_email (self):
return self.user
def set_user (self, user):
def set_client_email (self, user):
self.user = user
def get_pw (self):
def get_private_key (self):
return self.pw
def set_pw (self, pw):
def set_private_key (self, pw):
self.pw = pw
def get_gdc (self):
......@@ -196,8 +199,15 @@ class GCPIMDB(PIMDB):
def gc_init (self):
logging.info('Logging into Google...')
em = self.get_client_email()
key = self.get_private_key()
scope = 'https://www.google.com/m8/feeds'
gdc = gdata.contacts.client.ContactsClient(source='ASynK')
gdc.ClientLogin(self.get_user(), self.get_pw(), gdc.source)
credentials = SignedJwtAssertionCredentials(em, key, scope)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
auth2token.authorize(gdc)
self.set_gdc(gdc)
# if not self.get_config().get_gid():
......
......@@ -24,7 +24,7 @@
## (DB ID, Store ID, Folder ID)
##
import logging, netrc
import logging, netrc, os
from abc import ABCMeta, abstractmethod
from pimdb_bb import BBPIMDB
from gdata.client import BadAuthentication
......@@ -330,6 +330,46 @@ class GCCollection(Collection):
Collection.__init__(self, config=config, dbid='gc', stid=stid, fid=fid,
pname=pname, colln=colln)
## We are overriding this method from base class Collection as we want to
## hack the way netrc password is really specified. This is a quick hack
## to see if this can fly
def init_username_pwd (self):
print 'pname, db, colln: ', self.get_pname(), self.get_dbid(), self.get_colln()
netrc_u, netrc_p = netrc.get_auth(self.get_pname(), self.get_dbid(),
self.get_colln())
## Hack Alert!: We will treat the "password" as a filename in case the
## first character starts with ~ or / and read that in.
if netrc_p and len(netrc_p) > 0 and netrc_p[0] in ['~', '/']:
fn = os.path.abspath(os.path.expanduser(netrc_p))
with open(fn, "rb") as f:
netrc_p = f.read()
u = netrc_u
p = netrc_p
cmd_u = self.get_username()
cmd_p = self.get_pwd()
dbn = '%s%s' % (self.get_dbid(), self.get_colln())
if cmd_u is not None:
logging.debug('Using cmdline username for collection %s', dbn)
u = cmd_u
else:
while not u and self.force_username():
u = raw_input('Please enter username for %s: ' % dbn)
if cmd_p is not None:
logging.debug('Using cmdline password for collection %s', dbn)
p = cmd_p
else:
while not p and self.force_pwd():
p = raw_input('Enter password for %s: ' % dbn)
self.set_username(u)
self.set_pwd(p)
def login (self):
try:
pimgc = GCPIMDB(self.get_config(),
......
......@@ -110,6 +110,14 @@ def setup_parser ():
# Google Contacts authentication
gg = p.add_argument_group('Google Authentication')
gg.add_argument('--gcuser', action='store', nargs='+',
help=('Google sername. Relevant only if --db=gc is used. '
'You can specify two if you are operating with 2 cd '
'dbs. You could also specify one from netrc and one '
'on the command line. First one can optionally be '
'"None" (without the quotes). '
'If this option is not specified, user is prompted '
'for it from stdin if required.'))
gg.add_argument('--gcpwd', action='store', nargs='+',
help=('Google password. Relevant only if --db=gc is used. '
'You can specify two if you are operating with 2 gc '
......@@ -178,6 +186,9 @@ class AsynkBuilderC:
coll.set_stid(stid)
def _snarf_auth_creds (self, uinps):
if uinps.gcuser and len(uinps.gcuser) > 2:
raise AsynkParserError('--gcuser takes 1 or 2 arguments only')
if uinps.gcpwd and len(uinps.gcpwd) > 2:
raise AsynkParserError('--gcpwd takes 1 or 2 arguments only')
......@@ -192,6 +203,12 @@ class AsynkBuilderC:
raise AsynkParserError('--cdpwd and --gcpwd should together have'
'only 2 values')
if uinps.gcuser:
for i, gcuser in enumerate(uinps.gcuser):
coll = self.asynk.get_colls()[i]
if gcuser != 'None':
coll.set_username(gcuser)
if uinps.gcpwd:
for i, gcpwd in enumerate(uinps.gcpwd):
coll = self.asynk.get_colls()[i]
......@@ -239,13 +256,16 @@ class AsynkBuilderC:
op = 'op_' + string.replace(uinps.op, '-', '_')
self.asynk.set_op(op)
self._snarf_pname(uinps)
# Let's start with the db flags
if uinps.db:
if len(uinps.db) > 2:
raise AsynkParserError('--db takes 1 or 2 arguments only')
for dbid in uinps.db:
coll = coll_id_class[dbid](config=self.asynk.get_config())
coll = coll_id_class[dbid](config=self.asynk.get_config(),
pname=self.asynk.get_name())
self.asynk.add_coll(coll)
else:
# Only a few operations do not need a db. Check for this and move
......@@ -265,7 +285,6 @@ class AsynkBuilderC:
self._snarf_store_ids(uinps)
self._snarf_auth_creds(uinps)
self._snarf_pname(uinps)
self._snarf_folder_ids(uinps)
self._snarf_sync_dir(uinps)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment