|
|
- #!/bin/usr/env python3
- import lilikusers
- import json
- from flask import Flask, jsonify
- from flask import request, Response
- from functools import wraps
-
- app = Flask(__name__)
-
- lilik_ldap = lilikusers.LILiK_LDAP()
-
- def check_auth(user_name, password):
- """This function is called to check if a username /
- password combination is valid.
- """
- return lilik_ldap.login(user_name, password)
-
- def authenticate():
- """Sends a 401 response that enables basic auth"""
- return Response(
- 'Could not verify your access level for that URL.\n'
- 'You have to login with proper credentials', 401,
- {'WWW-Authenticate': 'Basic realm="Login Required"'})
-
- def requires_auth(f):
- @wraps(f)
- def decorated(*args, **kwargs):
- auth = request.authorization
- if not auth or not check_auth(auth.username, auth.password):
- return authenticate()
- return f(*args, **kwargs)
- return decorated
-
- @app.route('/api/users', methods=['GET'])
- @requires_auth
- def get_users():
- ''' return the list of users'''
- return jsonify(lilik_ldap.get_users())
-
- @app.route('/api/user/<user_name>', methods=['GET'])
- @requires_auth
- def get_user(user_name):
- ''' return the list of users'''
- return jsonify(lilik_ldap.get_user(user_name).to_dict())
-
- @app.route('/api/user/<user_name>', methods=['POST'])
- @requires_auth
- def post_user(self, user_name):
- new_lilik_user = request.get_json()
- return lilik_ldap.get_user(user_name).update(new_lilik_user)
-
- if __name__ == '__main__':
- app.run(debug=True)
|