2021-12-04 03:49:20 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
import gc
|
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
from flask import session
|
|
|
|
from flask_restx import Resource, Namespace, reqparse
|
2021-12-04 03:49:20 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from app.config import settings
|
|
|
|
from utilities.helper import check_credentials
|
2021-12-04 03:49:20 +00:00
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
api_ns_system_account = Namespace('System Account', description='Login or logout from Bazarr UI')
|
2021-12-04 03:49:20 +00:00
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
|
|
|
|
@api_ns_system_account.hide
|
|
|
|
@api_ns_system_account.route('system/account')
|
2021-12-04 03:49:20 +00:00
|
|
|
class SystemAccount(Resource):
|
2022-09-22 03:51:34 +00:00
|
|
|
post_request_parser = reqparse.RequestParser()
|
|
|
|
post_request_parser.add_argument('action', type=str, required=True, help='Action from ["login", "logout"]')
|
|
|
|
post_request_parser.add_argument('username', type=str, required=False, help='Bazarr username')
|
|
|
|
post_request_parser.add_argument('password', type=str, required=False, help='Bazarr password')
|
|
|
|
|
|
|
|
@api_ns_system_account.doc(parser=post_request_parser)
|
|
|
|
@api_ns_system_account.response(204, 'Success')
|
|
|
|
@api_ns_system_account.response(400, 'Unknown action')
|
|
|
|
@api_ns_system_account.response(404, 'Unknown authentication type define in config.ini')
|
2021-12-04 03:49:20 +00:00
|
|
|
def post(self):
|
2022-09-22 03:51:34 +00:00
|
|
|
"""Login or logout from Bazarr UI when using form login"""
|
|
|
|
args = self.patch_request_parser.parse_args()
|
2021-12-04 03:49:20 +00:00
|
|
|
if settings.auth.type != 'form':
|
2022-08-24 01:13:06 +00:00
|
|
|
return 'Unknown authentication type define in config.ini', 404
|
2021-12-04 03:49:20 +00:00
|
|
|
|
2022-09-22 03:51:34 +00:00
|
|
|
action = args.get('action')
|
2021-12-04 03:49:20 +00:00
|
|
|
if action == 'login':
|
2022-09-22 03:51:34 +00:00
|
|
|
username = args.get('username')
|
|
|
|
password = args.get('password')
|
2021-12-04 03:49:20 +00:00
|
|
|
if check_credentials(username, password):
|
|
|
|
session['logged_in'] = True
|
|
|
|
return '', 204
|
|
|
|
elif action == 'logout':
|
|
|
|
session.clear()
|
|
|
|
gc.collect()
|
|
|
|
return '', 204
|
|
|
|
|
2022-08-24 01:13:06 +00:00
|
|
|
return 'Unknown action', 400
|