Skip to content

Commit

Permalink
Merge pull request #150 from maxlaverse/base64_encode_ourselves
Browse files Browse the repository at this point in the history
base64 encode ourselves
  • Loading branch information
maxlaverse authored Aug 25, 2024
2 parents aa2dd91 + 0d3e325 commit 372b928
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 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.RawStdEncoding.EncodeToString(newOut), nil
}
11 changes: 5 additions & 6 deletions internal/bitwarden/bw/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

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

b := NewClient("dummy")
_, err := b.CreateObject(context.Background(), Object{
Type: ItemTypeLogin,
Type: ItemTypeLogin,
Object: ObjectTypeItem,
Fields: []Field{
{
Name: "test",
Expand All @@ -28,9 +28,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 item eyJncm91cHMiOm51bGwsImxvZ2luIjp7fSwib2JqZWN0IjoiaXRlbSIsInNlY3VyZU5vdGUiOnt9LCJ0eXBlIjoxLCJmaWVsZHMiOlt7Im5hbWUiOiJ0ZXN0IiwidmFsdWUiOiJwYXNzZWQiLCJ0eXBlIjowLCJsaW5rZWRJZCI6bnVsbH1dfQ", commandsExecuted()[0])
}
}

Expand Down

0 comments on commit 372b928

Please sign in to comment.