1+ import logging as log
2+
3+ import bcrypt
4+ import flask
5+ import hashlib
6+ import json
7+ import pyotp
8+ import os
9+ import werkzeug
10+ from datetime import datetime
11+
12+ from .response import make_resp_obj
13+ from .routes_utils import routes_utils
14+ from .locale import localeman
15+
16+ from ..database .functions import functions
17+ from ..config .config import config
18+
19+ routes_welcome = flask .Blueprint ("routes_welcome" , __name__ )
20+
21+ @routes_welcome .route ('/api/Welcome_Finish' , methods = ["POST" ])
22+ def api_welcome_finish ():
23+ ok , config_other = config .filter (flask .current_app .wgd_config , 'OTHER' )
24+ if not ok :
25+ log .error ("failed to filter the config in-memory" )
26+ return make_resp_obj (False , 'Internal error' , {}, 500 )
27+
28+ req_data = flask .request .get_json ()
29+ if not req_data :
30+ return make_resp_obj (False , "Invalid request body" , {}, 400 )
31+
32+ if len (req_data ["username" ]) == 0 :
33+ return make_resp_obj (False , "Username cannot be empty" , {}, 400 )
34+
35+ if len (req_data ["newPassword" ]) < 7 :
36+ return make_resp_obj (False , "Password must be at least 8 characters" , {}, 400 )
37+
38+ if not config .update ('ACCOUNT' , 'username' , req_data ["username" ]):
39+ log .error ("failed to update the key in the configuration file" )
40+ return make_resp_obj (False , 'Internal error' , {}, 500 )
41+
42+ hashed_password = bcrypt .hashpw (req_data ["newPassword" ].encode ('utf-8' ), bcrypt .gensalt ())
43+
44+ if not config .update ('ACCOUNT' , 'password' , hashed_password .decode ('utf-8' )):
45+ log .error ("failed to update the key in the configuration file" )
46+ return make_resp_obj (False , 'Internal error' , {}, 500 )
47+
48+ if not config .update ('OTHER' , 'welcome_session' , False ):
49+ log .error ("failed to update the key in the configuration file" )
50+ return make_resp_obj (False , 'Internal error' , {}, 500 )
51+
52+ # Very important to also refresh the config in-memory
53+ ok , flask .current_app .wgd_config = config .read ()
54+ if not ok :
55+ log .error ("failed to refresh the in-memory configuration" )
56+ return make_resp_obj (False , 'Internal error' , {}, 500 )
57+
58+ return make_resp_obj ()
59+
60+ @routes_welcome .route ('/api/Welcome_GetTotpLink' )
61+ def api_welcome_get_totp ():
62+ ok , config_account = config .filter (flask .current_app .wgd_config , 'ACCOUNT' )
63+ if not ok :
64+ log .error ("failed to filter the config in-memory" )
65+ return make_resp_obj (False , 'Internal error' , {}, 500 )
66+
67+ if "totp_verified" not in config_account or not config_account ["totp_verified" ]:
68+ totp_key = pyotp .random_base32 ()
69+
70+ log .debug (totp_key )
71+ ok = config .update ('ACCOUNT' , 'totp_key' , totp_key )
72+ if not ok :
73+ log .error ("failed to update the key in the configuration file" )
74+ return make_resp_obj (False , 'Internal error' , {}, 500 )
75+
76+ return make_resp_obj (True , '' , pyotp .totp .TOTP (totp_key ).provisioning_uri (issuer_name = "WGDashboard Admin" ))
77+
78+ return make_resp_obj (False , 'Internal error' , {}, 500 )
0 commit comments