diff --git a/src/client.py b/src/client.py index 72c0409..1333f33 100644 --- a/src/client.py +++ b/src/client.py @@ -106,4 +106,12 @@ def createClientBlueprint(wireguardConfigurations: dict[WireguardConfiguration], def ClientAPI_Settings_GetClientProfile(): return ResponseObject(data=DashboardClients.GetClientProfile(session['ClientID'])) + @client.post(f'{prefix}/api/settings/updatePassword') + @login_required + def ClientAPI_Settings_UpdatePassword(): + data = request.json + status, message = DashboardClients.UpdateClientPassword(session['Email'], **data) + + return ResponseObject(status, message) + return client \ No newline at end of file diff --git a/src/modules/DashboardClients.py b/src/modules/DashboardClients.py index 8658c5d..fafbf72 100644 --- a/src/modules/DashboardClients.py +++ b/src/modules/DashboardClients.py @@ -10,7 +10,6 @@ from .DashboardClientsPeerAssignment import DashboardClientsPeerAssignment from .DashboardClientsTOTP import DashboardClientsTOTP from .Utilities import ValidatePasswordStrength from .DashboardLogger import DashboardLogger - from flask import session @@ -69,21 +68,34 @@ class DashboardClients: self.dashboardClientsInfoTable.c.ClientID == ClientID ) ).mappings().fetchone()) - - def SignIn(self, Email, Password) -> tuple[bool, str]: + + def SignIn_ValidatePassword(self, Email, Password) -> bool: if not all([Email, Password]): - return False, "Please fill in all fields" + return False + existingClient = self.SignIn_UserExistence(Email) + if existingClient: + return bcrypt.checkpw(Password.encode("utf-8"), existingClient.get("Password").encode("utf-8")) + return False + + def SignIn_UserExistence(self, Email): with self.engine.connect() as conn: existingClient = conn.execute( self.dashboardClientsTable.select().where( self.dashboardClientsTable.c.Email == Email ) ).mappings().fetchone() - if existingClient: - checkPwd = bcrypt.checkpw(Password.encode("utf-8"), existingClient.get("Password").encode("utf-8")) - if checkPwd: - session['ClientID'] = existingClient.get("ClientID") - return True, self.DashboardClientsTOTP.GenerateToken(existingClient.get("ClientID")) + return existingClient + + def SignIn(self, Email, Password) -> tuple[bool, str]: + if not all([Email, Password]): + return False, "Please fill in all fields" + existingClient = self.SignIn_UserExistence(Email) + if existingClient: + checkPwd = self.SignIn_ValidatePassword(Email, Password) + if checkPwd: + session['Email'] = Email + session['ClientID'] = existingClient.get("ClientID") + return True, self.DashboardClientsTOTP.GenerateToken(existingClient.get("ClientID")) return False, "Email or Password is incorrect" def SignIn_GetTotp(self, Token: str, UserProvidedTotp: str = None) -> tuple[bool, str] or tuple[bool, None, str]: @@ -119,15 +131,10 @@ class DashboardClients: return False, "Please fill in all fields" if Password != ConfirmPassword: return False, "Passwords does not match" - - with self.engine.connect() as conn: - existingClient = conn.execute( - self.dashboardClientsTable.select().where( - self.dashboardClientsTable.c.Email == Email - ) - ).mappings().fetchone() - if existingClient: - return False, "Email already signed up" + + existingClient = self.SignIn_UserExistence(Email) + if existingClient: + return False, "Email already signed up" pwStrength, msg = ValidatePasswordStrength(Password) if not pwStrength: @@ -150,6 +157,7 @@ class DashboardClients: "ClientID": newClientUUID }) ) + self.logger.log(Message=f"User {Email} signed up") except Exception as e: self.logger.log(Status="false", Message=f"Signed up failed, reason: {str(e)}") return False, "Signed up failed." @@ -159,5 +167,30 @@ class DashboardClients: def GetClientAssignedPeers(self, ClientID): return self.DashboardClientsPeerAssignment.GetAssignedPeers(ClientID) - def UpdatePassword(self, CurrentPassword, NewPassword, ConfirmNewPassword): - pass \ No newline at end of file + def UpdateClientPassword(self, Email, CurrentPassword, NewPassword, ConfirmNewPassword): + if not all([CurrentPassword, NewPassword, ConfirmNewPassword]): + return False, "Please fill in all fields" + + if not self.SignIn_ValidatePassword(Email, CurrentPassword): + return False, "Current password does not match" + + if NewPassword != ConfirmNewPassword: + return False, "New passwords does not match" + + pwStrength, msg = ValidatePasswordStrength(NewPassword) + if not pwStrength: + return pwStrength, msg + try: + with self.engine.begin() as conn: + conn.execute( + self.dashboardClientsTable.update().values({ + "Password": bcrypt.hashpw(NewPassword.encode('utf-8'), bcrypt.gensalt()).decode("utf-8"), + }).where( + self.dashboardClientsTable.c.Email == Email + ) + ) + self.logger.log(Message=f"User {Email} updated password") + except Exception as e: + self.logger.log(Status="false", Message=f"Signed up failed, reason: {str(e)}") + return False, "Signed up failed." + return True, None \ No newline at end of file diff --git a/src/static/app/src/main.js b/src/static/app/src/main.js index 04ada83..6f0e35f 100644 --- a/src/static/app/src/main.js +++ b/src/static/app/src/main.js @@ -9,7 +9,6 @@ import { createPinia } from 'pinia' import App from './App.vue' import router from './router/router.js' import {DashboardConfigurationStore} from "@/stores/DashboardConfigurationStore.js"; -import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' let Locale; await fetch("/api/locale") .then(res => res.json()) diff --git a/src/static/client/src/components/Settings/profile.vue b/src/static/client/src/components/Settings/profile.vue index 5b32f8b..a9542b5 100644 --- a/src/static/client/src/components/Settings/profile.vue +++ b/src/static/client/src/components/Settings/profile.vue @@ -1,69 +1,26 @@ diff --git a/src/static/client/src/components/Settings/updatePassword.vue b/src/static/client/src/components/Settings/updatePassword.vue new file mode 100644 index 0000000..ebeadcf --- /dev/null +++ b/src/static/client/src/components/Settings/updatePassword.vue @@ -0,0 +1,102 @@ + + + + + \ No newline at end of file diff --git a/src/static/client/src/views/index.vue b/src/static/client/src/views/index.vue index 868e110..960a0d1 100644 --- a/src/static/client/src/views/index.vue +++ b/src/static/client/src/views/index.vue @@ -6,18 +6,12 @@ import Configuration from "@/components/Configuration/configuration.vue"; const store = clientStore() const loading = ref(true) -const loadConfigurations = async () => { - - await store.getConfigurations() - -} - const configurations = computed(() => { return store.configurations }); onMounted(async () => { - await loadConfigurations(); + await store.getConfigurations() loading.value = false; }) diff --git a/src/static/client/src/views/settings.vue b/src/static/client/src/views/settings.vue index de1a34a..b8028c2 100644 --- a/src/static/client/src/views/settings.vue +++ b/src/static/client/src/views/settings.vue @@ -1,6 +1,7 @@