2019-07-13 20:45:48 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-07-16 21:08:23 +02:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 jim3ma
|
|
|
|
#
|
|
|
|
# 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/>
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
from flask import session
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
from flask_dance.consumer.backend.sqla import SQLAlchemyBackend, first, _get_real_user
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2020-04-16 17:58:42 +02:00
|
|
|
backend_resultcode = False # prevent storing values with this resultcode
|
2020-04-14 18:28:16 +02:00
|
|
|
except ImportError:
|
|
|
|
# fails on flask-dance >1.3, due to renaming
|
|
|
|
try:
|
|
|
|
from flask_dance.consumer.storage.sqla import SQLAlchemyStorage as SQLAlchemyBackend
|
|
|
|
from flask_dance.consumer.storage.sqla import first, _get_real_user
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2020-04-19 19:08:58 +02:00
|
|
|
backend_resultcode = True # prevent storing values with this resultcode
|
2020-04-14 18:28:16 +02:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2020-04-14 18:28:16 +02:00
|
|
|
try:
|
2019-07-13 20:45:48 +02:00
|
|
|
class OAuthBackend(SQLAlchemyBackend):
|
|
|
|
"""
|
|
|
|
Stores and retrieves OAuth tokens using a relational database through
|
|
|
|
the `SQLAlchemy`_ ORM.
|
|
|
|
|
|
|
|
.. _SQLAlchemy: http://www.sqlalchemy.org/
|
|
|
|
"""
|
2019-07-21 08:10:23 +02:00
|
|
|
def __init__(self, model, session, provider_id,
|
2019-07-13 20:45:48 +02:00
|
|
|
user=None, user_id=None, user_required=None, anon_user=None,
|
|
|
|
cache=None):
|
2019-07-21 08:10:23 +02:00
|
|
|
self.provider_id = provider_id
|
2019-07-13 20:45:48 +02:00
|
|
|
super(OAuthBackend, self).__init__(model, session, user, user_id, user_required, anon_user, cache)
|
|
|
|
|
|
|
|
def get(self, blueprint, user=None, user_id=None):
|
2019-07-21 08:10:23 +02:00
|
|
|
if self.provider_id + '_oauth_token' in session and session[self.provider_id + '_oauth_token'] != '':
|
2020-04-16 17:58:42 +02:00
|
|
|
return session[self.provider_id + '_oauth_token']
|
2019-07-13 20:45:48 +02:00
|
|
|
# check cache
|
|
|
|
cache_key = self.make_cache_key(blueprint=blueprint, user=user, user_id=user_id)
|
|
|
|
token = self.cache.get(cache_key)
|
|
|
|
if token:
|
|
|
|
return token
|
|
|
|
|
|
|
|
# if not cached, make database queries
|
|
|
|
query = (
|
|
|
|
self.session.query(self.model)
|
2019-07-21 08:10:23 +02:00
|
|
|
.filter_by(provider=self.provider_id)
|
2019-07-13 20:45:48 +02:00
|
|
|
)
|
|
|
|
uid = first([user_id, self.user_id, blueprint.config.get("user_id")])
|
|
|
|
u = first(_get_real_user(ref, self.anon_user)
|
|
|
|
for ref in (user, self.user, blueprint.config.get("user")))
|
|
|
|
|
|
|
|
use_provider_user_id = False
|
2019-07-21 08:10:23 +02:00
|
|
|
if self.provider_id + '_oauth_user_id' in session and session[self.provider_id + '_oauth_user_id'] != '':
|
|
|
|
query = query.filter_by(provider_user_id=session[self.provider_id + '_oauth_user_id'])
|
2019-07-13 20:45:48 +02:00
|
|
|
use_provider_user_id = True
|
|
|
|
|
|
|
|
if self.user_required and not u and not uid and not use_provider_user_id:
|
2020-04-15 19:57:33 +02:00
|
|
|
# raise ValueError("Cannot get OAuth token without an associated user")
|
2019-07-13 20:45:48 +02:00
|
|
|
return None
|
|
|
|
# check for user ID
|
|
|
|
if hasattr(self.model, "user_id") and uid:
|
|
|
|
query = query.filter_by(user_id=uid)
|
|
|
|
# check for user (relationship property)
|
|
|
|
elif hasattr(self.model, "user") and u:
|
|
|
|
query = query.filter_by(user=u)
|
|
|
|
# if we have the property, but not value, filter by None
|
|
|
|
elif hasattr(self.model, "user_id"):
|
|
|
|
query = query.filter_by(user_id=None)
|
|
|
|
# run query
|
|
|
|
try:
|
|
|
|
token = query.one().token
|
|
|
|
except NoResultFound:
|
|
|
|
token = None
|
|
|
|
|
|
|
|
# cache the result
|
|
|
|
self.cache.set(cache_key, token)
|
|
|
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
def set(self, blueprint, token, user=None, user_id=None):
|
|
|
|
uid = first([user_id, self.user_id, blueprint.config.get("user_id")])
|
|
|
|
u = first(_get_real_user(ref, self.anon_user)
|
2020-04-19 19:08:58 +02:00
|
|
|
for ref in (user, self.user, blueprint.config.get("user")))
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
if self.user_required and not u and not uid:
|
|
|
|
raise ValueError("Cannot set OAuth token without an associated user")
|
|
|
|
|
|
|
|
# if there was an existing model, delete it
|
|
|
|
existing_query = (
|
|
|
|
self.session.query(self.model)
|
2019-07-21 08:10:23 +02:00
|
|
|
.filter_by(provider=self.provider_id)
|
2019-07-13 20:45:48 +02:00
|
|
|
)
|
|
|
|
# check for user ID
|
|
|
|
has_user_id = hasattr(self.model, "user_id")
|
|
|
|
if has_user_id and uid:
|
|
|
|
existing_query = existing_query.filter_by(user_id=uid)
|
|
|
|
# check for user (relationship property)
|
|
|
|
has_user = hasattr(self.model, "user")
|
|
|
|
if has_user and u:
|
|
|
|
existing_query = existing_query.filter_by(user=u)
|
|
|
|
# queue up delete query -- won't be run until commit()
|
|
|
|
existing_query.delete()
|
|
|
|
# create a new model for this token
|
|
|
|
kwargs = {
|
2019-07-21 08:10:23 +02:00
|
|
|
"provider": self.provider_id,
|
2019-07-13 20:45:48 +02:00
|
|
|
"token": token,
|
|
|
|
}
|
|
|
|
if has_user_id and uid:
|
|
|
|
kwargs["user_id"] = uid
|
|
|
|
if has_user and u:
|
|
|
|
kwargs["user"] = u
|
|
|
|
self.session.add(self.model(**kwargs))
|
|
|
|
# commit to delete and add simultaneously
|
|
|
|
self.session.commit()
|
|
|
|
# invalidate cache
|
|
|
|
self.cache.delete(self.make_cache_key(
|
|
|
|
blueprint=blueprint, user=user, user_id=user_id
|
|
|
|
))
|
|
|
|
|
|
|
|
def delete(self, blueprint, user=None, user_id=None):
|
|
|
|
query = (
|
|
|
|
self.session.query(self.model)
|
2019-07-21 08:10:23 +02:00
|
|
|
.filter_by(provider=self.provider_id)
|
2019-07-13 20:45:48 +02:00
|
|
|
)
|
|
|
|
uid = first([user_id, self.user_id, blueprint.config.get("user_id")])
|
|
|
|
u = first(_get_real_user(ref, self.anon_user)
|
|
|
|
for ref in (user, self.user, blueprint.config.get("user")))
|
|
|
|
|
|
|
|
if self.user_required and not u and not uid:
|
|
|
|
raise ValueError("Cannot delete OAuth token without an associated user")
|
|
|
|
|
|
|
|
# check for user ID
|
|
|
|
if hasattr(self.model, "user_id") and uid:
|
|
|
|
query = query.filter_by(user_id=uid)
|
|
|
|
# check for user (relationship property)
|
|
|
|
elif hasattr(self.model, "user") and u:
|
|
|
|
query = query.filter_by(user=u)
|
|
|
|
# if we have the property, but not value, filter by None
|
|
|
|
elif hasattr(self.model, "user_id"):
|
|
|
|
query = query.filter_by(user_id=None)
|
|
|
|
# run query
|
|
|
|
query.delete()
|
|
|
|
self.session.commit()
|
|
|
|
# invalidate cache
|
|
|
|
self.cache.delete(self.make_cache_key(
|
|
|
|
blueprint=blueprint, user=user, user_id=user_id,
|
|
|
|
))
|
|
|
|
|
2020-04-14 18:28:16 +02:00
|
|
|
except Exception:
|
2019-07-13 20:45:48 +02:00
|
|
|
pass
|