Skip to content

Commit e51c1c2

Browse files
authored
Add SetContext on Builder interface and GetContext on Biscuit struct. (#153)
1 parent 6cde69d commit e51c1c2

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

biscuit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ func (b *Biscuit) Checks() [][]datalog.Check {
386386
return result
387387
}
388388

389+
func (b *Biscuit) GetContext() string {
390+
if b == nil || b.authority == nil {
391+
return ""
392+
}
393+
394+
return b.authority.context
395+
}
396+
389397
func (b *Biscuit) Serialize() ([]byte, error) {
390398
return proto.Marshal(b.container)
391399
}

biscuit_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func TestBiscuit(t *testing.T) {
1414
rng := rand.Reader
1515
const rootKeyID = 123
16+
const contextText = "current_context"
1617
publicRoot, privateRoot, _ := ed25519.GenerateKey(rng)
1718

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

34+
builder.SetContext(contextText)
35+
3336
b1, err := builder.Build()
3437
require.NoError(t, err)
38+
require.EqualValues(t, contextText, b1.GetContext(), "context authority")
3539
{
3640
keyID := b1.RootKeyID()
3741
require.NotNil(t, keyID, "root key ID present")

builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Builder interface {
2222
AddAuthorityFact(fact Fact) error
2323
AddAuthorityRule(rule Rule) error
2424
AddAuthorityCheck(check Check) error
25+
SetContext(string)
2526
Build() (*Biscuit, error)
2627
}
2728

@@ -113,6 +114,10 @@ func (b *builderOptions) AddAuthorityCheck(check Check) error {
113114
return nil
114115
}
115116

117+
func (b *builderOptions) SetContext(context string) {
118+
b.context = context
119+
}
120+
116121
func (b *builderOptions) Build() (*Biscuit, error) {
117122
opts := make([]biscuitOption, 0, 2)
118123
if v := b.rng; v != nil {

0 commit comments

Comments
 (0)