token: move nonce generation to a shared space

This commit is contained in:
Michael Muré
2024-11-12 10:38:25 +01:00
parent 3c705ca150
commit 633b3d210a
3 changed files with 18 additions and 26 deletions

View File

@@ -10,7 +10,6 @@ package delegation
// TODO: change the "delegation" link above when the specification is merged // TODO: change the "delegation" link above when the specification is merged
import ( import (
"crypto/rand"
"errors" "errors"
"fmt" "fmt"
"time" "time"
@@ -21,6 +20,7 @@ import (
"github.com/ucan-wg/go-ucan/pkg/command" "github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta" "github.com/ucan-wg/go-ucan/pkg/meta"
"github.com/ucan-wg/go-ucan/pkg/policy" "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" "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 { if len(tkn.nonce) == 0 {
tkn.nonce, err = generateNonce() tkn.nonce, err = nonce.Generate()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -217,14 +217,3 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
return &tkn, nil 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
}

View File

@@ -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
}

View File

@@ -8,7 +8,6 @@
package invocation package invocation
import ( import (
"crypto/rand"
"errors" "errors"
"fmt" "fmt"
"time" "time"
@@ -19,6 +18,7 @@ import (
"github.com/ucan-wg/go-ucan/pkg/args" "github.com/ucan-wg/go-ucan/pkg/args"
"github.com/ucan-wg/go-ucan/pkg/command" "github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta" "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" "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 { if len(tkn.nonce) == 0 {
tkn.nonce, err = generateNonce() tkn.nonce, err = nonce.Generate()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -220,14 +220,3 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
return &tkn, nil 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
}