LILiK login and user managment web interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.8 KiB

8 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env python3
  2. import lilikusers
  3. import logging
  4. import json
  5. from flask import Flask, jsonify
  6. from flask import request, Response, redirect, url_for
  7. from functools import wraps
  8. app = Flask(__name__)
  9. logrotate = logging.handlers.RotatingFileHandlers(
  10. '/var/log/login/server',
  11. backupCount=10,
  12. )
  13. app.logger.add_handler(logrotate)
  14. lilik_ldap = lilikusers.LILiK_LDAP()
  15. def check_auth(user_name, password):
  16. """This function is called to check if a username /
  17. password combination is valid.
  18. """
  19. if lilik_ldap.login(user_name, password):
  20. return lilik_ldap.get_user(user_name)
  21. app.logger.warning('Invalid login for <{}>'.format(user_name))
  22. raise ValueError
  23. def authenticate():
  24. """Sends a 401 response that enables basic auth"""
  25. app.logger.debug('Request http basic auth')
  26. return Response(
  27. 'Could not verify your access level for that URL.\n'
  28. 'You have to login with proper credentials\n', 401)
  29. def admin_required():
  30. """Sends a 401 response that enables basic auth"""
  31. app.logger.debug('Request admin level')
  32. return Response(
  33. 'Could not verify your access level for that URL.\n'
  34. 'You have to login with admin rights\n', 401)
  35. def requires_auth(f):
  36. @wraps(f)
  37. def decorated(*args, **kwargs):
  38. auth = request.authorization
  39. if not auth:
  40. return authenticate()
  41. try:
  42. user = check_auth(auth.username, auth.password)
  43. except ValueError:
  44. app.logger.warning('Authentication failed for <{}>'.format(auth.username))
  45. return authenticate()
  46. return f(user, *args, **kwargs)
  47. return decorated
  48. def requires_admin_auth(f):
  49. @wraps(f)
  50. def decorated(user, *args, **kwargs):
  51. if not user.services['admin']:
  52. app.logger.warning('Admin privilege required for user <{}>'.format(user.uid))
  53. return admin_required()
  54. return f(user, *args, **kwargs)
  55. return decorated
  56. def requires_same_user_or_admin_auth(f):
  57. @wraps(f)
  58. def decorated(user, user_name, *args, **kwargs):
  59. if not user.services['admin'] and user.uid != user_name:
  60. app.logger.warning('Admin privilege required for user <{}>'.format(user.uid))
  61. return admin_required()
  62. return f(user, user_name, *args, **kwargs)
  63. return decorated
  64. @app.route('/api/users', methods=['GET'])
  65. @requires_auth
  66. @requires_admin_auth
  67. def get_users(user):
  68. ''' return the list of users'''
  69. return jsonify(lilik_ldap.get_users())
  70. @app.route('/api/users/<user_name>', methods=['GET'])
  71. @requires_auth
  72. @requires_same_user_or_admin_auth
  73. def get_user(user, user_name):
  74. ''' return the list of users'''
  75. return jsonify(lilik_ldap.get_user(user_name).to_dict())
  76. @app.route('/api/users/<user_name>', methods=['PUT'])
  77. @requires_auth
  78. @requires_same_user_or_admin_auth
  79. def update_user(user, user_name):
  80. new_lilik_user = request.get_json()
  81. app.logger.info('User <{}> editing user <{}>'.format(user.uid, user_name))
  82. user_to_edit = lilik_ldap.get_user(user_name)
  83. diff = user_to_edit.diff(new_lilik_user)
  84. is_permitted_self_changes = diff.changed() <= set(['cn']) and diff.removed() == set() and diff.added() <= set(['userPassword'])
  85. if user.services['admin'] or is_permitted_self_changes:
  86. user_to_edit.update(new_lilik_user)
  87. return jsonify(lilik_ldap.get_user(user_name).to_dict())
  88. @app.route('/api/users', methods=['POST'])
  89. @requires_auth
  90. @requires_admin_auth
  91. def new_user(user):
  92. app.logger.info('User <{}> create a new user'.format(user.uid))
  93. new_lilik_user = request.get_json()
  94. app.logger.info(lilik_ldap.new_user(new_lilik_user))
  95. return jsonify(lilik_ldap.get_user(new_lilik_user['uid']).to_dict())
  96. @app.route('/')
  97. def home():
  98. return redirect(url_for('static', filename='index.html'))
  99. if __name__ == '__main__':
  100. app.run(debug=True)