def signin(): if request.method == 'POST': if 'id' in session: # print("already signed in") return '', 200 name = request.form.get("name", None) password = request.form.get("password", None) errorMsg = None if name is None: errorMsg = "name" elif password is None: errorMsg = "password" if errorMsg is not None: return json.dumps({"message": errorMsg + " is not provided"}), 400 password = hashing.hash_value(password) user = User.query.filter_by(name=name).first() if user is None: return json.dumps({"message": "user not registered"}), 404 if user.password != password: return json.dumps({"message": "password incorrect"}), 404 session["id"] = user.user_id resp = make_response() return resp else: return json.dumps({"msg": "GET"})
def config(): # This is the default pull config d = app.config['PULL_DEFAULTS'].copy() # Ensure that all values are accounted from url args # to create key to find config if request.args.get('guid') is None or \ request.args.get('hostname') is None or \ request.args.get('domain') is None: return render_template('errors/404.html'), 400 key = request.args['guid'] + ',' + request.args['hostname'] + ',' + request.args['domain'] key = hashing.hash_value(key, salt= app.config['HASH_SALT']) # Contains the config changes for this particular client config = mongo.db.clientConfigs.find_one({'key': key}, projection={'_id': False, 'key': False}) # Modify the the configuration accordingly isDefault = True if config is not None: isDefault = False for k,v in config.items(): d[k] = v return render_template( 'config.html', guid=request.args['guid'], hostname=request.args['hostname'], domain=request.args['domain'], config=d, isDefault=isDefault )
def editConfig(): # Ensure that all values are accounted from url args # to edit the corresponding config if request.args.get('guid') is None or \ request.args.get('hostname') is None or \ request.args.get('domain') is None: return render_template('errors/404.html'), 400 key = request.args['guid'] + ',' + request.args['hostname'] + ',' + request.args['domain'] key = hashing.hash_value(key, salt= app.config['HASH_SALT']) form=EditConfigField(key) d = app.config['PULL_DEFAULTS'] if form.validate_on_submit(): configChanges = changes(d, form.toDict()) if len(configChanges) > 0: mongo.db.clientConfigs.update_one( {'key': key}, update={'$set': configChanges}, upsert=True ) else: mongo.db.clientConfigs.delete_one({'key': key}) return redirect( url_for('config') + '?guid=' + request.args['guid'] + '&hostname=' + request.args['hostname'] + '&domain=' + request.args['domain'] ) elif request.method == 'GET': data = mongo.db.clientConfigs.find_one({'key': key}, projection={'_id': False, 'key': False}) if data is not None: for k,v in data.items(): d[k] = v form.config_interval.data = d.get('config_interval') form.FileVersionMS.data = d.get('FileVersionMS') form.logs.data = ','.join(d.get('logs')) form.cpu_interval.data = d.get('cpu_interval') form.FileVersionLS.data = d.get('FileVersionLS') form.data_url.data = d.get('data_url') form.num.data = d.get('num') form.MonitorPath.data = d.get('MonitorPath') form.debug.data = d.get('debug') form.upload_interval.data = d.get('upload_interval') return render_template( 'edit_config.html', form=form, hostname=request.args['hostname'], domain=request.args['domain'] )
def resetConfig(): # Ensure that all values are accounted from url args # to edit the corresponding config if request.args.get('guid') is None or \ request.args.get('hostname') is None or \ request.args.get('domain') is None: return render_template('errors/404.html'), 400 key = request.args['guid'] + ',' + request.args['hostname'] + ',' + request.args['domain'] key = hashing.hash_value(key, salt= app.config['HASH_SALT']) mongo.db.clientConfigs.delete_one({'key': key}) return redirect( url_for('config') + '?guid=' + request.args['guid'] + '&hostname=' + request.args['hostname'] + '&domain=' + request.args['domain'] )
def signup(): if request.method == 'POST': if 'id' in session: # print("already signed up") session.pop('name', None) name = request.form.get("name", None) email = request.form.get("email", None) phone = request.form.get("phone", None) password = request.form.get("password", None) errorMsg = None if name is None: errorMsg = "name" elif email is None: errorMsg = "email" elif password is None: errorMsg = "password" if errorMsg is not None: return json.dumps({ "message": errorMsg + " is not provided" }), 400 password = hashing.hash_value(password) user = User(name=name, email=email, phone=phone, password=password) try: user.insert() except sqlalchemy.exc.IntegrityError as e: return json.dumps({ "message": "user name registered" }), 409 session["id"] = user.user_id resp = make_response() return resp else: return json.dumps({"msg": "GET"})
def push(): if not request.is_json: return 'Bad request. Not JSON.', 400 # Ensure that that the POST body has the minimum required values d = request.get_json() if d['post'].get('guid') is None or \ d['post'].get('hostname') is None or \ d['post'].get('domain') is None: return 'Bad request', 400 guid = d['post'].get('guid') hostname = d['post'].get('hostname') domain = d['post'].get('domain') uploadTime = d['post'].get('UploadTime') key = guid + ',' + hostname + ',' + domain key = hashing.hash_value(key, salt= app.config['HASH_SALT']) dataToSet = { 'guid':guid, 'hostname':hostname, 'domain':domain, 'uploadTime':uploadTime, 'data':request.get_data(), } # Record the incoming data serialized result = mongo.db.agentData.update_one( filter={'key': key}, update={'$set': dataToSet}, upsert=True ) if result.upserted_id is not None: print("I'm in!") add_to_index(index='agentdata', key=key, doc={'hostname':hostname, 'domain':domain}) return 'Okay', 200
def pull(): # This is the default pull config d = app.config['PULL_DEFAULTS'].copy() # Ensure that all values are accounted from url args # to create key to find config or else send default config if request.args.get('guid') is None or \ request.args.get('hostname') is None or \ request.args.get('domain') is None: return jsonify(d), 200 key = request.args['guid'] + ',' + request.args['hostname'] + ',' + request.args['domain'] key = hashing.hash_value(key, salt= app.config['HASH_SALT']) # Contains the config changes for this particular client config = mongo.db.clientConfigs.find_one({'key': key}, projection={'_id': False, 'key': False}) # Modify the the configuration accordingly if config is not None: for k,v in config.items(): d[k] = v return jsonify(d), 200
def fake_data(): mixer.init_app(app) mixer.cycle(50).blend(User, password=hashing.hash_value("123456")) mixer.cycle(10).blend(Cinema, info=mixer.sequence("info_{0}")) mixer.cycle(10).blend(Movie, length=(a for a in range(95, 126)))
def hash_password(raw_pwd, salt): raw_pwd += app.config['PEPPER'] return hashing.hash_value(raw_pwd, salt=salt)