def add_token(self,user,token): token_map=self.db.find_one(DB.TOKEN_TABLE,{DB.USER_NAME_KEY:user}) if not token_map: token_map={DB.USER_NAME_KEY:user,DB.TOKEN_KEY:token,DB.MD5_KEY:MD5.get(token)} self.db.insert(DB.TOKEN_TABLE,token_map) if token != token_map[DB.TOKEN_KEY]: token_map[DB.TOKEN_KEY]=token token_map[DB.MD5_KEY]=MD5.get(token) self.db.update(DB.TOKEN_TABLE,{DB.USER_NAME_KEY:user},token_map)
def _add_token(self): if not request.json: return jsonify({"error":HTTP_BAD_REQUEST_STR}),HTTP_BAD_REQUEST username=request.json[RestServer.USER_NAME] token_id=self.token_mgr.find_token_id(username) if not token_id: self.log.info("username %s find token failed, request keystone"%(username)) token=self.auth.get_token(username,request.json[RestServer.PASSWORD]) if not token: self.log.error("request token from keystone failed.") return jsonify({RestServer.RESULT:HTTP_UNAUTHORIZED_STR}),HTTP_UNAUTHORIZED self.token_mgr.add_token(username,token) token_id=MD5.get(token) return jsonify({RestServer.TOKEN:token_id}),HTTP_OK