Ejemplo n.º 1
0
    def post(self):
        try:
            db = self.settings['db']
            username = self.get_argument("u", "")
            password = self.get_argument("p", "")
            Logmgr.PYLOG("LoginHandler", {
                "username": username,
                "password": password
            })
            if username == '' or password == '':
                self.write(
                    JsonHelper.failWithMessage("Empty username or password"))
                return

            res = yield db.accounts.find_one({"username": username})
            if not res or not ("password"
                               in res) or res['password'] != password:
                self.write(
                    JsonHelper.failWithMessage("Wrong username or password"))
                return

            new_token = str(uuid.uuid1())
            result = yield db.accounts.update({"username": username},
                                              {'$set': {
                                                  'token': new_token
                                              }})
            if result['ok'] == 1:
                respond_data = {"oid": str(res['_id']), "token": new_token}
                Logmgr.PYLOG("LoginHandler", JsonHelper.success(respond_data))
                self.write(JsonHelper.success(respond_data))
        except Exception, e:
            if DEBUG:
                print Exception, ":", e
                traceback.print_exc()
            self.write(JsonHelper.failWithMessage("Internal error."))
Ejemplo n.º 2
0
    def post(self):
        try:
            db = self.settings['db']
            username = self.get_argument("u", "")
            password = self.get_argument("p", "")
            # print self.request.body
            Logmgr.PYLOG("RegistHandler", {
                "username": username,
                "password": password
            })
            if username == '' or password == '':
                self.write(
                    JsonHelper.failWithMessage("Empty username or password"))
                return

            res = yield db.accounts.find_one({"username": username})
            # TODO handle db failure
            if res:
                self.write(
                    JsonHelper.failWithMessage("Username already exists"))
                return

            new_token = str(uuid.uuid1())
            oid = bson.ObjectId()
            result = yield db.accounts.insert({
                "_id": oid,
                "username": username,
                "password": password,
                'token': new_token
            })
            if result == oid:
                respond_data = {"oid": str(oid), "token": new_token}
                Logmgr.PYLOG("RegistHandler", JsonHelper.success(respond_data))
                self.write(JsonHelper.success(respond_data))
        except Exception, e:
            if DEBUG:
                print Exception, ":", e
                traceback.print_exc()
            self.write(JsonHelper.failWithMessage("Internal error."))