Beispiel #1
0
def register(request):
	if 'reg_data' in request.POST:
		message = request.POST['reg_data']
		message = rsa_pubkey.rsa_decrypt(message)

		info_dict=json.loads(message.strip())
		print info_dict
		new_user=User(name=info_dict['uname'],
					  email=info_dict['email'],
					  pword=info_dict['pword'],
					 )
		try:
			new_user.save()
			message='200 OK'
		except Exception,e:
			print e
			message='203 DataBase Error'
Beispiel #2
0
def login(request):
	if 'login_data' in request.POST and 'method' in request.POST:
		message = request.POST['login_data']
		method = request.POST['method']
		message = rsa_pubkey.rsa_decrypt(message)
		info_dict=json.loads(message.strip())
		#print info_dict
		uname=info_dict['uname']
		pword=info_dict['pword']
		#print uname,pword
		try:
			user=User.objects.get(name=uname)
			if(method == 'afts'):
				print user,"ask for time stamp"
				if(user.pword==pword):
					message='__TIMESTAMP__'+random_str(5)+str(int(time.time()))
					try:
						ts=TimeStamp.objects.get(name=uname)
						ts.timeStamp=message
						ts.save()
						message='200 '+message
					except:
						try:
							ts=TimeStamp(name=uname,timeStamp=message)
							ts.save()
						except:
							message = "333 System Error!"
				else:
					message="201 Password Error!"
			elif(method == 'login'):
				print user,"want login"
				now=int(time.time())
				try:
					ts=TimeStamp.objects.get(name=uname)
					pre=int(ts.timeStamp[len('__TIMESTAMP__')+5:])
					if(now-pre<32):
						_MD5=md5()
						_MD5.update(user.pword+ts.timeStamp)
						if(pword==_MD5.hexdigest()):
							message="__TOKEN__"+random_str(24)
							try:
								tk=Token.objects.get(name=uname)
								tk.token=message
								tk.timeStamp=int(time.time())
								tk.save()
								message='200 '+message
							except:
								try:
									tk=Token(name=uname,token=message,timeStamp=int(time.time()))
									tk.save()
								except:
									message='333 System Error!'
						else:
							message='201 username/password Error'
					else:
						message="202 TimeStamp Overtime!"
						
				except Exception,e:
					print e
					message="202 TimeStamp Error!"
			else:
				message = "999 Unknown Method"