diff --git a/cmd/ppass/ppass.go b/cmd/ppass/ppass.go index 5562299..a94e7a2 100644 --- a/cmd/ppass/ppass.go +++ b/cmd/ppass/ppass.go @@ -100,16 +100,10 @@ func main() { Config: Cfg, } - Namespaces := make(map[string]*storage.Namespace) - for _, nsCfg := range Cfg { - ns1, err := storage.CreateNamespace(nsCfg.Id, storageHandler) + storageHandler.InitNamespaces() - if err != nil { - logger.Fatal(err) - } - - Namespaces[nsCfg.Name] = ns1 - defer ns1.Close() + for _, val := range storageHandler.Namespaces { + defer val.Close() } fmt.Printf(` @@ -191,7 +185,7 @@ Commands: case "list": if len(fields) < 2 { fmt.Printf("Available Namespaces:\n") - for k := range Namespaces { + for k := range storageHandler.Namespaces { fmt.Printf("%s\n", k) } continue @@ -201,7 +195,7 @@ Commands: fmt.Printf("Listing content of %s", namespace) - val, ok := Namespaces[namespace] + val, ok := storageHandler.Namespaces[namespace] if !ok { fmt.Println("Namespace does not exist") @@ -218,7 +212,7 @@ Commands: namespace := fields[1] - val, ok := Namespaces[namespace] + val, ok := storageHandler.Namespaces[namespace] if !ok { fmt.Println("Namespace does not exist") @@ -242,7 +236,7 @@ Commands: namespace := fields[1] - val, ok := Namespaces[namespace] + val, ok := storageHandler.Namespaces[namespace] if !ok { fmt.Println("Namespace does not exist") @@ -279,7 +273,7 @@ Commands: namespace := fields[1] - val, ok := Namespaces[namespace] + val, ok := storageHandler.Namespaces[namespace] if !ok { fmt.Println("Namespace does not exist") diff --git a/storage/storage.go b/storage/storage.go index 66abc83..7003b96 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -33,6 +33,7 @@ import ( logging "github.com/ipfs/go-log/v2" agelib "filippo.io/age" + password "github.com/k4lipso/pentapass/crypto" "github.com/k4lipso/pentapass/crypto/age" ) @@ -250,6 +251,42 @@ func (n *Namespace) List() { } } +func (n *Namespace) GetAllPasswords() ([]password.Password, error) { + q := query.Query{} + results, err := n.Datastore.Query(n.ctx, q) + + if err != nil { + return nil, fmt.Errorf("Error during GetAllPasswords: %s", err) + } + + var result []password.Password + + for r := range results.Next() { + if r.Error != nil { + printErr(err) + continue + } + + val, err := age.Decrypt(r.Value, n.Key) + + if err != nil { + printErr(err) + continue + } + + pw, err := password.GetPasswordFromJson(val) + + if err != nil { + printErr(err) + continue + } + + result = append(result, pw) + } + + return result, nil +} + func (n *Namespace) Close() { n.Datastore.Close() n.CancelFunc() @@ -263,6 +300,26 @@ type StorageHandler struct { PubSub *pubsub.PubSub Key *agelib.X25519Identity Config []NamespaceConfig + Namespaces map[string]*Namespace +} + +func (s *StorageHandler) GetDefaultNamespace(Name string) *Namespace { + return s.Namespaces["root"] +} + +func (s *StorageHandler) InitNamespaces() { + NamespaceMap := make(map[string]*Namespace) + for _, nsCfg := range s.Config { + ns1, err := CreateNamespace(nsCfg.Id, *s) + + if err != nil { + logger.Fatal(err) + } + + NamespaceMap[nsCfg.Name] = ns1 + } + + s.Namespaces = NamespaceMap } func IsTrustedPeer(ctx context.Context, id peer.ID, namespace string, config []NamespaceConfig) bool { @@ -431,7 +488,6 @@ func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) { func printErr(err error) { fmt.Println("error:", err) - fmt.Println("> ") } func ConnectedPeers(h host.Host) []*peer.AddrInfo {