Skip to content

Commit 812cc20

Browse files
committed
Increase password strength in TG2 quickstart
1 parent a35f823 commit 812cc20

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

devtools/gearbox/quickstart/command.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import shutil
66
import glob
7+
import uuid
78
import importlib.metadata
89
import importlib.util
910

@@ -160,17 +161,8 @@ def take_action(self, opts):
160161
% opts.name)
161162
return
162163

163-
opts.cookiesecret = None
164-
try:
165-
import uuid
166-
opts.cookiesecret = str(uuid.uuid4())
167-
except ImportError:
168-
import random
169-
import base64
170-
import struct
171-
opts.cookiesecret = base64.b64encode(''.join(
172-
[struct.pack('i', random.randrange(2 ** 31))
173-
for _n in range(6)])).strip()
164+
opts.cookiesecret = str(uuid.uuid4())
165+
opts.passwordsalt = str(uuid.uuid4())
174166

175167
quickstart_path = os.path.os.path.abspath(os.path.dirname(__file__))
176168

devtools/gearbox/quickstart/template/+package+/model/auth.py_tmpl

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ though.
1212
{{if auth}}
1313
import os
1414
from datetime import datetime
15-
from hashlib import sha256
15+
from hashlib import pbkdf2_hmac, sha256
1616
{{endif}}
1717

1818
{{if auth == "sqlalchemy"}}
@@ -125,16 +125,13 @@ class User(DeclarativeBase):
125125

126126
@classmethod
127127
def _hash_password(cls, password):
128-
salt = sha256()
129-
salt.update(os.urandom(60))
130-
salt = salt.hexdigest()
128+
password = pbkdf2_hmac(
129+
'sha256',
130+
password.encode('utf-8'),
131+
b"{{passwordsalt}}",
132+
iterations=100_000
133+
).hex()
131134

132-
hash = sha256()
133-
# Make sure password is a str because we cannot hash unicode objects
134-
hash.update((password + salt).encode('utf-8'))
135-
hash = hash.hexdigest()
136-
137-
password = salt + hash
138135
return password
139136

140137
def _set_password(self, password):
@@ -160,9 +157,14 @@ class User(DeclarativeBase):
160157
:rtype: bool
161158

162159
"""
163-
hash = sha256()
164-
hash.update((password + self.password[:64]).encode('utf-8'))
165-
return self.password[64:] == hash.hexdigest()
160+
password = pbkdf2_hmac(
161+
'sha256',
162+
password.encode('utf-8'),
163+
b"{{passwordsalt}}",
164+
iterations=100_000
165+
).hex()
166+
167+
return self.password == password
166168

167169

168170
class Permission(DeclarativeBase):
@@ -247,16 +249,13 @@ class User(MappedClass):
247249
class PasswordProperty(FieldProperty):
248250
@classmethod
249251
def _hash_password(cls, password):
250-
salt = sha256()
251-
salt.update(os.urandom(60))
252-
salt = salt.hexdigest()
253-
254-
hash = sha256()
255-
# Make sure password is a str because we cannot hash unicode objects
256-
hash.update((password + salt).encode('utf-8'))
257-
hash = hash.hexdigest()
252+
password = pbkdf2_hmac(
253+
'sha256',
254+
password.encode('utf-8'),
255+
b"{{passwordsalt}}",
256+
iterations=100_000
257+
).hex()
258258

259-
password = salt + hash
260259
return password
261260

262261
def __set__(self, instance, value):
@@ -295,8 +294,12 @@ class User(MappedClass):
295294
:rtype: bool
296295

297296
"""
298-
hash = sha256()
299-
hash.update((password + self.password[:64]).encode('utf-8'))
300-
return self.password[64:] == hash.hexdigest()
297+
password = pbkdf2_hmac(
298+
'sha256',
299+
password.encode('utf-8'),
300+
b"{{passwordsalt}}",
301+
iterations=100_000
302+
).hex()
303+
return self.password == password
301304

302305
{{endif}}

0 commit comments

Comments
 (0)