Exemplo n.º 1
0
def ClientHandler(url,clientHttpHeader) :
	"""handler gerant les requetes venant du client ou du serveur dropbox"""
	
	# il faut connaitre host et path pour savoir le type de requete
	path = str(url[2])

	# gestion de l authentification
	"""UTILE UNIQUEMENT TANT QUE OATH_TOKEN_SECRET N EST PAS CONNU DU PROXY, ie si le client n'a jamais fait login"""
	oauthParsed =parser.parseHeaders(clientHttpHeader)

	consumer_key = oauthParsed['Authorization']['oauth_consumer_key'][1:-1] # on identifie le client

	consumer_secret=ClientConsumerInfo(consumer_key,"id.conf").split()[1]
	sess = session_proxy.DropboxSession(consumer_key, consumer_secret, 'app_folder', locale=None)


	if path.split("/")[2] == "oauth" :
	# il s agit d un message d authentification

		if path.split("/")[3] == "request_token" :
		# client en est a etape 1 de l authentification (il a fait login)

			# transmission transparente de la requete a l aide du parsage de clientHttpMessage	
			response = sess.obtain_request_token_proxy()
			repread=response.read()
			request_token = oauth.OAuthToken.from_string(repread)

			#print request_token
			#print response.msg

			# stocker request_tocken dans le fichier de conf contenant les non-authorized token
			write_creds(consumer_key, request_token, "tokentemp.conf")

			# renvoyer le message a envoyer au client
			return response

		
		if path.split("/")[3] == "access_token" : 
			# client en est a etape 2 de l authentification
			#transmission transparente de la requete a l aide du parsage de clientHttpMessage
			#reconstruire en premier le token pour utiliser l'api (pas sur que ce soit utile)
			
			print "STEP 2"
Exemplo n.º 2
0
def ClientHandler(url,clientHttpHeader, readFile) :
	"""handler handling requests from a client"""
	
	path = str(url[2])

	"""identifying the client"""
	oauthParsed =parser.parseHeaders(clientHttpHeader)

	consumer_key = oauthParsed['Authorization']['oauth_consumer_key'][1:-1]
	info=ClientConsumerInfo(consumer_key,"id.conf")
	consumer_secret=info.split()[1]

	"""creating the db objects for the backup"""
	backup_sess = session_proxy.DropboxSession(backup_key, backup_secret, 'app_folder', locale=None)
	backup_sess.set_token(*backup_token.split('|'))

	"""creating a DropboxSession object for the client"""
	sess = session_proxy.DropboxSession(consumer_key, consumer_secret, 'app_folder', locale=None)
	"""load token if already exists"""
	stored_creds=info.split()[2]
	if stored_creds != "" :
		sess.set_token(*stored_creds.split('|'))



	if path.split("/")[2] == "oauth" :
		"""it is an login"""

		if path.split("/")[3] == "request_token" :
			"""step 1 of authentication"""

			response = sess.obtain_request_token_proxy()
			#repread=response.read()
			#request_token = oauth.OAuthToken.from_string(repread)

			"""storing the requested token in a file"""
			#write_creds(consumer_key, request_token, "tokentemp.conf")

			# renvoyer le message a envoyer au client
			return response

		
		if path.split("/")[3] == "access_token" : 
			"""step 2 of authentication"""

			print "step 2"
			#info=ClientConsumerInfo(consumer_key,"tokentemp.conf")
			#request_token_str="oauth_token_secret="+info.split()[2].split("|")[1]+"&oauth_token="+info.split()[2].split("|")[0]
			#request_token=oauth.OAuthToken.from_string(request_token_str)
			#new_request_token=sess.obtain_access_token_proxy(oauthParsed['oauth_timestamp'], oauthParsed['oauth_nonce'], oauthParsed['oauth_version'],request_token)

			"""storing the requested token in a file"""
			#write_creds(consumer_key, request_token, "id.conf")
		


	if path.split("/")[2] == "account" :
		"""client requested account_info""" 

		dbclient=client_proxy.DropboxClient(sess)
		response=dbclient.account_info()

		return response



	if path.split("/")[2] == "files_put" :
		"""the client has done a put_file"""

		body_length=oauthParsed["Content-Length"]

		"""check remaining free space"""
		checkRemainingSpace(sess)


		"""we first store the file"""
		"""we need to know where"""
		temp_path=str(consumer_key)+"/"+path[13:]
		to_path=path[13:]

		"""creating folder if it does not exist"""
		try :
			os.makedirs(os.path.split(temp_path)[0])

		except OSError:
			pass

		"""write readFile in a file"""
		with open(temp_path,"wb") as f :
			line = readFile.read(int(body_length))
			f.write(line)
		f.close()


		"""encryption of the file"""
		process.putFile(temp_path) 
		

		"""sending the encrypted file to dropbox"""
		with open(temp_path, "rb") as from_file :
			dbclient=client_proxy.DropboxClient(sess)
			response=dbclient.put_file(to_path,from_file)

		with open(temp_path, "rb") as from_file :
			"""send to the backup dropbox account"""
			backup_to_path='sandbox/'+temp_path

			backup_dbclient=client_proxy.DropboxClient(backup_sess)
			backup_dbclient.put_file(backup_to_path,from_file)

		"""destroying the temp file"""
		try :
			os.remove(temp_path)

		except OSError:
			pass

		return response


	if path.split("/")[2] == "files" :
		"""the client has done a get_file"""

		"""first get the file from dropbox"""
		from_path=path[9:]

		dbclient=client_proxy.DropboxClient(sess)
		dbresponse=dbclient.get_file(from_path)
		
		"""write the file locally"""
		local_path=str(consumer_key)+"/"+from_path

		try :
			os.makedirs(os.path.split(local_path)[0])
		except OSError:
			pass

		with open(local_path,"wb") as f :
			f.write(dbresponse.read())

		"""then check integrity and unencrytion"""
		process.getFile(local_path)

		"""finally send it to the client"""