diff --git a/creds/password.go b/creds/password.go index 595fef90cc6ec2ce8fa9bc73c0983f15d9084ae3..58eb8d9ffe3c002963702d6f2573ec4c031d3ca9 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 4e9f74a77671923592940fc04275174861d72e62..822746e8c87020175528227cf42131e9a63509d7 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 a7484593ddf3cef3fdb831ca1dd06da04d538548..c4f89dd5ce318ce470649ae1d78196a331b1db55 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))