view_basics_api.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import Model.isp_model as isp_model
  2. from flask import session,request,jsonify
  3. import Model.model_manager as model_manager
  4. from werkzeug.security import check_password_hash,generate_password_hash
  5. import persistence
  6. import json
  7. import View.view_privilege as privileges
  8. import logging
  9. from utility.app_logging import logger_name
  10. logger = logging.getLogger(logger_name + ".VIEW")
  11. __api_login_url__ = "/api/login"
  12. __id_counter__ : int = 1
  13. def define_basic_api(app) :
  14. @app.before_request
  15. def before_request_func():
  16. global __id_counter__
  17. logger.debug("before_request processing")
  18. logger.debug("request from " + request.remote_addr)
  19. logger.debug("request header" + str(request.headers.__dict__))
  20. if request.json :
  21. logger.debug("request json body : " + str(request.json))
  22. if not "client_id" in session :
  23. session["client_id"] = str(__id_counter__)
  24. logger.debug("client_id is " + session["client_id"])
  25. __id_counter__ = __id_counter__ + 1
  26. if not request.path == __api_login_url__ and not "username" in session :
  27. logger.warning("Unauthorized client with id " + session["client_id"] + " try to access application")
  28. resp = jsonify({'message' : 'Unauthorized access, request is not from and authentificated user.'})
  29. resp.status_code = 401
  30. return resp
  31. if "username" in session :
  32. logger.debug("request from " + session["username"])
  33. @app.route(__api_login_url__,methods = ['POST'])
  34. def login():
  35. _json = request.json
  36. _username = _json['username']
  37. _password = _json['password']
  38. with persistence.get_Session_Instance() as sess :
  39. Item = sess.query(isp_model.user_account).filter(isp_model.user_account.nickname == _username).first()
  40. if not isinstance(Item,isp_model.user_account) :
  41. logger.warning("user tried to login with unknown account name : " + _username)
  42. resp = jsonify({'message' : 'Bad Request - user account not found'})
  43. resp.status_code = 400
  44. return resp
  45. if not check_password_hash(Item.password,_password) :
  46. logger.warning("user with account name '" + _username + "' tried to login with invalid password")
  47. resp = jsonify({'message' : 'Bad Request - invalid password for this account'})
  48. resp.status_code = 400
  49. return resp
  50. session["username"] = _username
  51. session["user_account_id"] = Item.id
  52. logger.info("account " + _username + " logged IN successfully with id : " + session["client_id"])
  53. resp = jsonify({'message' : 'login successful'})
  54. resp.status_code = 200
  55. return resp
  56. @app.route('/api/logout',methods = ['DELETE'])
  57. def logout():
  58. logger.info("account " + session["username"] + " logged OUT with id : " + session["client_id"])
  59. session.clear()
  60. return jsonify('logout')
  61. @app.route('/api/me',methods = ['GET'])
  62. def user_description():
  63. with persistence.get_Session_Instance() as sess :
  64. item : isp_model.user_account = sess.query(isp_model.user_account).filter(isp_model.user_account.id == session["client_id"]).first()
  65. json_string = model_manager.ModelObjectToJsonString(item)
  66. json_dict : dict = json.loads(json_string)
  67. json_dict.pop("password") # removing the password item for security
  68. json_string = json.dumps(json_dict)
  69. return jsonify(json_dict)
  70. @app.route('/api/password',methods = ['POST'])
  71. def change_password():
  72. _json = request.json
  73. _old_password = _json['old_password']
  74. _password = _json['new_password']
  75. with persistence.get_Session_Instance() as sess :
  76. Item : isp_model.user_account = sess.query(isp_model.user_account).filter(isp_model.user_account.id == session["user_account_id"]).first()
  77. if not check_password_hash(Item.password,_password) :
  78. raise Exception("old password is incorrect")
  79. Item.password = generate_password_hash(_password)
  80. sess.commit()
  81. return jsonify('password changed')
  82. @app.route('/routes',methods = ['GET'])
  83. def routes():
  84. routes = []
  85. for route in app.url_map.iter_rules():
  86. routes.append('%s' % route)
  87. return jsonify(routes)