Use Flask-Hashids package

This commit is contained in:
Patrick Jentsch 2021-12-08 11:25:33 +01:00
parent e1004a0181
commit 63527da37f
5 changed files with 14 additions and 10 deletions

View File

@ -6,14 +6,13 @@ from flask_migrate import Migrate
from flask_paranoid import Paranoid from flask_paranoid import Paranoid
from flask_socketio import SocketIO from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from hashids import Hashids from flask_hashids import Hashids
import flask_assets import flask_assets
assets: flask_assets.Environment = flask_assets.Environment() assets: flask_assets.Environment = flask_assets.Environment()
db: SQLAlchemy = SQLAlchemy() db: SQLAlchemy = SQLAlchemy()
# TODO: Add 'SECRET_KEY' from as 'salt' kwarg hashids: Hashids = Hashids()
hashids: Hashids = Hashids(min_length=32)
login: LoginManager = LoginManager() login: LoginManager = LoginManager()
login.login_view: str = 'auth.login' login.login_view: str = 'auth.login'
login.login_message: str = 'Please log in to access this page.' login.login_message: str = 'Please log in to access this page.'
@ -24,14 +23,15 @@ paranoid.redirect_view: str = '/'
socketio: SocketIO = SocketIO() socketio: SocketIO = SocketIO()
def create_app(config_class: Config = Config) -> Flask: def create_app(config: Config = Config) -> Flask:
''' Creates an initialized Flask (WSGI Application) object. ''' ''' Creates an initialized Flask (WSGI Application) object. '''
app: Flask = Flask(__name__) app: Flask = Flask(__name__)
app.config.from_object(config_class) app.config.from_object(config)
assets.init_app(app) assets.init_app(app)
config_class.init_app(app) config.init_app(app)
db.init_app(app) db.init_app(app)
hashids.init_app(app)
login.init_app(app) login.init_app(app)
mail.init_app(app) mail.init_app(app)
migrate.init_app(app, db) migrate.init_app(app, db)
@ -39,8 +39,8 @@ def create_app(config_class: Config = Config) -> Flask:
socketio.init_app( socketio.init_app(
app, message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI']) app, message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI'])
from .utils import HashidConverter # from .utils import HashidConverter
app.url_map.converters['hashid'] = HashidConverter # app.url_map.converters['hashid'] = HashidConverter
from .events import socketio as socketio_events from .events import socketio as socketio_events
from .events import sqlalchemy as sqlalchemy_events from .events import sqlalchemy as sqlalchemy_events

View File

@ -57,7 +57,7 @@ from .cqi import * # noqa
def connect(auth): def connect(auth):
# the auth variable is used in a hacky way. It contains the corpus id for # the auth variable is used in a hacky way. It contains the corpus id for
# which a corpus analysis session should be started. # which a corpus analysis session should be started.
corpus_id = hashids.decode(auth['corpus_id'])[0] corpus_id = hashids.decode(auth['corpus_id'])
corpus = Corpus.query.get(corpus_id) corpus = Corpus.query.get(corpus_id)
if corpus is None: if corpus is None:
# return {'code': 404, 'msg': 'Not Found'} # return {'code': 404, 'msg': 'Not Found'}

View File

@ -12,7 +12,7 @@ from ..models import User
@socketio.on('users.user.get') @socketio.on('users.user.get')
@socketio_login_required @socketio_login_required
def users_user_get(user_hashid): def users_user_get(user_hashid):
user_id = hashids.decode(user_hashid)[0] user_id = hashids.decode(user_hashid)
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None: if user is None:
response = {'code': 404, 'msg': 'Not found'} response = {'code': 404, 'msg': 'Not found'}

View File

@ -19,6 +19,9 @@ class Config:
TEMPLATES_AUTO_RELOAD = \ TEMPLATES_AUTO_RELOAD = \
os.environ.get('TEMPLATES_AUTO_RELOAD', 'false').lower() == 'true' os.environ.get('TEMPLATES_AUTO_RELOAD', 'false').lower() == 'true'
''' # Flask-Hashids '''
HASHIDS_MIN_LENGTH = 32
''' # Flask-Login # ''' ''' # Flask-Login # '''
REMEMBER_COOKIE_HTTPONLY = True REMEMBER_COOKIE_HTTPONLY = True
REMEMBER_COOKIE_SECURE = \ REMEMBER_COOKIE_SECURE = \

View File

@ -3,6 +3,7 @@ docker
eventlet==0.30.2 eventlet==0.30.2
Flask~=1.1 Flask~=1.1
Flask-Assets Flask-Assets
Flask-Hashids
Flask-HTTPAuth Flask-HTTPAuth
Flask-Login Flask-Login
Flask-Mail Flask-Mail