Allow offline start
This commit is contained in:
parent
3ca14678bd
commit
8ff9ea3ac9
5 changed files with 58 additions and 8 deletions
|
|
@ -96,7 +96,7 @@ func handleLogin(msg ipc.IPCMessage, cfg *config.Config, vault *vault.Vault, cal
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, &protectedUserSymetricKey)
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, &protectedUserSymetricKey, false)
|
||||||
|
|
||||||
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
|
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ func handleUnlockVault(request ipc.IPCMessage, cfg *config.Config, vault *vault.
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
bitwarden.RefreshToken(ctx, cfg)
|
bitwarden.RefreshToken(ctx, cfg)
|
||||||
token, err := cfg.GetToken()
|
token, err := cfg.GetToken()
|
||||||
err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, nil)
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, cfg, nil, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,7 @@ func StartUnixAgent(path string) error {
|
||||||
}
|
}
|
||||||
protectedUserSymetricKey, err := crypto.SymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
protectedUserSymetricKey, err := crypto.SymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
||||||
|
|
||||||
err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, &cfg, &protectedUserSymetricKey)
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, &cfg, &protectedUserSymetricKey, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +150,7 @@ func StartUnixAgent(path string) error {
|
||||||
}
|
}
|
||||||
protectedUserSymetricKey, err := crypto.SymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
protectedUserSymetricKey, err := crypto.SymmetricEncryptionKeyFromBytes(userSymmetricKey)
|
||||||
|
|
||||||
err = bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, &cfg, &protectedUserSymetricKey)
|
err = bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token.AccessToken), vault, &cfg, &protectedUserSymetricKey, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
@ -181,7 +181,7 @@ func StartUnixAgent(path string) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
bitwarden.SyncToVault(context.WithValue(ctx, bitwarden.AuthToken{}, token), vault, &cfg, nil)
|
bitwarden.DoFullSync(context.WithValue(ctx, bitwarden.AuthToken{}, token), vault, &cfg, nil, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@ package bitwarden
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/LlamaNite/llamalog"
|
"github.com/LlamaNite/llamalog"
|
||||||
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
|
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
|
||||||
|
|
@ -13,20 +15,30 @@ import (
|
||||||
|
|
||||||
var log = llamalog.NewLogger("Goldwarden", "Bitwarden API")
|
var log = llamalog.NewLogger("Goldwarden", "Bitwarden API")
|
||||||
|
|
||||||
|
const path = "/.cache/goldwarden-vault.json"
|
||||||
|
|
||||||
func Sync(ctx context.Context, config *config.Config) (models.SyncData, error) {
|
func Sync(ctx context.Context, config *config.Config) (models.SyncData, error) {
|
||||||
var sync models.SyncData
|
var sync models.SyncData
|
||||||
if err := authenticatedHTTPGet(ctx, config.ConfigFile.ApiUrl+"/sync", &sync); err != nil {
|
if err := authenticatedHTTPGet(ctx, config.ConfigFile.ApiUrl+"/sync", &sync); err != nil {
|
||||||
return models.SyncData{}, fmt.Errorf("could not sync: %v", err)
|
return models.SyncData{}, fmt.Errorf("could not sync: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
WriteVault(sync, home+path)
|
||||||
return sync, nil
|
return sync, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SyncToVault(ctx context.Context, vault *vault.Vault, config *config.Config, userSymmetricKey *crypto.SymmetricEncryptionKey) error {
|
func DoFullSync(ctx context.Context, vault *vault.Vault, config *config.Config, userSymmetricKey *crypto.SymmetricEncryptionKey, allowCache bool) error {
|
||||||
log.Info("Performing full sync...")
|
log.Info("Performing full sync...")
|
||||||
|
|
||||||
sync, err := Sync(ctx, config)
|
sync, err := Sync(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if allowCache {
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
sync, err = ReadVault(home + path)
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if userSymmetricKey != nil {
|
if userSymmetricKey != nil {
|
||||||
|
|
@ -52,3 +64,41 @@ func SyncToVault(ctx context.Context, vault *vault.Vault, config *config.Config,
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WriteVault(data models.SyncData, path string) error {
|
||||||
|
dataJson, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write to disk
|
||||||
|
os.Remove(path)
|
||||||
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
_, err = file.Write(dataJson)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadVault(path string) (models.SyncData, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return models.SyncData{}, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
data := models.SyncData{}
|
||||||
|
err = decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return models.SyncData{}, err
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
|
||||||
websocketLog.Error("Error getting token %s", err)
|
websocketLog.Error("Error getting token %s", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
SyncToVault(context.WithValue(ctx, AuthToken{}, token.AccessToken), vault, cfg, nil)
|
DoFullSync(context.WithValue(ctx, AuthToken{}, token.AccessToken), vault, cfg, nil, false)
|
||||||
break
|
break
|
||||||
case SyncCipherDelete:
|
case SyncCipherDelete:
|
||||||
websocketLog.Warn("Delete requested for cipher " + cipherid)
|
websocketLog.Warn("Delete requested for cipher " + cipherid)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue