Skip to content

Commit

Permalink
Added remove and update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
swicherwich committed Sep 26, 2023
1 parent 3d6d6eb commit efc84ba
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
bin
62 changes: 62 additions & 0 deletions cmd/pwdmg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/atotto/clipboard"
"github.com/swicherwich/pwdmg/internal/app/command/get"
"github.com/swicherwich/pwdmg/internal/app/command/importpwd"
"github.com/swicherwich/pwdmg/internal/app/command/remove"
"github.com/swicherwich/pwdmg/internal/app/command/save"
"github.com/swicherwich/pwdmg/internal/app/command/update"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
"syscall"
Expand All @@ -19,6 +21,8 @@ func PwdCommand() *cli.Command {
Subcommands: []*cli.Command{
getCommand(),
saveCommand(),
updateCommand(),
removeCommand(),
importCommand(),
},
}
Expand Down Expand Up @@ -56,6 +60,30 @@ func getCommand() *cli.Command {
}
}

func removeCommand() *cli.Command {
return &cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "pwdmg pwd remove <domain> <login>",
Description: "Remove domain and login record",
Action: func(c *cli.Context) error {
domain := c.Args().Get(0)
login := c.Args().Get(1)

if domain == "" || login == "" {
return errors.New("domain or login cannot be empty")
}

fmt.Println("Login removed")

if err := remove.RemoveAccount(domain, login); err != nil {
return err
}
return nil
},
}
}

func saveCommand() *cli.Command {
return &cli.Command{
Name: "save",
Expand Down Expand Up @@ -88,6 +116,40 @@ func saveCommand() *cli.Command {
}
}

func updateCommand() *cli.Command {
return &cli.Command{
Name: "update",
Aliases: []string{"upd"},
Usage: "pwdmg pwd update <domain> <login>",
Description: "Update password for provided domain and login account",
Action: func(c *cli.Context) error {
domain := c.Args().Get(0)
login := c.Args().Get(1)

if domain == "" || login == "" {
return errors.New("domain or login cannot be empty")
}

fmt.Print("Password: ")
pwdB, err := terminal.ReadPassword(syscall.Stdin)
pwd := string(pwdB)

if err != nil {
return errors.New("error reading password")
}

if pwd == "" {
return errors.New("empty pwd")
}

if err := update.UpdatePassword(domain, login, pwd); err != nil {
return err
}
return nil
},
}
}

func importCommand() *cli.Command {
return &cli.Command{
Name: "import",
Expand Down
2 changes: 0 additions & 2 deletions cmd/pwdmg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func main() {
},
}

fmt.Println(app.Command("pwd").Command("get").Names())

err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
Expand Down
46 changes: 46 additions & 0 deletions internal/app/command/remove/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package remove

import (
"errors"
"fmt"
"github.com/swicherwich/pwdmg/internal/app/domain"
"github.com/swicherwich/pwdmg/internal/pkg/fsutil"
"github.com/swicherwich/pwdmg/internal/pkg/secutil"
"time"
)

func RemoveAccount(d, acc string) error {
pwdmgDir, _ := fsutil.GetPwdmgDir()

dHash := secutil.HashStr(d)
accHash := secutil.HashStr(acc)
dFile := fmt.Sprintf("%s/%s%s", pwdmgDir, dHash, ".json")

if fsutil.FileExists(dFile) {
var d domain.Domain

if err := fsutil.ReadData(dFile, &d); err != nil {
return err
}

for i := range d.Accounts {
account := &d.Accounts[i]
if account.Login == accHash {
d.Metadata = domain.Metadata{
Entries: d.Metadata.Entries - 1,
LastModified: time.Now().Format(time.DateTime),
}

d.Accounts = append(d.Accounts[:i], d.Accounts[i+1:]...)

if err := fsutil.PersistDataToFile(dFile, d); err != nil {
return err
}

return nil
}
}
}

return errors.New("no such login")
}
45 changes: 45 additions & 0 deletions internal/app/command/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package update

import (
"errors"
"fmt"
"github.com/swicherwich/pwdmg/internal/app/domain"
"github.com/swicherwich/pwdmg/internal/pkg/fsutil"
"github.com/swicherwich/pwdmg/internal/pkg/secutil"
"time"
)

func UpdatePassword(d, acc, pwd string) error {
pwdmgDir, _ := fsutil.GetPwdmgDir()

dHash := secutil.HashStr(d)
accHash := secutil.HashStr(acc)
pwd64 := secutil.EncodeBase64(pwd)
dFile := fmt.Sprintf("%s/%s%s", pwdmgDir, dHash, ".json")

if fsutil.FileExists(dFile) {
var d domain.Domain

if err := fsutil.ReadData(dFile, &d); err != nil {
return err
}

for i := range d.Accounts {
account := &d.Accounts[i]
if account.Login == accHash {
d.Metadata = domain.Metadata{
LastModified: time.Now().Format(time.DateOnly),
}
account.Password = pwd64

if err := fsutil.PersistDataToFile(dFile, d); err != nil {
return err
}

return nil
}
}
}

return errors.New("no such login")
}
7 changes: 1 addition & 6 deletions internal/pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func PersistDataToFile(dFile string, d any) error {
fmt.Println("Error creating file:", err)
return err
}
defer file.Close()
defer func() { _ = file.Close() }()

jData, err := json.MarshalIndent(d, "", " ")
if err != nil {
Expand All @@ -43,11 +43,6 @@ func PersistDataToFile(dFile string, d any) error {
fmt.Println("Error writing to file:", err)
return err
}
_, err = fmt.Fprintf(file, "")
if err != nil {
fmt.Println("Error writing to file:", err)
return err
}

return nil
}
Expand Down

0 comments on commit efc84ba

Please sign in to comment.