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

Added sign func for external signing method #130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 14 additions & 8 deletions biscuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package biscuit

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/binary"

//"crypto/sha256"
"crypto/ed25519"
"errors"
"fmt"
"io"

"google.golang.org/protobuf/proto"

"github.com/biscuit-auth/biscuit-go/v2/datalog"
"github.com/biscuit-auth/biscuit-go/v2/pb"

//"github.com/biscuit-auth/biscuit-go/sig"
"google.golang.org/protobuf/proto"
)

// Biscuit represents a valid Biscuit token
Expand Down Expand Up @@ -53,7 +50,12 @@ var (
UnsupportedAlgorithm = errors.New("biscuit: unsupported signature algorithm")
)

func New(rng io.Reader, root ed25519.PrivateKey, baseSymbols *datalog.SymbolTable, authority *Block) (*Biscuit, error) {
func New(
rng io.Reader,
sign signFunc,
baseSymbols *datalog.SymbolTable,
authority *Block,
) (*Biscuit, error) {
if rng == nil {
rng = rand.Reader
}
Expand Down Expand Up @@ -83,7 +85,11 @@ func New(rng io.Reader, root ed25519.PrivateKey, baseSymbols *datalog.SymbolTabl
toSign := append(marshalledAuthority[:], toSignAlgorithm...)
toSign = append(toSign, nextPublicKey[:]...)

signature := ed25519.Sign(root, toSign)
var signature []byte
if signature, err = sign(toSign); err != nil {
return nil, err
}

nextKey := &pb.PublicKey{
Algorithm: &algorithm,
Key: nextPublicKey,
Expand Down
2 changes: 1 addition & 1 deletion biscuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func TestNewErrors(t *testing.T) {

t.Run("authority block Strings overlap", func(t *testing.T) {
_, privateRoot, _ := ed25519.GenerateKey(rng)
_, err := New(rng, privateRoot, &datalog.SymbolTable{"String1", "String2"}, &Block{
_, err := New(rng, DefaultSignMethod(privateRoot), &datalog.SymbolTable{"String1", "String2"}, &Block{
symbols: &datalog.SymbolTable{"String1"},
})
require.Equal(t, ErrSymbolTableOverlap, err)
Expand Down
25 changes: 20 additions & 5 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Builder interface {
}

type builder struct {
rng io.Reader
root ed25519.PrivateKey
rng io.Reader
signMethod signFunc

symbolsStart int
symbols *datalog.SymbolTable
Expand All @@ -38,7 +38,16 @@ type builder struct {
context string
}

type builderOption func(b *builder)
type (
builderOption func(b *builder)
signFunc func([]byte) ([]byte, error)
)

func DefaultSignMethod(root ed25519.PrivateKey) signFunc {
return func(toSign []byte) ([]byte, error) {
return ed25519.Sign(root, toSign), nil
}
}

func WithRandom(rng io.Reader) builderOption {
return func(b *builder) {
Expand All @@ -53,13 +62,19 @@ func WithSymbols(symbols *datalog.SymbolTable) builderOption {
}
}

func WithCustomSignMethod(s signFunc) builderOption {
return func(b *builder) {
b.signMethod = s
}
}

func NewBuilder(root ed25519.PrivateKey, opts ...builderOption) Builder {
b := &builder{
rng: rand.Reader,
root: root,
symbols: defaultSymbolTable.Clone(),
symbolsStart: defaultSymbolTable.Len(),
facts: new(datalog.FactSet),
signMethod: DefaultSignMethod(root),
}

for _, o := range opts {
Expand Down Expand Up @@ -112,7 +127,7 @@ func (b *builder) AddAuthorityCheck(check Check) error {
}

func (b *builder) Build() (*Biscuit, error) {
return New(b.rng, b.root, b.symbols, &Block{
return New(b.rng, b.signMethod, b.symbols, &Block{
symbols: b.symbols.SplitOff(b.symbolsStart),
facts: b.facts,
rules: b.rules,
Expand Down