From 81ab15f89d8f25e11a4fd283407cff6d5550430d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Marguerie?= Date: Thu, 5 Dec 2024 17:07:05 +0100 Subject: [PATCH] Add SetContext on Builder interface and GetContext on Biscuit struct. --- biscuit.go | 8 ++++++++ biscuit_test.go | 4 ++++ builder.go | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/biscuit.go b/biscuit.go index 4112bda..b5cf545 100644 --- a/biscuit.go +++ b/biscuit.go @@ -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) } diff --git a/biscuit_test.go b/biscuit_test.go index f099a6b..0aae6d5 100644 --- a/biscuit_test.go +++ b/biscuit_test.go @@ -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( @@ -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") diff --git a/builder.go b/builder.go index ff8641d..3ef9a11 100644 --- a/builder.go +++ b/builder.go @@ -22,6 +22,7 @@ type Builder interface { AddAuthorityFact(fact Fact) error AddAuthorityRule(rule Rule) error AddAuthorityCheck(check Check) error + SetContext(string) Build() (*Biscuit, error) } @@ -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 {