Exemplo n.º 1
0
    def authenticate(self, remote_token, remote_user, remote_ip, token_key):
        
        if not remote_token or not remote_user:
            return

        user = None
        try:
            logging.debug("Token: %s User: %s IP: %s Key: %s" % (remote_token, remote_user, remote_ip, token_key))
            #Appengine legacy query
            user = User.objects.get(username=remote_user)
            authtoken = AuthToken.objects.get(token=remote_token, user=user)
            key = generate_key(authtoken.token, authtoken.token_key)
            logging.debug("GenKey: %s" % key)
            if not key == token_key:
                return None
            user = authtoken.user

            obj, created = TokenUsage.objects.get_or_create(token=authtoken, ip=remote_ip)
            if not created:
                obj.save() # if the object already exists, we force a save to update the last access date
        except (AuthToken.DoesNotExist, User.DoesNotExist):
            logging.DEBUG("NOT FOUND")
        return user
Exemplo n.º 2
0
    
class ExportService(object):

    def __init__(self, username, token, token_key):
        self.url = settings.EXPORT_URL
        self.username = username
        self.token = token
        self.token_key = token_key
        
    def update(self, data):
        rsp = ExportServiceResponse()
        try:
            postdata = serializers.serialize("json",data)
        except Exception, e:
            rsp.reason = str(e)
        else:            
            urldata = urlparse('//%s' % self.url)
            if time.localtime().tm_sec > 50:
                time.sleep(15)
            token_data = "%s-%s-%s"% (self.username, self.token, generate_key(self.token, self.token_key))
            params = urllib.urlencode({'REMOTE_TOKEN': token_data, 'data': postdata})
            conn = httplib.HTTPConnection(urldata.netloc, timeout=30)
            conn.request("POST", urldata.path, params)
            response = conn.getresponse()
            rsp.status = response.status
            rsp.reason = response.reason
            if response.status == 200:
                rsp.response = json.loads(response.read())
            conn.close()
        return rsp