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

Give json encoding for nonce #33

Merged
merged 2 commits into from
Aug 23, 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
1 change: 1 addition & 0 deletions flyio/caveats.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (r Role) String() string {
combined |= namedRole

if combined == r {
slices.Sort(names) // for consistency in tests
return strings.Join(names, "+")
}
}
Expand Down
2 changes: 1 addition & 1 deletion flyio/caveats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestAllowedRoles(t *testing.T) {
func TestRole(t *testing.T) {
assert.Equal(t, "admin", RoleAdmin.String())
assert.Equal(t, "member", RoleMember.String())
assert.Equal(t, "member+billing_manager", (RoleMember | RoleBillingManager).String())
assert.Equal(t, "billing_manager+member", (RoleMember | RoleBillingManager).String())

assert.True(t, RoleAdmin.HasAllRoles(RoleAdmin))
assert.True(t, RoleAdmin.HasAllRoles(RoleMember))
Expand Down
22 changes: 22 additions & 0 deletions macaroon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package macaroon

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -683,6 +684,27 @@ func TestDecodeNonce(t *testing.T) {
assert.Equal(t, m.Nonce, n)
}

func TestNonceJSON(t *testing.T) {
n1 := newNonce([]byte{1, 2, 3}, false)
n2 := newNonce([]byte{1, 2, 3}, true)
n3 := n1
n3.version = nonceV0
n4 := n1
n4.version = nonceV0

for _, n := range []Nonce{n1, n2, n3, n4} {
assert.NoError(t, msgpack.Unmarshal(n.MustEncode(), &n))

j, err := n.MarshalJSON()
assert.NoError(t, err)

var d Nonce
assert.NoError(t, json.Unmarshal(j, &d))

assert.Equal(t, n, d)
}
}

func dischargeMacaroon(ka EncryptionKey, location string, encodedMacaroon []byte) (bool, []Caveat, *Macaroon, error) {
tickets, err := TicketsForThirdParty(encodedMacaroon, location)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions nonce.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package macaroon

import (
"encoding/json"
"fmt"

"github.com/google/uuid"
Expand Down Expand Up @@ -112,6 +113,24 @@ func (n Nonce) MustEncode() []byte {
return b
}

func (n Nonce) MarshalJSON() ([]byte, error) {
raw, err := msgpack.Marshal(&n)
if err != nil {
return nil, err
}

return json.Marshal(raw)
}

func (c *Nonce) UnmarshalJSON(b []byte) error {
var raw []byte
if err := json.Unmarshal(b, &raw); err != nil {
return err
}

return msgpack.Unmarshal(raw, c)
}

// newNonce creates a nonce from a key-id, where the key-id
// is any opaque string; the resulting nonce has a bunch of random
// goo in it to goo up the nonce. A sane key-id might be a user-id
Expand Down
Loading