2

I have Python script which create proxy to GMail email box via IMAP. This proxy adds authentication token to request. End user run this script and after that use some tool to downloading all user emails from email box. Of course, this tool connecting with proxy and proxy makes all authentication magic.
My current solution works only for small email boxes. When we want to download all emails from big email box, we receive authentication error. Authentication error is linked with token expiration. Authentication token is valid only during one hour. After that server returns error and close connection.
I didn't write this script. I have received it from someone and I have to fix this bug. In this script I use Twisted engine (Twisted code examples).

Let me show some source code. Below you can see two main methods from Proxy class:

def dataReceived(self, data):
    if OPTIONS.verbose:
        print "> " + data
    #send raw data
    Proxy.dataReceived(self,data)

def lineReceived(self, line):
    if self.logged_in:
        self.setRawMode()
        self.peer.setRawMode()
        Proxy.lineReceived(self,line)
    else:
        login_match = self.factory.login_re.match(line)
        if login_match != None:
            #Create token and send it
            consumer = OAuthEntity(self.domain, self.secret)
            xoauth_string = GenerateXOauthString(consumer, self.email_address)
            xoauth_b64 = base64.b64encode(xoauth_string)
            line = login_match.groups()[0] + " AUTHENTICATE XOAUTH " + xoauth_b64
            Proxy.lineReceived(self,line)
        else:
            Proxy.lineReceived(self,line)

As you can see in lineReceived method I create token and send it when I receive login request from client tool. Now script creates token only once. I decided to change this script and recreate token in dataReceived method after each hour. Below you can see my changes:

def dataReceived(self, data):
    if OPTIONS.verbose:
        print "> " + data
    if anHourPassedFromLastRecreation():
        consumer = OAuthEntity(self.domain, self.secret)
        xoauth_string = GenerateXOauthString(consumer, self.email_address)
        xoauth_b64 = base64.b64encode(xoauth_string)
        line = self.prefix + " AUTHENTICATE XOAUTH " + xoauth_b64
        Proxy.lineReceived(self,line)
        Proxy.dataReceived(self,data)
    else:
        Proxy.dataReceived(self,data)

As you can see, in dataReceived method I try to recreate token and send it before data to the server. I know it can be a little naive, but I do not have any other idea, how I can recreate token and use it in my future requests.
Unfortunately it doesn't work. Could you give me some advice how to create proxy or how to recreate token and how to link it with future requests?

Helpful links:
- IMAP protocol exchange.
- Gmail IMAP OAuth for desktop clients.

Community
  • 1
  • 1
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146

0 Answers0