Skip to content

Commit

Permalink
Merge pull request #33 from superfly/json-nonce
Browse files Browse the repository at this point in the history
Give json encoding for nonce
  • Loading branch information
btoews authored Aug 23, 2024
2 parents fab30a6 + 6f1c90d commit 43a0ea3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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

0 comments on commit 43a0ea3

Please sign in to comment.