Skip to content

Commit

Permalink
Merge pull request CanastaWiki#104 from naresh-kumar-babu/wikiPassword
Browse files Browse the repository at this point in the history
Move password length validation to pre-create stage
  • Loading branch information
naresh-kumar-babu authored Jun 21, 2024
2 parents 2fbcb74 + 8a88887 commit 13b43d8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
Binary file added canasta
Binary file not shown.
11 changes: 0 additions & 11 deletions internal/mediawiki/mediawiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/CanastaWiki/Canasta-CLI-Go/internal/canasta"
Expand Down Expand Up @@ -173,16 +172,6 @@ func RemoveDatabase(path, name, orchestrator string) error {
return nil
}

func passwordCheck(admin, password string) error {
if len(password) < 10 {
logging.Fatal(fmt.Errorf("Password must be at least 10 characters long "))
} else if strings.Contains(password, admin) || strings.Contains(admin, password) {
logging.Fatal(fmt.Errorf("Password should not be same as admin name"))
}

return nil
}

func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
Expand Down
13 changes: 13 additions & 0 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func promptForUserPassword(username, password string) (string, string, error) {
return username, password, nil
}

func passwordCheck(username string, password string) (error) {
if len(password) < 10 {
return fmt.Errorf("Password must be at least 10 characters long")
} else if strings.Contains(password, username) || strings.Contains(username, password) {
return fmt.Errorf("Password should not be similar to the username")
}
return nil
}

func getAndConfirmPassword(username string) (string, string, error) {
fmt.Print("Enter the admin password (Press Enter to get saved password or, if one does not exist, autogenerate a password): \n")
password, err := getPasswordInput()
Expand All @@ -125,6 +134,10 @@ func getAndConfirmPassword(username string) (string, string, error) {
if err != nil || password != confirmedPassword {
return "", "", fmt.Errorf("Passwords do not match, please try again.")
}
passErr := passwordCheck(username, password)
if passErr != nil {
return "", "", passErr
}
return username, password, nil
}

Expand Down
29 changes: 29 additions & 0 deletions internal/prompt/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package prompt

import (
"testing"
)

func TestPasswordCheck(t *testing.T) {
tests := []struct {
username string
password string
wantErr bool
errMsg string
}{
{"user1", "short", true, "Password must be at least 10 characters long"},
{"user1", "user1password", true, "Password should not be similar to the username"},
{"password1234User", "password1234", true, "Password should not be similar to the username"},
{"user1", "securepassword", false, ""},
}

for _, tt := range tests {
err := passwordCheck(tt.username, tt.password)
if (err != nil) != tt.wantErr {
t.Errorf("passwordCheck(%q, %q) error = %v, wantErr %v", tt.username, tt.password, err, tt.wantErr)
}
if err != nil && err.Error() != tt.errMsg {
t.Errorf("passwordCheck(%q, %q) error message = %v, want %v", tt.username, tt.password, err.Error(), tt.errMsg)
}
}
}

0 comments on commit 13b43d8

Please sign in to comment.