auth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #-*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from flask import request, g, abort, render_template, redirect
  16. from flask_babel import refresh
  17. import requests
  18. import json
  19. import string
  20. import random
  21. from rrd import app
  22. from rrd import config
  23. from rrd.model.user import User
  24. from rrd.view import utils as view_utils
  25. from rrd.utils.logger import logging
  26. log = logging.getLogger(__file__)
  27. def id_generator(size=16, chars=string.ascii_uppercase + string.digits):
  28. return ''.join(random.choice(chars) for _ in range(size))
  29. @app.route("/auth/login", methods=["GET", "POST"])
  30. def auth_login():
  31. if request.method == "GET":
  32. if g.user:
  33. return redirect("/")
  34. return render_template("auth/login.html", **locals())
  35. if request.method == "POST":
  36. ret = { "msg": "", }
  37. name = request.form.get("name")
  38. password = request.form.get("password")
  39. ldap = request.form.get("ldap") or "0"
  40. if not name or not password:
  41. ret["msg"] = "no name or password"
  42. return json.dumps(ret)
  43. if ldap == "1":
  44. try:
  45. ldap_info = view_utils.ldap_login_user(name, password)
  46. password = id_generator()
  47. user_info = {
  48. "name": name,
  49. "password": password,
  50. "cnname": ldap_info['cnname'],
  51. "email": ldap_info['email'],
  52. "phone": ldap_info['phone'],
  53. }
  54. Apitoken = view_utils.get_Apitoken(config.API_USER, config.API_PASS)
  55. ut = view_utils.admin_login_user(name, Apitoken)
  56. if not ut:
  57. view_utils.create_user(user_info)
  58. ut = view_utils.admin_login_user(name, Apitoken)
  59. #if user not exist, create user , signup must be enabled
  60. ret["data"] = {
  61. "name": ut.name,
  62. "sig": ut.sig,
  63. }
  64. return json.dumps(ret)
  65. except Exception as e:
  66. ret["msg"] = str(e)
  67. return json.dumps(ret)
  68. try:
  69. ut = view_utils.login_user(name, password)
  70. if not ut:
  71. ret["msg"] = "no such user"
  72. return json.dumps(ret)
  73. ret["data"] = {
  74. "name": ut.name,
  75. "sig": ut.sig,
  76. }
  77. return json.dumps(ret)
  78. except Exception as e:
  79. ret["msg"] = str(e)
  80. return json.dumps(ret)
  81. @app.route("/auth/logout", methods=["GET",])
  82. def auth_logout():
  83. if request.method == "GET":
  84. view_utils.logout_user(g.user_token)
  85. return redirect("/auth/login")
  86. @app.route("/auth/register", methods=["GET", "POST"])
  87. def auth_register():
  88. if request.method == "GET":
  89. if g.user:
  90. return redirect("/auth/login")
  91. return render_template("auth/register.html", **locals())
  92. if request.method == "POST":
  93. ret = {"msg":""}
  94. name = request.form.get("name", "").strip()
  95. cnname = request.form.get("cnname", "").strip()
  96. email = request.form.get("email", "").strip()
  97. password = request.form.get("password", "")
  98. repeat_password = request.form.get("repeat_password", "")
  99. if not name or not password or not email or not cnname:
  100. ret["msg"] = "not all form item entered"
  101. return json.dumps(ret)
  102. if password != repeat_password:
  103. ret["msg"] = "repeat password not equal"
  104. return json.dumps(ret)
  105. h = {"Content-type":"application/json"}
  106. d = {
  107. "name": name,
  108. "cnname": cnname,
  109. "email": email,
  110. "password": password,
  111. }
  112. r = requests.post("%s/user/create" %(config.API_ADDR,), \
  113. data=json.dumps(d), headers=h)
  114. log.debug("%s:%s" %(r.status_code, r.text))
  115. if r.status_code != 200:
  116. ret['msg'] = r.text
  117. return json.dumps(ret)