def get(self): '''Verify user, and return a json string containing counter''' # Get username and password username = self.request.get("username","") password = self.request.get("password","") # Verify username if self.isUserExisted(username): user = UserList.query(UserList.Username == username).get() storedPassword = user.Password # Verify password if password == storedPassword: # Generate a random number as counter. randomCounter = random.randrange(10000000,99999999) # Assign counter to the user. user.Counter = str(randomCounter) user.put() # Send counter back to client. self.response.status = 200 self.response.body = json.dumps({"success": True, "counter": randomCounter}) self.response.content_type = 'application/json' else: # Send failure message. self.setFailResponse("Wrong Username/Password Combination") else: # Send failure message. self.setFailResponse("Wrong Username/Password Combination")
def post(self): '''Handle registeration request''' # Get parameters from request. username = self.request.get("username","") password = self.request.get("password","") secretKey = self.request.get("serialNumber","") # Add the user's details when no user existed with the same user name. if not self.isUserExisted(username): user = UserList() user.Username = username user.Password = password user.SecretKey = secretKey user.put() self.setSuccessResponse() # Deny registering when a duplicated user is found. else: self.denyIllegalAccess('Username Existed')
def isUserExisted(self, username): '''Check whether provided username exists''' # Directly try to get one record from ndb. # Expected to get None for True return UserList.query(UserList.Username == username).get() != None