Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 encode ourselves #150

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading