Initial commit of bin/nntp-tiny-passwd tool to change user passwords
This commit is contained in:
parent
a319650004
commit
ffca139659
1 changed files with 57 additions and 0 deletions
57
bin/nntp-tiny-passwd
Executable file
57
bin/nntp-tiny-passwd
Executable file
|
@ -0,0 +1,57 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
import getpass
|
||||
|
||||
from nntp.tiny.config import Config
|
||||
from nntp.tiny.db import Database
|
||||
from nntp.tiny.user import User
|
||||
from nntp.tiny.passwd import crypt
|
||||
|
||||
parser = argparse.ArgumentParser(description='Set password of account')
|
||||
parser.add_argument('--config-file', '-f', type=str, help='Specify a configuration file location')
|
||||
parser.add_argument('username', type=str, help='Username of account')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
config = Config.load(args.config_file)
|
||||
db = Database.connect(config.get('database', 'path'))
|
||||
|
||||
user = db.get(User, {'username': args.username})
|
||||
|
||||
if user is None:
|
||||
print(f"{sys.argv[0]}: unknown user '{args.username}'", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
attempts_total = 3
|
||||
attempts_left = attempts_total
|
||||
|
||||
while attempts_left > 0:
|
||||
attempts_left -= 1
|
||||
|
||||
password_1 = getpass.getpass()
|
||||
|
||||
if password_1 == '':
|
||||
continue
|
||||
|
||||
password_2 = getpass.getpass('Enter password again: ')
|
||||
|
||||
if password_1 != password_2:
|
||||
continue
|
||||
|
||||
user.password = crypt(password_2)
|
||||
|
||||
try:
|
||||
db.update(user)
|
||||
except Exception as e:
|
||||
print(f"Unable to change password for user '{args.username}': {e}")
|
||||
exit(1)
|
||||
|
||||
print(f"Password changed successfully for user '{args.username}'.")
|
||||
exit(0)
|
||||
|
||||
if attempts_left == 0:
|
||||
print(f"Failed to change password for user '{args.username}' after {attempts_total} attempts.", file=sys.stderr)
|
||||
exit(1)
|
Loading…
Add table
Reference in a new issue