Skip to content

Commit

Permalink
token: move nonce generation to a shared space
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Nov 12, 2024
1 parent 3c705ca commit 633b3d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
15 changes: 2 additions & 13 deletions token/delegation/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package delegation
// TODO: change the "delegation" link above when the specification is merged

import (
"crypto/rand"
"errors"
"fmt"
"time"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta"
"github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/token/internal/nonce"
"github.com/ucan-wg/go-ucan/token/internal/parse"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func New(privKey crypto.PrivKey, aud did.DID, cmd command.Command, pol policy.Po
}

if len(tkn.nonce) == 0 {
tkn.nonce, err = generateNonce()
tkn.nonce, err = nonce.Generate()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,14 +217,3 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {

return &tkn, nil
}

// generateNonce creates a 12-byte random nonce.
// TODO: some crypto scheme require more, is that our case?
func generateNonce() ([]byte, error) {
res := make([]byte, 12)
_, err := rand.Read(res)
if err != nil {
return nil, err
}
return res, nil
}
14 changes: 14 additions & 0 deletions token/internal/nonce/nonce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package nonce

import "crypto/rand"

// Generate creates a 12-byte random nonce.
// TODO: some crypto scheme require more, is that our case?
func Generate() ([]byte, error) {
res := make([]byte, 12)
_, err := rand.Read(res)
if err != nil {
return nil, err
}
return res, nil
}
15 changes: 2 additions & 13 deletions token/invocation/invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package invocation

import (
"crypto/rand"
"errors"
"fmt"
"time"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/ucan-wg/go-ucan/pkg/args"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta"
"github.com/ucan-wg/go-ucan/token/internal/nonce"
"github.com/ucan-wg/go-ucan/token/internal/parse"
)

Expand Down Expand Up @@ -85,7 +85,7 @@ func New(iss, sub did.DID, cmd command.Command, prf []cid.Cid, opts ...Option) (
}

if len(tkn.nonce) == 0 {
tkn.nonce, err = generateNonce()
tkn.nonce, err = nonce.Generate()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -220,14 +220,3 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {

return &tkn, nil
}

// generateNonce creates a 12-byte random nonce.
// TODO: some crypto scheme require more, is that our case?
func generateNonce() ([]byte, error) {
res := make([]byte, 12)
_, err := rand.Read(res)
if err != nil {
return nil, err
}
return res, nil
}

0 comments on commit 633b3d2

Please sign in to comment.