GithubHelp home page GithubHelp logo

biscuit-go's Introduction

biscuit-go

biscuit-go is an implementation of Biscuit in Go. It aims to be fully compatible with other existing implementations, so that tokens issued by, for example, the Rust version, could be validated by this library and vice versa.

Documentation and specifications

Usage

Create a biscuit

rng := rand.Reader
root := sig.GenerateKeypair(rng)

// retrieve public key with root.Public()
// and share or expose it to verifiers

builder := biscuit.NewBuilder(root)

err := builder.AddAuthorityFact(biscuit.Fact{biscuit.Predicate{
    Name: "right",
    IDs: []biscuit.Term{
        biscuit.Symbol("authority"),
        biscuit.String("/a/file1.txt"),
        biscuit.Symbol("read"),
    },
}})
if err != nil {
    panic(fmt.Errorf("failed to add authority facts: %v", err))
}

// ... add more authority facts, rules, caveats...

b, err := builder.Build()
if err != nil {
    panic(fmt.Errorf("failed to build biscuit: %v", err))
}
token, err := b.Serialize()
if err != nil {
    panic(fmt.Errorf("failed to serialize biscuit: %v", err))
}

// token is now a []byte, ready to be shared

Attenuate a biscuit

b, err = biscuit.Unmarshal(token)
if err != nil {
    panic(fmt.Errorf("failed to deserialize biscuit: %v", err))
}

// Attenuate the biscuit by appending a new block to it
blockBuilder := b.CreateBlock()
blockBuilder.AddFact(biscuit.Fact{/* ... */})

// ... add more facts, rules, caveats...

newKeyPair := sig.GenerateKeypair(rng)
attenuatedBiscuit, err := b.Append(rng, newKeyPair, blockBuilder.Build())
if err != nil {
    panic(fmt.Errorf("failed to append: %v", err))
}
attenuatedToken, err := b.Serialize()
if err != nil {
    panic(fmt.Errorf("failed to serialize biscuit: %v", err))
}

// token is now a []byte attenuation of the original token, and ready to be shared

Verify a biscuit

b, err := biscuit.Unmarshal(token)
if err != nil {
    panic(fmt.Errorf("failed to deserialize token: %v", err))
}

rootPubKey := sig.NewPublicKey([]byte{/* root public key used in create step*/})

verifier, err := b.Verify(rootPubKey)
if err != nil {
    panic(fmt.Errorf("failed to create verifier: %v", err))
}

verifier.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{
    Name: "resource", 
    IDs: []biscuit.Term{
        biscuit.Symbol("ambient"), 
        biscuit.String("/a/file1.txt")
    }
}})

// ... add more ambient facts, rules, caveats...

if err := verifier.Verify(); err != nil {
    fmt.Printf("failed to verify token: %v\n", err)
} else {
    fmt.Println("success verifying token")
}

Using biscuit-go grammar

To ease adding facts, rules, or caveats, a simple grammar and a parser are available, allowing to declare biscuit elements as plain strings. See GRAMMAR reference for the complete syntax.

p := parser.New()
b.AddFact(p.Must().Fact(`resource(#ambient, "/a/file1.txt")`))
b.AddRule(p.Must().Rule(`
    can_read($file) 
        <- resource(#ambient, $file) 
        @ prefix($file, "/a/")
`))

Examples

License

Licensed under Apache License, Version 2.0.

biscuit-go's People

Contributors

daemon63 avatar dependabot-preview[bot] avatar dependabot[bot] avatar titanous avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.