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.

53 lines
1.5 KiB

  1. #!/bin/usr/env python3
  2. import lilikusers
  3. import json
  4. from flask import Flask, jsonify
  5. from flask import request, Response
  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. return lilik_ldap.login(user_name, password)
  14. def authenticate():
  15. """Sends a 401 response that enables basic auth"""
  16. return Response(
  17. 'Could not verify your access level for that URL.\n'
  18. 'You have to login with proper credentials', 401,
  19. {'WWW-Authenticate': 'Basic realm="Login Required"'})
  20. def requires_auth(f):
  21. @wraps(f)
  22. def decorated(*args, **kwargs):
  23. auth = request.authorization
  24. if not auth or not check_auth(auth.username, auth.password):
  25. return authenticate()
  26. return f(*args, **kwargs)
  27. return decorated
  28. @app.route('/api/users', methods=['GET'])
  29. @requires_auth
  30. def get_users():
  31. ''' return the list of users'''
  32. return jsonify(lilik_ldap.get_users())
  33. @app.route('/api/user/<user_name>', methods=['GET'])
  34. @requires_auth
  35. def get_user(user_name):
  36. ''' return the list of users'''
  37. return jsonify(lilik_ldap.get_user(user_name).to_dict())
  38. @app.route('/api/user/<user_name>', methods=['POST'])
  39. @requires_auth
  40. def post_user(self, user_name):
  41. new_lilik_user = request.get_json()
  42. return lilik_ldap.get_user(user_name).update(new_lilik_user)
  43. if __name__ == '__main__':
  44. app.run(debug=True)