From 633b3d210ae150419d050de5fb603702fd68c4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 12 Nov 2024 10:38:25 +0100 Subject: [PATCH] token: move nonce generation to a shared space --- token/delegation/delegation.go | 15 ++------------- token/internal/nonce/nonce.go | 14 ++++++++++++++ token/invocation/invocation.go | 15 ++------------- 3 files changed, 18 insertions(+), 26 deletions(-) create mode 100644 token/internal/nonce/nonce.go diff --git a/token/delegation/delegation.go b/token/delegation/delegation.go index f1e5553..599f773 100644 --- a/token/delegation/delegation.go +++ b/token/delegation/delegation.go @@ -10,7 +10,6 @@ package delegation // TODO: change the "delegation" link above when the specification is merged import ( - "crypto/rand" "errors" "fmt" "time" @@ -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" ) @@ -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 } @@ -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 -} diff --git a/token/internal/nonce/nonce.go b/token/internal/nonce/nonce.go new file mode 100644 index 0000000..3bda21b --- /dev/null +++ b/token/internal/nonce/nonce.go @@ -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 +} diff --git a/token/invocation/invocation.go b/token/invocation/invocation.go index f6eb07a..c48121f 100644 --- a/token/invocation/invocation.go +++ b/token/invocation/invocation.go @@ -8,7 +8,6 @@ package invocation import ( - "crypto/rand" "errors" "fmt" "time" @@ -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" ) @@ -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 } @@ -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 -}