Skip to content

Commit

Permalink
Edit & Delete (#15)
Browse files Browse the repository at this point in the history
* feat: password edit & delete

* chore: update fairOS-dfs
  • Loading branch information
asabya authored Aug 18, 2022
1 parent eb3ed36 commit 4e04cd7
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 99 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ fyne package -os linux -icon icon.png
fyne package -os windows -icon icon.png
fyne package -os android -appID org.fairdatasociety.fairpass -icon icon.png
fyne package -os ios -appID org.fairdatasociety.fairpass -icon icon.png
fyne package -os android -appID org.fairdatasociety.fairpass
```

## Contribute
Expand Down
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ go 1.17

require (
fyne.io/fyne/v2 v2.1.4
github.com/fairdatasociety/fairOS-dfs v0.8.1
github.com/fairdatasociety/fairOS-dfs v0.8.2-rc4
github.com/google/uuid v1.3.0
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.8.1
)

require (
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/ethereum/go-ethereum v1.10.16 // indirect
github.com/ethersphere/bee v1.5.1-0.20220411230229-d5234fb078b7 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.18 // indirect
github.com/ethersphere/bee v1.7.0 // indirect
github.com/ethersphere/bmt v0.1.4 // indirect
github.com/fairdatasociety/fairOS-dfs-utils v0.0.0-20220208153721-e0f8b5b25222 // indirect
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect
Expand Down Expand Up @@ -57,13 +60,12 @@ require (
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/wealdtech/go-ens/v3 v3.5.3 // indirect
github.com/wealdtech/go-multicodec v1.4.0 // indirect
github.com/yuin/goldmark v1.3.8 // indirect
github.com/yuin/goldmark v1.4.1 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
65 changes: 51 additions & 14 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/screens/indexView.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type index struct {
progress dialog.Dialog

config *fairOSConfig
dfsAPI *dfs.DfsAPI
dfsAPI *dfs.API
sessionID string
password string
encryptor crypto.Crypto
Expand Down
12 changes: 4 additions & 8 deletions internal/screens/loginView.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ func (i *index) initLoginView() fyne.CanvasObject {
})
topContent := container.NewPadded(container.New(layout.NewHBoxLayout(), layout.NewSpacer(), configButton))
if i.dfsAPI == nil {
logger := logging.New(os.Stdout, logrus.ErrorLevel)
ensConfig := &contracts.Config{
ENSRegistryAddress: utils.EnsRegistryAddress,
SubdomainRegistrarAddress: utils.SubdomainRegistrarAddress,
PublicResolverAddress: utils.PublicResolverAddress,
ProviderDomain: utils.ProviderDomain,
ProviderBackend: i.config.RPC,
}
logger := logging.New(os.Stdout, logrus.DebugLevel)
// testnet config
ensConfig := contracts.TestnetConfig()
ensConfig.ProviderBackend = i.config.RPC
api, err := dfs.NewDfsAPI(
"",
i.config.BeeEndpoint,
Expand Down
2 changes: 1 addition & 1 deletion internal/screens/mainView.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (main *mainView) initMainView() {
var modal *widget.PopUp
c := container.NewVBox()
c.Add(widget.NewButton("Password", func() {
main.setContent(main.makeAddPasswordView(nil))
main.setContent(main.makeAddPasswordView(nil, true))
modal.Hide()
}))
c.Add(widget.NewButton("Notes", func() {
Expand Down
16 changes: 9 additions & 7 deletions internal/screens/passwordCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PasswordRecord struct {
Password string
}

func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
func (main *mainView) makeAddPasswordView(i *password, editable bool) fyne.CanvasObject {
if i == nil {
i = &password{}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
passwordBind = binding.BindString(&i.Password)
} else {
var err error
password, err := main.index.encryptor.DecryptContentWithPadding(main.index.password, i.Password, i.GeneratorOptions.Length)
password, err := main.index.encryptor.DecryptContent(main.index.password, i.Password)
if err != nil {
fmt.Println("failed to decrypt password ", err)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
d.Show()
})
var passwordObject fyne.CanvasObject
if i.ID != "" {
if !editable {
websiteEntry.Disable()
usernameEntry.Disable()
passwordEntry.Disable()
Expand Down Expand Up @@ -163,7 +163,7 @@ func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
i.Domain = rec.URL
i.Description = rec.Description
var err error
i.Password, err = main.index.encryptor.EncryptContentWithPadding(main.index.password, i.Password)
i.Password, err = main.index.encryptor.EncryptContent(main.index.password, i.Password)
if err != nil {
fmt.Println("failed to encrypt password")
return
Expand Down Expand Up @@ -201,7 +201,7 @@ func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
// Show file selection dialog.
})

if i.ID == "" {
if editable {
saveBtn := widget.NewButtonWithIcon("Save", theme.DocumentSaveIcon(), func() {
main.index.progress = dialog.NewProgressInfinite("", "Saving Password", main.index) //lint:ignore SA1019 fyne-io/fyne/issues/2782
main.index.progress.Show()
Expand All @@ -215,9 +215,11 @@ func (main *mainView) makeAddPasswordView(i *password) fyne.CanvasObject {
if i.Password == "" {
return
}
i.ID = uuid.New().String()
if i.ID == "" {
i.ID = uuid.New().String()
}
var err error
i.Password, err = main.index.encryptor.EncryptContentWithPadding(main.index.password, i.Password)
i.Password, err = main.index.encryptor.EncryptContent(main.index.password, i.Password)
if err != nil {
fmt.Println("failed to save password")
return
Expand Down
35 changes: 31 additions & 4 deletions internal/screens/passwordsView.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/fairdatasociety/fairpass/internal/utils"
)
Expand All @@ -30,9 +31,10 @@ func newListView(mainView *mainView) *listView {
items = append(items, r)
}
}

table := widget.NewTable(
func() (int, int) {
return len(items) + 1, 5
return len(items) + 1, 7
},
func() fyne.CanvasObject {
return widget.NewLabel("wide content")
Expand All @@ -51,6 +53,10 @@ func newListView(mainView *mainView) *listView {
label.SetText("Password")
case 4:
label.SetText("Info")
case 5:
label.SetText("Edit")
case 6:
label.SetText("Delete")
}
return
}
Expand All @@ -65,8 +71,12 @@ func newListView(mainView *mainView) *listView {
label.SetText(items[id.Row-1].Username)
case 3:
label.SetText("Copy")
default:
case 4:
label.SetText("View")
case 5:
label.SetText("Edit")
default:
label.SetText("Delete")
}
},
)
Expand All @@ -92,7 +102,7 @@ func newListView(mainView *mainView) *listView {
Content: "Username copied to clipboard",
})
case 3:
password, err := mainView.index.encryptor.DecryptContentWithPadding(mainView.index.password, items[id.Row-1].Password, items[id.Row-1].GeneratorOptions.Length)
password, err := mainView.index.encryptor.DecryptContent(mainView.index.password, items[id.Row-1].Password)
if err != nil {
fmt.Println("failed to decrypt password ", err)
}
Expand All @@ -101,8 +111,25 @@ func newListView(mainView *mainView) *listView {
Title: "FairPass",
Content: "Password copied to clipboard",
})
case 4:
mainView.setContent(mainView.makeAddPasswordView(items[id.Row-1], false))
case 5:
mainView.setContent(mainView.makeAddPasswordView(items[id.Row-1], true))
default:
mainView.setContent(mainView.makeAddPasswordView(items[id.Row-1]))
dialog.NewConfirm("Delete Password", "Are you sure?", func(choice bool) {
if choice {
err = mainView.index.dfsAPI.DocDel(mainView.index.sessionID, utils.PodName, utils.PasswordsTable, items[id.Row-1].ID)
if err != nil {
fmt.Println("failed to delete password ", err)
return
}
passwordsView := newListView(mainView)
mainView.setContent(passwordsView.view)
return
}
table.UnselectAll()
}, mainView.index.Window).Show()

}
}

Expand Down
46 changes: 0 additions & 46 deletions internal/utils/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,11 @@ import (
"encoding/base64"
"fmt"
"io"
mRand "math/rand"
"time"

"github.com/fairdatasociety/fairOS-dfs/pkg/utils"
)

const (
paddingMin = 300
paddingMax = 500
)

type Crypto interface {
EncryptContent(string, string) (string, error)
DecryptContent(string, string) (string, error)
EncryptContentWithPadding(string, string) (string, error)
DecryptContentWithPadding(string, string, int) (string, error)
}

type Encryptor struct{}
Expand Down Expand Up @@ -60,41 +49,6 @@ func (*Encryptor) EncryptContent(passphrase, data string) (string, error) {
return encryptedMessage, nil
}

func (*Encryptor) DecryptContentWithPadding(passphrase, encryptedContent string, length int) (string, error) {
password := passphrase
if password == "" {
return "", fmt.Errorf("passphrase cannot be blank")
}

if encryptedContent == "" {
return "", fmt.Errorf("invalid encrypted content")
}
aesKey := sha256.Sum256([]byte(password))

//decrypt the message
data, err := decrypt(aesKey[:], encryptedContent)
if err != nil {
return "", err
}
return data[:length], nil
}

func (*Encryptor) EncryptContentWithPadding(passphrase, data string) (string, error) {
password := passphrase
if password == "" {
return "", fmt.Errorf("passphrase cannot be blank")
}
aesKey := sha256.Sum256([]byte(password))
mRand.Seed(time.Now().UnixNano())
paddingLength := mRand.Intn(paddingMax-paddingMin) + paddingMin
randomStr := utils.GetRandString(paddingLength)
encryptedMessage, err := encrypt(aesKey[:], data+randomStr)
if err != nil {
return "", fmt.Errorf("create user account: %w", err)
}
return encryptedMessage, nil
}

func encrypt(key []byte, message string) (encmess string, err error) {
plainText := []byte(message)

Expand Down
8 changes: 0 additions & 8 deletions internal/utils/ens.go

This file was deleted.

0 comments on commit 4e04cd7

Please sign in to comment.