
import random
import zlib
import base64
import urllib2

import twitter

from django.conf import settings

def twitter_account_raw(username, password):
    if username and password:
        twitter_password = get_twitter_password(settings.SECRET_KEY,
            password, decode=True)
        return twitter.Api(username=username, password=twitter_password)

def twitter_account_for_user(user):
    profile = user.get_profile()
    return twitter_account_raw(profile.twitter_user, profile.twitter_password)

def twitter_verify_credentials(account):
    if account is None:
        return False
    url = 'http://twitter.com/account/verify_credentials.json'
    try:
        json = account._FetchUrl(url)
    except account._urllib.HTTPError:
        return False
    return True

def get_twitter_password(key, text, decode=False):
    rand = random.Random(key).randrange
    xortext = lambda text : ''.join([chr(ord(elem)^rand(256)) for elem in text])
    if not decode:
        text = base64.encodestring(xortext(zlib.compress(text)))
    else:
        text = zlib.decompress(xortext(base64.decodestring(text)))
    return text


@@ -153,281 +153,301 @@
     
-if username and password:%0A        twitter_password = get_twitter_password(settings.SECRET_KEY,%0A            password, decode=True)%0A        return twitter.Api(username=username, password=twitter_password)%0A%0Adef twitter_account_for_user(user):%0A    profile = user.get_profile()%0A
+return twitter.Api(username=username, password=password)%0A%0Adef twitter_account_for_user(user):%0A    profile = user.get_profile()%0A    if profile.twitter_user and profile.twitter_password:%0A        twitter_password = get_twitter_password(settings.SECRET_KEY,%0A            password, decode=True)%0A    
     
@@ -495,24 +495,16 @@
 r_user, 
-profile.
 twitter_

Fix a blind change I made in r618 thinking it was correct to encode/decode password in twitter_account_raw. Bad idea.