Skip to content

Commit

Permalink
Make the default user more secure
Browse files Browse the repository at this point in the history
  • Loading branch information
oscie57 authored Jan 23, 2024
1 parent 1e17183 commit 4bd924e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ config.py

images/
!images/.gitkeep

passwd.txt
10 changes: 8 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.event import listens_for
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin, LoginManager
import sqlalchemy, json
import sqlalchemy, json, string, random

db = SQLAlchemy()
login = LoginManager()
Expand Down Expand Up @@ -108,9 +108,15 @@ def create_default_user(target, connection, **kw):
"""Adds a default user to The Pantry.
By default, we assume admin:admin."""
table = User.__table__

default_password = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation) for _ in range(32))
with open("passwd.txt", 'x', encoding='UTF-8') as f:
f.write(default_password)
print(f"The default password is:\n{default_password}\nThis has been saved to 'passwd.txt'. Please keep this safe!")

connection.execute(
table.insert().values(
username="admin",
password_hash=generate_password_hash("admin"),
password_hash=generate_password_hash(default_password),
)
)

0 comments on commit 4bd924e

Please sign in to comment.