Skip to content

Commit

Permalink
Add sync return url
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Feb 4, 2024
1 parent d0e0d66 commit f319686
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
6 changes: 4 additions & 2 deletions agent/actions/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func handleCreateSend(msg messages.IPCMessage, cfg *config.Config, vault *vault.
parsedMsg := messages.ParsePayload(msg).(messages.CreateSendRequest)

ctx := context.WithValue(context.TODO(), bitwarden.AuthToken{}, token.AccessToken)
_, err = bitwarden.CreateSend(ctx, cfg, vault, parsedMsg.Name, parsedMsg.Text)
url, err := bitwarden.CreateSend(ctx, cfg, vault, parsedMsg.Name, parsedMsg.Text)

response, err = messages.IPCMessageFromPayload(messages.CreateSendResponse{})
response, err = messages.IPCMessageFromPayload(messages.CreateSendResponse{
URL: url,
})
return
}

Expand Down
22 changes: 13 additions & 9 deletions agent/bitwarden/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"io"
"strings"
"time"

"github.com/quexten/goldwarden/agent/bitwarden/crypto"
Expand Down Expand Up @@ -38,6 +40,8 @@ type SendMetadata struct {
}

type SendCreateRequest struct {
AccessCount *int `json:"accessCount"`
AccessId *string `json:"accessId"`
DeletionDate string `json:"deletionDate"`
Disabled bool `json:"disabled"`
ExpirationDate *string `json:"expirationDate"`
Expand All @@ -50,38 +54,38 @@ type SendCreateRequest struct {
Type int `json:"type"`
}

func CreateSend(ctx context.Context, cfg *config.Config, vault *vault.Vault, name string, text string) (SendMetadata, error) {
func CreateSend(ctx context.Context, cfg *config.Config, vault *vault.Vault, name string, text string) (string, error) {
timestampIn14Days := time.Now().AddDate(0, 0, 14)
timestampIn14DaysStr := timestampIn14Days.Format("2006-01-02T15:04:05Z")

// generate 32 byte key
sendSourceKey := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, sendSourceKey)
if err != nil {
return SendMetadata{}, err
return "", err
}

encryptedSendSourceKey, err := crypto.EncryptWithToString(sendSourceKey, crypto.AesCbc256_HmacSha256_B64, vault.Keyring.GetAccountKey())
if err != nil {
return SendMetadata{}, err
return "", err
}

sendUseKeyPairBytes := make([]byte, 64)
hkdf.New(sha256.New, sendSourceKey, []byte("bitwarden-send"), []byte("send")).Read(sendUseKeyPairBytes)

sendUseKeyPair, err := crypto.MemorySymmetricEncryptionKeyFromBytes(sendUseKeyPairBytes)
if err != nil {
return SendMetadata{}, err
return "", err
}

encryptedName, err := crypto.EncryptWithToString([]byte(name), crypto.AesCbc256_HmacSha256_B64, sendUseKeyPair)
if err != nil {
return SendMetadata{}, err
return "", err
}

encryptedText, err := crypto.EncryptWithToString([]byte(text), crypto.AesCbc256_HmacSha256_B64, sendUseKeyPair)
if err != nil {
return SendMetadata{}, err
return "", err
}

sendRequest := SendCreateRequest{
Expand All @@ -97,11 +101,11 @@ func CreateSend(ctx context.Context, cfg *config.Config, vault *vault.Vault, nam
Type: 0,
}

var result interface{}
var result SendCreateRequest
err = authenticatedHTTPPost(ctx, cfg.ConfigFile.ApiUrl+"/sends", &result, sendRequest)
if err != nil {
return SendMetadata{}, err
return "", err
}

return SendMetadata{}, nil
return cfg.ConfigFile.VaultUrl + "/#/send/" + *result.AccessId + "/" + strings.ReplaceAll(base64.RawURLEncoding.EncodeToString(sendSourceKey), "+", "-"), nil
}
2 changes: 0 additions & 2 deletions agent/bitwarden/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
"github.com/quexten/goldwarden/agent/bitwarden/models"
"github.com/quexten/goldwarden/agent/config"
"github.com/quexten/goldwarden/agent/notify"
"github.com/quexten/goldwarden/agent/vault"
"github.com/quexten/goldwarden/logging"
)
Expand All @@ -35,7 +34,6 @@ func DoFullSync(ctx context.Context, vault *vault.Vault, config *config.Config,
sync, err := Sync(ctx, config)
if err != nil {
log.Error("Could not sync: %v", err)
notify.Notify("Goldwarden", "Could not sync", "", 0, func() {})
if allowCache {
home, _ := os.UserHomeDir()
sync, err = ReadVault(home + path)
Expand Down

0 comments on commit f319686

Please sign in to comment.