From 26a79d15b494e8997e33b4c67ad43794a94fbe22 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Mon, 9 Nov 2020 11:01:50 +0100 Subject: [PATCH] Configurable keylen, rename SNIP_FLASK_SECRET - Length of snipped url is now configurable via `SNIP_KEYLEN` SNIP_FLASK_SECRET - is renamed to SNIP_SECRET as it is also used internally in snip, not just flask --- README.md | 2 +- snip/__init__.py | 2 +- snip/config.py | 3 ++- snip/snipper.py | 8 +++----- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1aabf15..e103071 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The simplest way to get started is to run snip from the regularily [published docker containers](https://hub.docker.com/repository/docker/arminfriedl/snip). ```shell -docker run -p5000 -e"SNIP_FLASK_SECRET=secretkey" arminfriedl/snip:latest +docker run -p5000 -e"SNIP_SECRET=secretkey" arminfriedl/snip:latest ``` Then navigate your browser to http://localhost:5000. Alternatively, you can also diff --git a/snip/__init__.py b/snip/__init__.py index 8caaa2f..116c67f 100644 --- a/snip/__init__.py +++ b/snip/__init__.py @@ -24,7 +24,7 @@ os.environ['FLASK_SKIP_DOTENV'] = str(snip_config.SNIP_FLASK_SKIP_DOTENV) app = Flask(__name__) app.config.update( - SECRET_KEY = snip_config.SNIP_FLASK_SECRET.get_secret_value(), + SECRET_KEY = snip_config.SNIP_SECRET.get_secret_value(), PREFERRED_URL_SCHEME = snip_config.SNIP_FLASK_PREFERRED_URL_SCHEME, SQLALCHEMY_DATABASE_URI = snip_config.SNIP_DATABASE_URI, SQLALCHEMY_TRACK_MODIFICATIONS = snip_config.SNIP_DATABASE_TRACK_MODIFICATION) diff --git a/snip/config.py b/snip/config.py index 4caf2b3..83f6974 100644 --- a/snip/config.py +++ b/snip/config.py @@ -39,7 +39,6 @@ class SnipConfig(BaseModel): SNIP_DATABASE_TRACK_MODIFICATION: bool = False # Flask settings - SNIP_FLASK_SECRET: SecretStr SNIP_FLASK_ENVIRONMENT: Literal['development', 'production'] = 'production' SNIP_FLASK_DEBUG: bool = False SNIP_FLASK_SKIP_DOTENV: int = 1 @@ -51,6 +50,8 @@ class SnipConfig(BaseModel): # Snip settings SNIP_STAGE: Optional[str] + SNIP_SECRET: SecretStr # also used as flask's SECRET_KEY + SNIP_KEYLEN: int = 5 def configure(stage: Optional[str] = None) -> SnipConfig: config_dict = {} diff --git a/snip/snipper.py b/snip/snipper.py index 1cf2e8e..01c9a68 100644 --- a/snip/snipper.py +++ b/snip/snipper.py @@ -1,3 +1,4 @@ +from . import snip_config from . import db from .models import Snip @@ -35,13 +36,10 @@ def snip(url: str, reusable=False) -> str: def gen_snip(): """ Generate a random snip """ - rand = secrets.token_bytes(5) + rand = secrets.token_bytes(snip_config.SNIP_KEYLEN) snip = str(base58.b58encode(rand), 'ascii') return snip def unsnip(snip: str): - snip_dao = Snip.query.filter(Snip.snip == snip).first() - if snip_dao: - return snip_dao.url - return None + return db.session.query(Snip.url).filter_by(snip=snip).scalar()