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 . import config, logger, kobo_auth, db, helper
from .services import SyncToken as SyncToken from .services import SyncToken as SyncToken
from .web import download_required from .web import download_required
from .kobo_auth import requires_kobo_auth
KOBO_FORMATS = {"KEPUB": ["KEPUB"], "EPUB": ["EPUB3", "EPUB"]} KOBO_FORMATS = {"KEPUB": ["KEPUB"], "EPUB": ["EPUB3", "EPUB"]}
KOBO_STOREAPI_URL = "https://storeapi.kobo.com" KOBO_STOREAPI_URL = "https://storeapi.kobo.com"
@ -72,8 +73,8 @@ CONNECTION_SPECIFIC_HEADERS = [
def get_kobo_activated(): def get_kobo_activated():
return config.config_kobo_sync return config.config_kobo_sync
def redirect_or_proxy_request(): def redirect_or_proxy_request(proxy=False):
if config.config_kobo_proxy: if config.config_kobo_proxy or proxy == True:
if request.method == "GET": if request.method == "GET":
return redirect(get_store_url_for_current_request(), 307) return redirect(get_store_url_for_current_request(), 307)
if request.method == "DELETE": if request.method == "DELETE":
@ -102,7 +103,7 @@ def redirect_or_proxy_request():
return make_response(jsonify({})) return make_response(jsonify({}))
@kobo.route("/v1/library/sync") @kobo.route("/v1/library/sync")
@login_required @requires_kobo_auth
@download_required @download_required
def HandleSyncRequest(): def HandleSyncRequest():
sync_token = SyncToken.SyncToken.from_headers(request.headers) 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") @kobo.route("/v1/library/<book_uuid>/metadata")
@login_required @requires_kobo_auth
@download_required @download_required
def HandleMetadataRequest(book_uuid): def HandleMetadataRequest(book_uuid):
if not current_app.wsgi_app.is_proxied: if not current_app.wsgi_app.is_proxied:
@ -347,7 +348,7 @@ def reading_state(book):
@kobo.route("/<book_uuid>/image.jpg") @kobo.route("/<book_uuid>/image.jpg")
@login_required @requires_kobo_auth
def HandleCoverImageRequest(book_uuid): def HandleCoverImageRequest(book_uuid):
log.debug("Cover request received for book %s" % book_uuid) log.debug("Cover request received for book %s" % book_uuid)
book_cover = helper.get_book_cover_with_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) log.debug("Unknown Request received: %s", request.base_url)
return redirect_or_proxy_request() 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") @kobo.route("/v1/initialization")
@login_required @requires_kobo_auth
def HandleInitRequest(): def HandleInitRequest():
if not current_app.wsgi_app.is_proxied: if not current_app.wsgi_app.is_proxied:
log.debug('Kobo: Received unproxied request, changed request port to server port') 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 datetime import datetime
from os import urandom 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_login import login_user, login_required
from flask_babel import gettext as _ from flask_babel import gettext as _
from . import logger, ub, lm from . import logger, ub, lm
from .web import render_title_template from .web import render_title_template
try:
from functools import wraps
except ImportError:
pass # We're not using Python 3
log = logger.create() log = logger.create()
@ -88,21 +94,24 @@ def get_auth_token():
return None return None
@lm.request_loader def requires_kobo_auth(f):
def load_user_from_kobo_request(request): @wraps(f)
auth_token = get_auth_token() def inner(*args, **kwargs):
if auth_token is not None: auth_token = get_auth_token()
user = ( if auth_token is not None:
ub.session.query(ub.User) user = (
.join(ub.RemoteAuthToken) ub.session.query(ub.User)
.filter(ub.RemoteAuthToken.auth_token == auth_token).filter(ub.RemoteAuthToken.token_type==1) .join(ub.RemoteAuthToken)
.first() .filter(ub.RemoteAuthToken.auth_token == auth_token).filter(ub.RemoteAuthToken.token_type==1)
) .first()
if user is not None: )
login_user(user) if user is not None:
return user login_user(user)
log.debug("Received Kobo request without a recognizable auth token.") return f(*args, **kwargs)
return log.debug("Received Kobo request without a recognizable auth token.")
return abort(401)
return inner
kobo_auth = Blueprint("kobo_auth", __name__, url_prefix="/kobo_auth") kobo_auth = Blueprint("kobo_auth", __name__, url_prefix="/kobo_auth")

View File

@ -8,7 +8,7 @@
{{_('api_endpoint=')}}{{kobo_auth_url}}</a> {{_('api_endpoint=')}}{{kobo_auth_url}}</a>
</p> </p>
<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> </p>
</div> </div>
{% endblock %} {% endblock %}