Skip to content

Commit

Permalink
base64 encode ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Aug 25, 2024
1 parent aa2dd91 commit 20aac95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
14 changes: 5 additions & 9 deletions internal/bitwarden/bw/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bw

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -81,7 +82,7 @@ func DisableRetryBackoff() Options {
}

func (c *client) CreateObject(ctx context.Context, obj Object) (*Object, error) {
objEncoded, err := c.encode(ctx, obj)
objEncoded, err := c.encode(obj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,7 +129,7 @@ func (c *client) CreateAttachment(ctx context.Context, itemId string, filePath s
}

func (c *client) EditObject(ctx context.Context, obj Object) (*Object, error) {
objEncoded, err := c.encode(ctx, obj)
objEncoded, err := c.encode(obj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -323,15 +324,10 @@ func (c *client) env() []string {
return defaultEnv
}

func (c *client) encode(ctx context.Context, item Object) (string, error) {
func (c *client) encode(item Object) (string, error) {
newOut, err := json.Marshal(item)
if err != nil {
return "", fmt.Errorf("marshalling error: %v, %v", err, string(newOut))
}

out, err := c.cmd("encode").WithStdin(string(newOut)).Run(ctx)
if err != nil {
return "", fmt.Errorf("encoding error: %v, %v", err, string(newOut))
}
return string(out), err
return base64.StdEncoding.EncodeToString(newOut), nil
}
8 changes: 3 additions & 5 deletions internal/bitwarden/bw/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

func TestCreateObjectEncoding(t *testing.T) {
removeMocks, commandsExecuted := test_command.MockCommands(t, map[string]string{
"encode": `e30K`,
"create e30K": `{}`,
"create eyJncm91cHMiOm51bGwsImxvZ2luIjp7fSwic2VjdXJlTm90ZSI6e30sInR5cGUiOjEsImZpZWxkcyI6W3sibmFtZSI6InRlc3QiLCJ2YWx1ZSI6InBhc3NlZCIsInR5cGUiOjAsImxpbmtlZElkIjpudWxsfV19": `{}`,
})
defer removeMocks(t)

Expand All @@ -28,9 +27,8 @@ func TestCreateObjectEncoding(t *testing.T) {
})

assert.NoError(t, err)
if assert.Len(t, commandsExecuted(), 2) {
assert.Equal(t, "{\"groups\":null,\"login\":{},\"secureNote\":{},\"type\":1,\"fields\":[{\"name\":\"test\",\"value\":\"passed\",\"type\":0,\"linkedId\":null}]}:/:encode", commandsExecuted()[0])
assert.Equal(t, "create e30K", commandsExecuted()[1])
if assert.Len(t, commandsExecuted(), 1) {
assert.Equal(t, "create eyJncm91cHMiOm51bGwsImxvZ2luIjp7fSwic2VjdXJlTm90ZSI6e30sInR5cGUiOjEsImZpZWxkcyI6W3sibmFtZSI6InRlc3QiLCJ2YWx1ZSI6InBhc3NlZCIsInR5cGUiOjAsImxpbmtlZElkIjpudWxsfV19", commandsExecuted()[0])
}
}

Expand Down

0 comments on commit 20aac95

Please sign in to comment.