From 9f176d2a41ffb59a03a0fef61c8ef19aea8cd5ac Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Wed, 19 Oct 2022 15:16:06 -0700
Subject: [PATCH] Add VerifyPassword, which allows the checking if the password
 is correct

---
 creds/password.go      | 28 ++++++++++++++++++++--------
 creds/password_test.go | 18 ++++++++++++++++++
 main.go                |  1 +
 3 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/creds/password.go b/creds/password.go
index 595fef90..58eb8d9f 100644
--- a/creds/password.go
+++ b/creds/password.go
@@ -116,15 +116,19 @@ func ChangeExternalPassword(_ js.Value, args []js.Value) interface{} {
 	return nil
 }
 
-// getOrInit takes a user-provided password and returns its associated 256-bit
-// internal password.
+// VerifyPassword determines if the user-provided password is correct.
 //
-// If the internal password has not previously been created, then it is
-// generated, saved to local storage, and returned. If the internal password has
-// been previously generated, it is retrieved from local storage and returned.
+// Parameters:
+//  - args[0] - The user supplied password (string).
 //
-// Any password saved to local storage is encrypted using the user-provided
-// password.
+// Returns:
+//  - True if the password is correct and false if it is incorrect (boolean).
+func VerifyPassword(_ js.Value, args []js.Value) interface{} {
+	return verifyPassword(args[0].String())
+}
+
+// getOrInit is the private function for GetOrInitPassword that is used for
+// testing.
 func getOrInit(externalPassword string) ([]byte, error) {
 	localStorage := utils.GetLocalStorage()
 	internalPassword, err := getInternalPassword(externalPassword, localStorage)
@@ -141,7 +145,8 @@ func getOrInit(externalPassword string) ([]byte, error) {
 	return internalPassword, nil
 }
 
-// changeExternalPassword allows a user to change their external password.
+// changeExternalPassword is the private function for ChangeExternalPassword
+// that is used for testing.
 func changeExternalPassword(oldExternalPassword, newExternalPassword string) error {
 	localStorage := utils.GetLocalStorage()
 	internalPassword, err := getInternalPassword(oldExternalPassword, localStorage)
@@ -164,6 +169,13 @@ func changeExternalPassword(oldExternalPassword, newExternalPassword string) err
 	return nil
 }
 
+// verifyPassword is the private function for VerifyPassword that is used for
+// testing.
+func verifyPassword(externalPassword string) bool {
+	_, err := getInternalPassword(externalPassword, utils.GetLocalStorage())
+	return err == nil
+}
+
 // initInternalPassword generates a new internal password, stores an encrypted
 // version in local storage, and returns it.
 func initInternalPassword(externalPassword string,
diff --git a/creds/password_test.go b/creds/password_test.go
index 4e9f74a7..822746e8 100644
--- a/creds/password_test.go
+++ b/creds/password_test.go
@@ -75,6 +75,24 @@ func Test_changeExternalPassword(t *testing.T) {
 	}
 }
 
+// Tests that verifyPassword returns true for a valid password and false for an
+// invalid password
+func Test_verifyPassword(t *testing.T) {
+	externalPassword := "myPassword"
+
+	if _, err := getOrInit(externalPassword); err != nil {
+		t.Errorf("%+v", err)
+	}
+
+	if !verifyPassword(externalPassword) {
+		t.Errorf("Password %q is incorrect.", externalPassword)
+	}
+
+	if verifyPassword("wrong password") {
+		t.Error("Incorrect password found to be correct.")
+	}
+}
+
 // Tests that the internal password returned by initInternalPassword matches
 // the encrypted one saved to local storage.
 func Test_initInternalPassword(t *testing.T) {
diff --git a/main.go b/main.go
index a7484593..c4f89dd5 100644
--- a/main.go
+++ b/main.go
@@ -42,6 +42,7 @@ func main() {
 	js.Global().Set("GetOrInitPassword", js.FuncOf(creds.GetOrInitPassword))
 	js.Global().Set("ChangeExternalPassword",
 		js.FuncOf(creds.ChangeExternalPassword))
+	js.Global().Set("VerifyPassword", js.FuncOf(creds.VerifyPassword))
 
 	// utils/array.go
 	js.Global().Set("Uint8ArrayToBase64", js.FuncOf(utils.Uint8ArrayToBase64))
-- 
GitLab