mirror of
https://github.com/janeczku/calibre-web
synced 2024-11-24 18:47:23 +00:00
Handle kobo auth request
Handle access from localhost for kobo
This commit is contained in:
parent
ba6b5f8fd1
commit
6893635251
29
cps/kobo.py
29
cps/kobo.py
@ -18,6 +18,8 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import base64
|
||||||
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from time import gmtime, strftime
|
from time import gmtime, strftime
|
||||||
try:
|
try:
|
||||||
@ -394,10 +396,31 @@ 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"])
|
@kobo.route("/v1/auth/device", methods=["POST"])
|
||||||
def login_auth_token():
|
@requires_kobo_auth
|
||||||
log.info('Auth')
|
def HandleAuthRequest():
|
||||||
return redirect_or_proxy_request(proxy=True)
|
# Missing feature: Authentication :)
|
||||||
|
log.debug('Kobo Auth request')
|
||||||
|
content = request.get_json()
|
||||||
|
AccessToken = base64.b64encode(os.urandom(24)).decode('utf-8')
|
||||||
|
RefreshToken = base64.b64encode(os.urandom(24)).decode('utf-8')
|
||||||
|
if config.config_kobo_proxy:
|
||||||
|
return redirect_or_proxy_request(proxy=True)
|
||||||
|
else:
|
||||||
|
response = make_response(
|
||||||
|
jsonify(
|
||||||
|
{
|
||||||
|
"AccessToken": AccessToken,
|
||||||
|
"RefreshToken": RefreshToken,
|
||||||
|
"TokenType": "Bearer",
|
||||||
|
"TrackingId": str(uuid.uuid4()),
|
||||||
|
"UserKey": content['UserKey'],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@kobo.route("/v1/initialization")
|
@kobo.route("/v1/initialization")
|
||||||
@requires_kobo_auth
|
@requires_kobo_auth
|
||||||
|
@ -60,8 +60,9 @@ particular calls to non-Kobo specific endpoints such as the CalibreWeb book down
|
|||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from os import urandom
|
from os import urandom
|
||||||
|
import os
|
||||||
|
|
||||||
from flask import g, Blueprint, url_for, abort
|
from flask import g, Blueprint, url_for, abort, request
|
||||||
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 _
|
||||||
|
|
||||||
@ -119,28 +120,37 @@ kobo_auth = Blueprint("kobo_auth", __name__, url_prefix="/kobo_auth")
|
|||||||
@kobo_auth.route("/generate_auth_token/<int:user_id>")
|
@kobo_auth.route("/generate_auth_token/<int:user_id>")
|
||||||
@login_required
|
@login_required
|
||||||
def generate_auth_token(user_id):
|
def generate_auth_token(user_id):
|
||||||
# Invalidate any prevously generated Kobo Auth token for this user.
|
host = ':'.join(request.host.rsplit(':')[0:-1])
|
||||||
auth_token = ub.session.query(ub.RemoteAuthToken).filter(
|
if host == '127.0.0.1' or host.lower() == 'localhost' or host =='[::ffff:7f00:1]':
|
||||||
ub.RemoteAuthToken.user_id == user_id
|
warning = _('PLease access calibre-web from non localhost to get valid api_endpoint for kobo device')
|
||||||
).filter(ub.RemoteAuthToken.token_type==1).first()
|
return render_title_template(
|
||||||
|
"generate_kobo_auth_url.html",
|
||||||
|
title=_(u"Kobo Set-up"),
|
||||||
|
warning = warning
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Invalidate any prevously generated Kobo Auth token for this user.
|
||||||
|
auth_token = ub.session.query(ub.RemoteAuthToken).filter(
|
||||||
|
ub.RemoteAuthToken.user_id == user_id
|
||||||
|
).filter(ub.RemoteAuthToken.token_type==1).first()
|
||||||
|
|
||||||
if not auth_token:
|
if not auth_token:
|
||||||
auth_token = ub.RemoteAuthToken()
|
auth_token = ub.RemoteAuthToken()
|
||||||
auth_token.user_id = user_id
|
auth_token.user_id = user_id
|
||||||
auth_token.expiration = datetime.max
|
auth_token.expiration = datetime.max
|
||||||
auth_token.auth_token = (hexlify(urandom(16))).decode("utf-8")
|
auth_token.auth_token = (hexlify(urandom(16))).decode("utf-8")
|
||||||
auth_token.token_type = 1
|
auth_token.token_type = 1
|
||||||
|
|
||||||
ub.session.add(auth_token)
|
ub.session.add(auth_token)
|
||||||
ub.session.commit()
|
ub.session.commit()
|
||||||
|
return render_title_template(
|
||||||
return render_title_template(
|
"generate_kobo_auth_url.html",
|
||||||
"generate_kobo_auth_url.html",
|
title=_(u"Kobo Set-up"),
|
||||||
title=_(u"Kobo Set-up"),
|
kobo_auth_url=url_for(
|
||||||
kobo_auth_url=url_for(
|
"kobo.TopLevelEndpoint", auth_token=auth_token.auth_token, _external=True
|
||||||
"kobo.TopLevelEndpoint", auth_token=auth_token.auth_token, _external=True
|
),
|
||||||
),
|
warning = False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@kobo_auth.route("/deleteauthtoken/<int:user_id>")
|
@kobo_auth.route("/deleteauthtoken/<int:user_id>")
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
{% block body %}
|
{% block body %}
|
||||||
<div class="well">
|
<div class="well">
|
||||||
<p>
|
<p>
|
||||||
{{_('Open the .kobo/Kobo eReader.conf file in a text editor and add (or edit):')}}</a>.
|
{{_('Open the .kobo/Kobo eReader.conf file in a text editor and add (or edit):')}}</a>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
{{_('api_endpoint=')}}{{kobo_auth_url}}</a>
|
{% if not warning %}{{_('api_endpoint=')}}{{kobo_auth_url}}{% else %}{{warning}}{% endif %}</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>
|
||||||
|
Loading…
Reference in New Issue
Block a user