How to create a 'require_auth' decorator without inform a token model
I have two applications, the first one is an OAuth 2.0 server and the second one is an isolated application, present in another repository.
The second application connects to OAuth 2.0 database to check if the token is it valid, this is done by the decorator @require_auth, that to be created requires a token model that by its turn requires the user model:
from authlib.flask.oauth2.sqla import create_bearer_token_validator from authlib.flask.oauth2 import ResourceProtector from db import db class User(db.Model): __bind_key__ = 'oauth' # Database name __tablename__ = 'oauth2_user' # Table name # ... class Token(db.Model): __bind_key__ = 'oauth2' # Database name __tablename__ = 'oauth2_token' # Table name id = db.Column(db.BigInteger, primary_key=True) user_id = db.Column(sqlalchemy_utils.UUIDType(binary=True), db.ForeignKey('oauth2_user.id', ondelete='SET NULL')) user = db.relationship('User', cascade=False) require_oauth = ResourceProtector() bearer = create_bearer_token_validator(db.session, Token) # https://github.com/lepture/authlib/blob/8234da2896c724e3c7c021984175d99f48ef5a70/authlib/flask/oauth2/sqla.py#L306 require_oauth.register_token_validator(bearer())
As the architecture is based on microservices, to create this decorator, I'm duplicating code from OAuth 2.0 (token model and user model) in the second application, besides that, because the fact that I need to connect to two databases on the second application and the user and token tables are duplicated in the second application, when I generate the migrations of the second application, the SQLAlchemy track them and also generates it's structure on the second application' database, but when a token is checked the OAuth 2.0 tables are used to check the token, even if I inform which database I want to connect to
__bind_key__ = 'oauth2', for more details see this thread.
How can I create a
require_auth decorator to use in another application without inform a token model ?
Obs: I have tried to get the Meta Data of the token model from the OAuth 2.0 database
db.Model.tables, but I receive an empty immutable dict
Obs ²: I generate the migrations using Flask Migrate application
Obs ⁴: I'm now considering try to set it manualy using Alembic or creating two connections and generate the tables by
Extra: SQLalchemy orm skip already-existing tables on create, but in my context I need to ignorate a table that don't exist in the database.