Skip to content
Snippets Groups Projects
Commit 9f176d2a authored by Jono Wenger's avatar Jono Wenger
Browse files

Add VerifyPassword, which allows the checking if the password is correct

parent d65c4208
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
......@@ -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,
......
......@@ -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) {
......
......@@ -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))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment