def __get_storage(user_id, callback): """ Retrieve an instance of the current valid storage system for the user with user_id In future` if we add different storage mechanism it should be placed here """ #Try cache account_info = \ yield gen.Task(StorageServer.__cache.get_user_info, user_id) if account_info is not None: account_obj = json.loads(account_info) key = account_obj['key'] secret = account_obj['secret'] storage = DropboxHelper.create_client(key, secret) callback(storage) else: #get it from DB account_info = yield gen.Task(Accounts.get_account,user_id) if account_info is not None: key = account_info['ticket'][0] secret = account_info['ticket'][1] #cache it account_info_json = json.dumps({'key':key, 'secret':secret}) yield gen.Task(StorageServer.__cache.set_user_info, user_id, account_info_json) storage = DropboxHelper.create_client(key, secret) callback(storage) else: callback(None)
def post(self, account_id): self.__log.info('%s - POST : authenticating user %s' % (str(self.__class__), account_id)) if self.active_requests.has_key(account_id): sess = DropboxHelper.create_session() request_token = self.active_requests[account_id] #If the request token has been expired send # Not Authorized. The user should start again if request_token is None: self.set_status(401) #Get the access token from dropbox access_token = yield gen.Task(sess.obtain_access_token,request_token=request_token) #Store it for future use in the mongo yield gen.Task(Accounts.add_account, account_id,access_token) #remove the pending request from the dictionary del self.active_requests[account_id] self.__log.info('%s - POST: account %s added' % (str(self.__class__), account_id)) self.set_status(200) self.write('OK') self.finish() else: self.set_status(401)
def get(self, account_id): """ Authorize a single user. If user has been authorized before return authorized . Otherwise return unauthorized. When unauthorized is issued the user is added to a pending list. The user is removed from the pending list when he indicates that he has authorized mindcloud by issuing a post on this class After some period of time the users who have not answered the unauthorized call will be removed from the pending list """ self.__log.info('%s - GET : get user info for user %s' % (str(self.__class__), account_id)) sess = DropboxHelper.create_session() account_info = yield gen.Task(Accounts.get_account, account_id) if account_info: json_str = json.dumps({'account_status':'authorized'}) self.write(json_str) self.finish() else: request_token = yield gen.Task(sess.obtain_request_token) #store the request_token until the user approves self.active_requests[account_id] = request_token #lazily initialize __sweep timer if not self.timer: self.timer = Timer(self.__SWEEP_PERIOD, self.__sweep) self.timer.start() url = sess.build_authorize_url(request_token) json_str = json.dumps({'account_status':'unauthorized', 'url':url}) self._generate_headers() self.write(json_str) self.finish()