From 11b797d535043977276aea010fa7a214f035b336 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Fri, 3 Dec 2021 10:56:57 -0800 Subject: [PATCH] Add global settings endpoint (#52) * backend: - add /api/settings endpoint for misc system-wide settings - setting 'registrationEnabled' if open registration should be enabled, set via REGISTRATION_ENABLED=1 env var - setting 'jwtTokenLifetimeMinutes' returns the jwt token expiry in seconds, configured in minutes via JWT_TOKEN_LIFETIME_MINUTES env var (default: 60) --- backend/main.py | 11 ++++++++++- backend/users.py | 4 ++-- chart/templates/configmap.yaml | 5 +++++ chart/values.yaml | 6 ++++++ configs/config.sample.env | 5 +++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/backend/main.py b/backend/main.py index 5553820f..2e4da859 100644 --- a/backend/main.py +++ b/backend/main.py @@ -10,7 +10,7 @@ from fastapi import FastAPI from db import init_db from emailsender import EmailSender -from users import init_users_api, init_user_manager +from users import init_users_api, init_user_manager, JWT_TOKEN_LIFETIME from archives import init_archives_api from storages import init_storages_api @@ -30,6 +30,11 @@ def main(): mdb = init_db() + settings = { + "registrationEnabled": os.environ.get("REGISTRATION_ENABLED") == "1", + "jwtTokenLifetime": JWT_TOKEN_LIFETIME + } + user_manager = init_user_manager(mdb, email) fastapi_users = init_users_api(app, user_manager) @@ -74,6 +79,10 @@ def main(): app.include_router(archive_ops.router) + @app.get("/api/settings") + async def get_settings(): + return settings + @app.get("/healthz") async def healthz(): return {} diff --git a/backend/users.py b/backend/users.py index c4368014..48a0a2e3 100644 --- a/backend/users.py +++ b/backend/users.py @@ -23,7 +23,7 @@ from fastapi_users.db import MongoDBUserDatabase # ============================================================================ PASSWORD_SECRET = os.environ.get("PASSWORD_SECRET", uuid.uuid4().hex) -JWT_LIFETIME = int(os.environ.get("JWT_LIFETIME", 3600)) +JWT_TOKEN_LIFETIME = int(os.environ.get("JWT_TOKEN_LIFETIME_MINUTES", 60)) * 60 # ============================================================================ @@ -185,7 +185,7 @@ def init_users_api(app, user_manager): """ init fastapi_users """ jwt_authentication = JWTAuthentication( secret=PASSWORD_SECRET, - lifetime_seconds=JWT_LIFETIME, + lifetime_seconds=JWT_TOKEN_LIFETIME, tokenUrl="/auth/jwt/login", ) diff --git a/chart/templates/configmap.yaml b/chart/templates/configmap.yaml index aecb5b7e..529667c3 100644 --- a/chart/templates/configmap.yaml +++ b/chart/templates/configmap.yaml @@ -23,6 +23,11 @@ data: NO_DELETE_JOBS: "{{ .Values.no_delete_jobs | default '0' }}" + REGISTRATION_ENABLED: "{{ .Values.registration_enabled | default '0' }}" + + JWT_TOKEN_LIFETIME_MINUTES: "{{ .Values.jwt_token_lifetime_minutes | default '60' }}" + + --- apiVersion: v1 kind: ConfigMap diff --git a/chart/values.yaml b/chart/values.yaml index 4c5076ee..71b5d687 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1,5 +1,11 @@ +# Settings +# ========================================= name: browsertrix-cloud +registration_enabled: 1 +jwt_token_lifetime_minutes: 60 + + # API Image # ========================================= api_image: "webrecorder/browsertrix-api" diff --git a/configs/config.sample.env b/configs/config.sample.env index cd8f30fa..5b550610 100644 --- a/configs/config.sample.env +++ b/configs/config.sample.env @@ -29,3 +29,8 @@ CRAWLER_IMAGE=webrecorder/browsertrix-crawler CRAWL_ARGS="--timeout 90 --logging stats,behaviors,debug --generateWACZ --screencastPort 9037" +REGISTRATION_ENABLED=1 + +JWT_TOKEN_LIFETIME_MINUTES=60 + +