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.

108 lines
3.6 KiB

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