Fix opds login not working anymore (due to kobo sync protocol)

This commit is contained in:
Ozzieisaacs 2020-02-16 14:25:15 +01:00
parent 317e59df4b
commit e3ddc16657
3 changed files with 37 additions and 23 deletions

View File

@ -43,6 +43,7 @@ import requests
from . import config, logger, kobo_auth, db, helper
from .services import SyncToken as SyncToken
from .web import download_required
from .kobo_auth import requires_kobo_auth
KOBO_FORMATS = {"KEPUB": ["KEPUB"], "EPUB": ["EPUB3", "EPUB"]}
KOBO_STOREAPI_URL = "https://storeapi.kobo.com"
@ -72,8 +73,8 @@ CONNECTION_SPECIFIC_HEADERS = [
def get_kobo_activated():
return config.config_kobo_sync
def redirect_or_proxy_request():
if config.config_kobo_proxy:
def redirect_or_proxy_request(proxy=False):
if config.config_kobo_proxy or proxy == True:
if request.method == "GET":
return redirect(get_store_url_for_current_request(), 307)
if request.method == "DELETE":
@ -102,7 +103,7 @@ def redirect_or_proxy_request():
return make_response(jsonify({}))
@kobo.route("/v1/library/sync")
@login_required
@requires_kobo_auth
@download_required
def HandleSyncRequest():
sync_token = SyncToken.SyncToken.from_headers(request.headers)
@ -193,7 +194,7 @@ def generate_sync_response(request, sync_token, entitlements):
@kobo.route("/v1/library/<book_uuid>/metadata")
@login_required
@requires_kobo_auth
@download_required
def HandleMetadataRequest(book_uuid):
if not current_app.wsgi_app.is_proxied:
@ -347,7 +348,7 @@ def reading_state(book):
@kobo.route("/<book_uuid>/image.jpg")
@login_required
@requires_kobo_auth
def HandleCoverImageRequest(book_uuid):
log.debug("Cover request received for book %s" % book_uuid)
book_cover = helper.get_book_cover_with_uuid(
@ -393,9 +394,13 @@ def handle_404(err):
log.debug("Unknown Request received: %s", request.base_url)
return redirect_or_proxy_request()
@kobo.route("/v1/auth/device", methods=["POST"])
def login_auth_token():
log.info('Auth')
return redirect_or_proxy_request(proxy=True)
@kobo.route("/v1/initialization")
@login_required
@requires_kobo_auth
def HandleInitRequest():
if not current_app.wsgi_app.is_proxied:
log.debug('Kobo: Received unproxied request, changed request port to server port')

View File

@ -61,13 +61,19 @@ from binascii import hexlify
from datetime import datetime
from os import urandom
from flask import g, Blueprint, url_for
from flask import g, Blueprint, url_for, abort
from flask_login import login_user, login_required
from flask_babel import gettext as _
from . import logger, ub, lm
from .web import render_title_template
try:
from functools import wraps
except ImportError:
pass # We're not using Python 3
log = logger.create()
@ -88,8 +94,9 @@ def get_auth_token():
return None
@lm.request_loader
def load_user_from_kobo_request(request):
def requires_kobo_auth(f):
@wraps(f)
def inner(*args, **kwargs):
auth_token = get_auth_token()
if auth_token is not None:
user = (
@ -100,9 +107,11 @@ def load_user_from_kobo_request(request):
)
if user is not None:
login_user(user)
return user
return f(*args, **kwargs)
log.debug("Received Kobo request without a recognizable auth token.")
return
return abort(401)
return inner
kobo_auth = Blueprint("kobo_auth", __name__, url_prefix="/kobo_auth")

View File

@ -8,7 +8,7 @@
{{_('api_endpoint=')}}{{kobo_auth_url}}</a>
</p>
<p>
{{_('Please note that every visit to this current page invalidates any previously generated Authentication url for this user.')}}</a>.
{{_('Please note that every visit to this current page invalidates any previously generated Authentication url for this user.')}}</a>
</p>
</div>
{% endblock %}