Merge branch 'admin-token' into 'master'

Implement admin ID for automated tests

See merge request litecord/litecord!90
This commit is contained in:
Bluenix 2022-10-19 11:41:54 +00:00
commit aeee3d9dbb
3 changed files with 36 additions and 1 deletions

View File

@ -48,6 +48,9 @@ class Config:
# Postgres credentials # Postgres credentials
POSTGRES = {} POSTGRES = {}
ADMIN_ID = None
ADMIN_TOKEN = None
class Development(Config): class Development(Config):
DEBUG = True DEBUG = True

View File

@ -17,6 +17,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import os
from logbook import Logger
log = Logger(__name__)
MODE = "Development" MODE = "Development"
@ -69,6 +76,23 @@ class Config:
#: Shared secret for LVSP #: Shared secret for LVSP
LVSP_SECRET = "" LVSP_SECRET = ""
#: Admin credentials for automated testing
# The token is the value to pass in the Authorization header, and the ID
# is the user ID to use when it is passed.
ADMIN_ID = os.getenv("ADMIN_ID")
ADMIN_TOKEN = os.getenv("ADMIN_TOKEN")
if None in {ADMIN_ID, ADMIN_TOKEN} and not ADMIN_ID == ADMIN_TOKEN:
log.warning(
"Not both admin ID ({}) and token ({}) configured; ignoring",
ADMIN_ID,
ADMIN_TOKEN,
)
ADMIN_ID = ADMIN_TOKEN = None
if ADMIN_ID is not None:
ADMIN_ID = int(ADMIN_ID)
class Development(Config): class Development(Config):
DEBUG = True DEBUG = True
@ -91,3 +115,6 @@ class Production(Config):
"password": "some_production_password", "password": "some_production_password",
"database": "litecord_or_anything_else_really", "database": "litecord_or_anything_else_really",
} }
ADMIN_TOKEN = None
ADMIN_ID = None

View File

@ -47,6 +47,9 @@ async def raw_token_check(token: str, db=None) -> int:
Forbidden Forbidden
If token validation fails. If token validation fails.
""" """
if app.config["ADMIN_TOKEN"] is not None:
return app.config["ADMIN_ID"]
db = db or app.db db = db or app.db
# just try by fragments instead of # just try by fragments instead of
@ -121,6 +124,8 @@ async def token_check() -> int:
async def admin_check() -> int: async def admin_check() -> int:
"""Check if the user is an admin.""" """Check if the user is an admin."""
user_id = await token_check() user_id = await token_check()
if user_id == app.config["ADMIN_ID"]:
return user_id
flags = await app.db.fetchval( flags = await app.db.fetchval(
""" """