2019-05-02 23:40:13 +00:00
|
|
|
from geruecht import app, db, accesTokenController, MONEY, BAR, USER, GASTRO, LOGGER
|
2019-12-19 07:12:29 +00:00
|
|
|
from geruecht import ldapController as ldap
|
2019-04-11 21:56:55 +00:00
|
|
|
from geruecht.model.user import User
|
|
|
|
from flask import request, jsonify
|
|
|
|
|
2019-12-19 17:26:41 +00:00
|
|
|
def login(user, password):
|
|
|
|
return user.login(password)
|
|
|
|
|
2019-04-11 21:56:55 +00:00
|
|
|
def verifyAccessToken(token, group):
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Verify Accestoken
|
|
|
|
|
|
|
|
Verify an Accestoken and Group so if the User has permission or not.
|
|
|
|
Retrieves the accestoken if valid else retrieves None
|
|
|
|
|
|
|
|
Args:
|
|
|
|
token: Token to verify.
|
|
|
|
group: Group like 'moneymaster', 'gastro', 'user' or 'bar'
|
|
|
|
Returns:
|
2019-05-01 20:43:28 +00:00
|
|
|
An the AccesToken for this given Token or None.
|
2019-04-17 12:46:46 +00:00
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Verify AccessToken with token: {} and group: {}".format(token, group))
|
2019-04-11 21:56:55 +00:00
|
|
|
accToken = accesTokenController.findAccesToken(token)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("AccessToken is {}".format(accToken))
|
2019-04-11 21:56:55 +00:00
|
|
|
if accToken is not None:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Check if AccesToken {} has same group {}".format(accToken, group))
|
2019-04-11 21:56:55 +00:00
|
|
|
if accesTokenController.isSameGroup(accToken, group):
|
|
|
|
accToken.updateTimestamp()
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Found AccessToken {} with token: {} and group: {}".format(accToken, token, group))
|
2019-04-11 21:56:55 +00:00
|
|
|
return accToken
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("No AccessToken with token: {} and group: {} found".format(token, group))
|
2019-04-11 21:56:55 +00:00
|
|
|
return None
|
|
|
|
|
2019-04-23 22:08:25 +00:00
|
|
|
@app.route("/valid")
|
2019-04-12 12:51:37 +00:00
|
|
|
def _valid():
|
2019-04-23 22:08:25 +00:00
|
|
|
token = request.headers.get("Token")
|
2019-04-12 12:51:37 +00:00
|
|
|
accToken = verifyAccessToken(token, MONEY)
|
2019-04-23 22:08:25 +00:00
|
|
|
if accToken is not None:
|
|
|
|
return jsonify(accToken.user.toJSON())
|
|
|
|
accToken = verifyAccessToken(token, BAR)
|
|
|
|
if accToken is not None:
|
|
|
|
return jsonify(accToken.user.toJSON())
|
|
|
|
accToken = verifyAccessToken(token, GASTRO)
|
|
|
|
if accToken is not None:
|
|
|
|
return jsonify(accToken.user.toJSON())
|
|
|
|
accToken = verifyAccessToken(token, USER)
|
2019-04-12 12:51:37 +00:00
|
|
|
if accToken is not None:
|
|
|
|
return jsonify(accToken.user.toJSON())
|
|
|
|
return jsonify({"error": "permission denied"}), 401
|
|
|
|
|
2019-04-11 21:56:55 +00:00
|
|
|
@app.route("/login", methods=['POST'])
|
|
|
|
def _login():
|
2019-04-17 12:46:46 +00:00
|
|
|
""" Login User
|
2019-05-01 20:43:28 +00:00
|
|
|
|
2019-04-17 12:46:46 +00:00
|
|
|
Nothing to say.
|
|
|
|
Login in User and create an AccessToken for the User.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A JSON-File with createt Token or Errors
|
|
|
|
"""
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("Start log in.")
|
2019-04-11 21:56:55 +00:00
|
|
|
data = request.get_json()
|
2019-12-22 21:27:39 +00:00
|
|
|
print(data)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("JSON from request: {}".format(data))
|
2019-04-11 21:56:55 +00:00
|
|
|
username = data['username']
|
|
|
|
password = data['password']
|
2019-12-19 07:12:29 +00:00
|
|
|
LOGGER.info("search {} in database".format(username))
|
|
|
|
user = db.getUser(username)
|
|
|
|
if user is None:
|
|
|
|
LOGGER.info("User {} not found. Authenticate over LDAP and create User.")
|
|
|
|
try:
|
|
|
|
ldap.login(username, password)
|
|
|
|
LOGGER.info("Authentification successfull. Search Group")
|
2019-12-22 21:27:39 +00:00
|
|
|
groups = ldap.getGroup(username)
|
2019-12-19 07:12:29 +00:00
|
|
|
LOGGER.info("Get userdata from LDAP")
|
|
|
|
user_data = ldap.getUserData(username)
|
2019-12-22 21:27:39 +00:00
|
|
|
user_data['group'] = groups
|
2019-12-19 07:12:29 +00:00
|
|
|
LOGGER.info('Insert user {} into database')
|
|
|
|
db.insertUser(user_data)
|
|
|
|
|
|
|
|
except Exception as err:
|
2019-12-22 21:27:39 +00:00
|
|
|
return jsonify({"error": str(err)}), 401
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("{} try to log in".format(username))
|
2019-12-19 07:12:29 +00:00
|
|
|
user = db.getUser(username)
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("User is {}".format(user))
|
2019-04-11 21:56:55 +00:00
|
|
|
if user:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.debug("Check login for User {}".format(user))
|
2019-12-19 17:26:41 +00:00
|
|
|
if login(user, password):
|
2019-04-11 21:56:55 +00:00
|
|
|
token = accesTokenController.createAccesToken(user)
|
|
|
|
dic = user.toJSON()
|
|
|
|
dic["token"] = token
|
2019-12-22 21:27:39 +00:00
|
|
|
dic["accessToken"] = token
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("User {} success login.".format(username))
|
2019-04-12 12:51:37 +00:00
|
|
|
return jsonify(dic)
|
2019-01-14 23:25:42 +00:00
|
|
|
else:
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("User {} failed login.".format(username))
|
2019-04-11 21:56:55 +00:00
|
|
|
return jsonify({"error": "wrong password"}), 401
|
2019-05-02 23:40:13 +00:00
|
|
|
LOGGER.info("User {} does not exist.".format(username))
|
2019-05-01 20:43:28 +00:00
|
|
|
return jsonify({"error": "wrong username"}), 402
|
2019-04-11 21:56:55 +00:00
|
|
|
|
|
|
|
@app.route("/getFinanzer")
|
|
|
|
def getFinanzer():
|
|
|
|
users = User.query.all()
|
|
|
|
dic = {}
|
|
|
|
for user in users:
|
2019-04-23 18:26:20 +00:00
|
|
|
dic[user.userID] = user.toJSON()
|
2019-04-11 21:56:55 +00:00
|
|
|
print(dic)
|
|
|
|
return jsonify(dic)
|