Skip to content

Commit

Permalink
Add SetContext on Builder interface and GetContext on Biscuit struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit12345 committed Dec 5, 2024
1 parent 6cde69d commit 81ab15f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions biscuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ func (b *Biscuit) Checks() [][]datalog.Check {
return result
}

func (b *Biscuit) GetContext() string {
if b == nil || b.authority == nil {
return ""
}

return b.authority.context
}

func (b *Biscuit) Serialize() ([]byte, error) {
return proto.Marshal(b.container)
}
Expand Down
4 changes: 4 additions & 0 deletions biscuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func TestBiscuit(t *testing.T) {
rng := rand.Reader
const rootKeyID = 123
const contextText = "current_context"
publicRoot, privateRoot, _ := ed25519.GenerateKey(rng)

builder := NewBuilder(
Expand All @@ -30,8 +31,11 @@ func TestBiscuit(t *testing.T) {
Predicate: Predicate{Name: "right", IDs: []Term{String("/a/file2"), String("read")}},
})

builder.SetContext(contextText)

b1, err := builder.Build()
require.NoError(t, err)
require.EqualValues(t, contextText, b1.GetContext(), "context authority")
{
keyID := b1.RootKeyID()
require.NotNil(t, keyID, "root key ID present")
Expand Down
5 changes: 5 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Builder interface {
AddAuthorityFact(fact Fact) error
AddAuthorityRule(rule Rule) error
AddAuthorityCheck(check Check) error
SetContext(string)
Build() (*Biscuit, error)
}

Expand Down Expand Up @@ -113,6 +114,10 @@ func (b *builderOptions) AddAuthorityCheck(check Check) error {
return nil
}

func (b *builderOptions) SetContext(context string) {
b.context = context
}

func (b *builderOptions) Build() (*Biscuit, error) {
opts := make([]biscuitOption, 0, 2)
if v := b.rng; v != nil {
Expand Down

0 comments on commit 81ab15f

Please sign in to comment.