2020-12-12 10:23:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import json
|
|
|
|
from datetime import datetime
|
2022-03-13 11:34:21 +00:00
|
|
|
from functools import wraps
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
from flask import Blueprint, request, make_response, abort, url_for, flash, redirect
|
2024-07-14 14:24:07 +00:00
|
|
|
from .cw_login import login_user, current_user
|
2020-12-12 10:23:17 +00:00
|
|
|
from flask_babel import gettext as _
|
2020-12-13 08:53:18 +00:00
|
|
|
from sqlalchemy.sql.expression import true
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
from . import config, logger, ub
|
|
|
|
from .render_template import render_title_template
|
2024-07-14 14:24:07 +00:00
|
|
|
from .usermanagement import user_login_required
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
remotelogin = Blueprint('remotelogin', __name__)
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
|
|
def remote_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if config.config_remote_login:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
|
|
|
data = {'status': 'error', 'message': 'Forbidden'}
|
|
|
|
response = make_response(json.dumps(data, ensure_ascii=False))
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response, 403
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
@remotelogin.route('/remote/login')
|
|
|
|
@remote_login_required
|
|
|
|
def remote_login():
|
|
|
|
auth_token = ub.RemoteAuthToken()
|
|
|
|
ub.session.add(auth_token)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2020-12-13 08:53:18 +00:00
|
|
|
verify_url = url_for('remotelogin.verify_token', token=auth_token.auth_token, _external=true)
|
2023-01-21 14:19:59 +00:00
|
|
|
log.debug("Remot Login request with token: %s", auth_token.auth_token)
|
|
|
|
return render_title_template('remote_login.html', title=_("Login"), token=auth_token.auth_token,
|
2020-12-12 10:23:17 +00:00
|
|
|
verify_url=verify_url, page="remotelogin")
|
|
|
|
|
|
|
|
|
|
|
|
@remotelogin.route('/verify/<token>')
|
|
|
|
@remote_login_required
|
2024-07-14 14:24:07 +00:00
|
|
|
@user_login_required
|
2020-12-12 10:23:17 +00:00
|
|
|
def verify_token(token):
|
|
|
|
auth_token = ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.auth_token == token).first()
|
|
|
|
|
|
|
|
# Token not found
|
|
|
|
if auth_token is None:
|
2023-01-21 14:19:59 +00:00
|
|
|
flash(_("Token not found"), category="error")
|
|
|
|
log.error("Remote Login token not found")
|
2020-12-12 10:23:17 +00:00
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
# Token expired
|
2021-01-03 08:53:34 +00:00
|
|
|
elif datetime.now() > auth_token.expiration:
|
2020-12-12 10:23:17 +00:00
|
|
|
ub.session.delete(auth_token)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2020-12-12 10:23:17 +00:00
|
|
|
|
2023-01-21 14:19:59 +00:00
|
|
|
flash(_("Token has expired"), category="error")
|
|
|
|
log.error("Remote Login token expired")
|
2020-12-12 10:23:17 +00:00
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
# Update token with user information
|
|
|
|
auth_token.user_id = current_user.id
|
|
|
|
auth_token.verified = True
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2020-12-12 10:23:17 +00:00
|
|
|
|
2023-01-21 14:19:59 +00:00
|
|
|
flash(_("Success! Please return to your device"), category="success")
|
|
|
|
log.debug("Remote Login token for userid %s verified", auth_token.user_id)
|
2020-12-12 10:23:17 +00:00
|
|
|
return redirect(url_for('web.index'))
|
|
|
|
|
|
|
|
|
|
|
|
@remotelogin.route('/ajax/verify_token', methods=['POST'])
|
|
|
|
@remote_login_required
|
|
|
|
def token_verified():
|
|
|
|
token = request.form['token']
|
|
|
|
auth_token = ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.auth_token == token).first()
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
# Token not found
|
|
|
|
if auth_token is None:
|
|
|
|
data['status'] = 'error'
|
2023-01-21 14:19:59 +00:00
|
|
|
data['message'] = _("Token not found")
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
# Token expired
|
|
|
|
elif datetime.now() > auth_token.expiration:
|
|
|
|
ub.session.delete(auth_token)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
data['status'] = 'error'
|
2023-01-21 14:19:59 +00:00
|
|
|
data['message'] = _("Token has expired")
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
elif not auth_token.verified:
|
|
|
|
data['status'] = 'not_verified'
|
|
|
|
|
|
|
|
else:
|
|
|
|
user = ub.session.query(ub.User).filter(ub.User.id == auth_token.user_id).first()
|
|
|
|
login_user(user)
|
|
|
|
|
|
|
|
ub.session.delete(auth_token)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("User {} logged in via remotelogin, token deleted".format(user.name))
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
data['status'] = 'success'
|
2023-01-21 14:19:59 +00:00
|
|
|
log.debug("Remote Login for userid %s succeeded", user.id)
|
|
|
|
flash(_("Success! You are now logged in as: %(nickname)s", nickname=user.name), category="success")
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
response = make_response(json.dumps(data, ensure_ascii=False))
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
|
|
|
|
return response
|