diff options
| author | Frederick Muriuki Muriithi | 2026-05-21 13:43:51 -0500 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-05-21 13:43:51 -0500 |
| commit | 4324f2432390392c8022beab480d8bc911682d1f (patch) | |
| tree | e10cb2c4ca05a1b762af6fc926f4184e91ca97c4 /scripts/register_sys_admin.py | |
| parent | 6568edd6bf616a0c17c5226f40b494a6b2967dad (diff) | |
| download | gn-auth-4324f2432390392c8022beab480d8bc911682d1f.tar.gz | |
Move scripts to top-level gn_auth package.
In preparation for migrating to pyproject.toml (from setup.py and friends) we need to have only one top-level package. This will also help in improving testing and checks down the line, since everything will be relative to one single top-level directory.
Diffstat (limited to 'scripts/register_sys_admin.py')
| -rw-r--r-- | scripts/register_sys_admin.py | 68 |
1 files changed, 0 insertions, 68 deletions
diff --git a/scripts/register_sys_admin.py b/scripts/register_sys_admin.py deleted file mode 100644 index 06aa845..0000000 --- a/scripts/register_sys_admin.py +++ /dev/null @@ -1,68 +0,0 @@ -"""Script to register and mark a user account as sysadmin.""" -import sys -import getpass -from pathlib import Path - -import click -from email_validator import validate_email, EmailNotValidError - -from gn_auth.auth.db import sqlite3 as db -from gn_auth.auth.authorisation.users.admin.models import make_sys_admin -from gn_auth.auth.authentication.users import save_user, set_user_password - -def fetch_email() -> str: - """Prompt user for email.""" - while True: - try: - user_input = input("Enter the administrator's email: ") - email = validate_email(user_input.strip(), check_deliverability=True) - return email["email"] # type: ignore - except EmailNotValidError as _enve: - print("You did not provide a valid email address. Try again...", - file=sys.stderr) - -def fetch_password() -> str: - """Prompt user for password.""" - while True: - passwd = getpass.getpass(prompt="Enter password: ").strip() - passwd2 = getpass.getpass(prompt="Confirm password: ").strip() - if passwd != "" and passwd == passwd2: - return passwd - if passwd == "": - print("Empty password not accepted", file=sys.stderr) - continue - if passwd != passwd2: - print("Passwords *MUST* match", file=sys.stderr) - continue - -def fetch_name() -> str: - """Prompt user for name""" - while True: - name = input("Enter the user's name: ").strip() - if name == "": - print("Invalid name.") - continue - return name - -def save_admin(conn: db.DbConnection, name: str, email: str, passwd: str): - """Save the details to the database and assign the new user as admin.""" - with db.cursor(conn) as cursor: - usr, _hpasswd = set_user_password( - cursor, save_user(cursor, email, name), passwd) - make_sys_admin(cursor, usr) - return 0 - -def register_admin(authdbpath: Path): - """Register a user as a system admin.""" - assert authdbpath.exists(), "Could not find database file." - with db.connection(str(authdbpath)) as conn: - return save_admin(conn, fetch_name(), fetch_email(), fetch_password()) - -if __name__ == "__main__": - @click.command() - @click.argument("authdbpath") - def run(authdbpath): - """Entry-point for when script is run directly""" - return register_admin(Path(authdbpath).absolute()) - - run()# pylint: disable=[no-value-for-parameter] |
