78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
|
#############################################
|
||
|
# Plugin: Auth #
|
||
|
# Functionality: Allow management of #
|
||
|
# authentification, login, logout, etc #
|
||
|
#############################################
|
||
|
|
||
|
from flask import Blueprint, current_app, request, jsonify
|
||
|
from werkzeug.local import LocalProxy
|
||
|
|
||
|
from flaschengeist.system.decorator import login_required
|
||
|
from flaschengeist.system.exceptions import PermissionDenied
|
||
|
from flaschengeist.system.controller import mainController as mc
|
||
|
import flaschengeist.system.controller.accesTokenController as ac
|
||
|
|
||
|
logger = LocalProxy(lambda: current_app.logger)
|
||
|
accesTokenController = LocalProxy(lambda: ac.AccesTokenController())
|
||
|
|
||
|
auth_bp = Blueprint('auth', __name__)
|
||
|
|
||
|
def register():
|
||
|
return auth_bp
|
||
|
|
||
|
############################################
|
||
|
## Routes ##
|
||
|
############################################
|
||
|
|
||
|
@auth_bp.route("/logout", methods=['GET'])
|
||
|
@login_required()
|
||
|
def _logout(**kwargs):
|
||
|
try:
|
||
|
logger.debug("logout user")
|
||
|
accToken = kwargs['accToken']
|
||
|
logger.debug("accesstoken is {{ {} }}".format(accToken))
|
||
|
logger.debug("delete accesstoken")
|
||
|
accesTokenController.deleteAccessToken(accToken)
|
||
|
logger.info("return ok logout user")
|
||
|
return jsonify({"ok": "ok"})
|
||
|
except Exception as err:
|
||
|
logger.warning("exception in logout user.", exc_info=True)
|
||
|
return jsonify({"error": str(err)}), 500
|
||
|
|
||
|
|
||
|
@auth_bp.route("/login", methods=['POST'])
|
||
|
def _login():
|
||
|
""" Login User
|
||
|
|
||
|
Nothing to say.
|
||
|
Login in User and create an AccessToken for the User.
|
||
|
|
||
|
Returns:
|
||
|
A JSON-File with createt Token or Errors
|
||
|
"""
|
||
|
logger.debug("Start log in.")
|
||
|
data = request.get_json()
|
||
|
logger.info(request)
|
||
|
username = data['username']
|
||
|
password = data['password']
|
||
|
logger.debug("username is {{ {} }}".format(username))
|
||
|
try:
|
||
|
logger.debug("search {{ {} }} in database".format(username))
|
||
|
mainController = mc.MainController()
|
||
|
user = mainController.loginUser(username, password)
|
||
|
logger.debug("user is {{ {} }}".format(user))
|
||
|
token = accesTokenController.createAccesToken(user, user_agent=request.user_agent)
|
||
|
logger.debug("accesstoken is {{ {} }}".format(token))
|
||
|
logger.debug("validate accesstoken")
|
||
|
dic = user.toJSON()
|
||
|
dic["token"] = token
|
||
|
logger.info("User {{ {} }} success login.".format(username))
|
||
|
logger.debug("return login {{ {} }}".format(dic))
|
||
|
return jsonify(dic)
|
||
|
except PermissionDenied as err:
|
||
|
logger.debug("permission denied exception in login", exc_info=True)
|
||
|
return jsonify({"error": str(err)}), 401
|
||
|
except Exception as err:
|
||
|
logger.error("exception in login.", exc_info=True)
|
||
|
return jsonify({"error": "permission denied"}), 401
|