Browse Source

add password autogeneration mecanism

Guénaël Muller 5 years ago
parent
commit
47c69550bc

+ 24 - 0
backend/tracim_backend/lib/utils/utils.py View File

@@ -1,5 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import datetime
3
+import random
4
+import string
3 5
 from redis import Redis
4 6
 from rq import Queue
5 7
 
@@ -72,3 +74,25 @@ def current_date_for_filename() -> str:
72 74
     # webdav utils, it may cause trouble. So, it should be replaced to
73 75
     # a character which will not change in bdd.
74 76
     return datetime.datetime.now().isoformat().replace(':', '.')
77
+
78
+# INFO - G.M - 2018-08-02 - Simple password generator, inspired by
79
+# https://gist.github.com/23maverick23/4131896
80
+
81
+
82
+ALLOWED_AUTOGEN_PASSWORD_CHAR = string.ascii_letters + \
83
+                                string.digits + \
84
+                                string.punctuation
85
+
86
+DEFAULT_PASSWORD_GEN_CHAR_LENGTH = 12
87
+
88
+
89
+def password_generator(
90
+        length: int=DEFAULT_PASSWORD_GEN_CHAR_LENGTH,
91
+        chars: str=ALLOWED_AUTOGEN_PASSWORD_CHAR
92
+) -> str:
93
+    """
94
+    :param length: length of the new password
95
+    :param chars: characters allowed
96
+    :return: password as string
97
+    """
98
+    return ''.join(random.choice(chars) for char_number in range(length))

+ 14 - 0
backend/tracim_backend/tests/library/tests_utils.py View File

@@ -0,0 +1,14 @@
1
+import string
2
+
3
+from tracim_backend.lib.utils.utils import password_generator
4
+from tracim_backend.lib.utils.utils import ALLOWED_AUTOGEN_PASSWORD_CHAR
5
+from tracim_backend.lib.utils.utils import DEFAULT_PASSWORD_GEN_CHAR_LENGTH
6
+
7
+
8
+class TestPasswordGenerator(object):
9
+
10
+    def test_password_generator_ok_nominal_case(self):
11
+        password = password_generator()
12
+        assert len(password) == DEFAULT_PASSWORD_GEN_CHAR_LENGTH
13
+        for char in password:
14
+            assert char in ALLOWED_AUTOGEN_PASSWORD_CHAR

+ 3 - 1
backend/tracim_backend/views/core_api/workspace_controller.py View File

@@ -31,6 +31,7 @@ from tracim_backend.exceptions import ContentNotFound
31 31
 from tracim_backend.exceptions import WorkspacesDoNotMatch
32 32
 from tracim_backend.exceptions import ParentNotFound
33 33
 from tracim_backend.views.controllers import Controller
34
+from tracim_backend.lib.utils.utils import password_generator
34 35
 from tracim_backend.views.core_api.schemas import FilterContentQuerySchema
35 36
 from tracim_backend.views.core_api.schemas import WorkspaceMemberCreationSchema
36 37
 from tracim_backend.views.core_api.schemas import WorkspaceMemberInviteSchema
@@ -213,7 +214,8 @@ class WorkspaceController(Controller):
213 214
                 # TODO - G.M - 2018-07-05 - [UserCreation] Reenable email
214 215
                 # notification for creation
215 216
                 user = uapi.create_user(
216
-                    hapic_data.body.user_email_or_public_name,
217
+                    email=hapic_data.body.user_email_or_public_name,
218
+                    password= password_generator(),
217 219
                     do_notify=True
218 220
                 )  # nopep8
219 221
                 newly_created = True