-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description - Move `parser.zig` into `biscuit-builder` - Cleans up some of the lifetime stuff: - `Biscuit.fromBytes` can now take _any_ `mem.Allocator` - Internally it now creates an `ArenaAllocator` (this is heap allocated because some objects created during `Biscuit.fromBytes` retain their allocator which would be the `arena` in `Biscuit.fromBytes` that is pointing to the stack-allocated `ArenaAllocator`) - A similar change is made to `SerializedBiscuit` - Adds `fn seal` to `SerializedBiscuit` that does the sealing (but doesn't serialize)
- Loading branch information
1 parent
9aaff35
commit e733306
Showing
40 changed files
with
1,187 additions
and
486 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
# biscuit-zig | ||
|
||
```sh | ||
protoc --zig_out=. schema.proto | ||
Zig implementation of https://www.biscuitsec.org/ | ||
|
||
## Usage | ||
|
||
### Authorizing a token | ||
|
||
```zig | ||
var biscuit = try Biscuit.fromBytes(allocator, token, root_public_key); | ||
defer biscuit.deinit(); | ||
var authorizer = try biscuit.authorizer(); | ||
defer authorizer.deinit(); | ||
var errors = std.ArrayList(AuthorizerError).init(allocator); | ||
defer errors.deinit(); | ||
try authorizer.authorize(&errors); | ||
``` | ||
|
||
### Attenuating a token | ||
|
||
```zig | ||
var biscuit = try Biscuit.fromBytes(allocator, token, root_public_key); | ||
defer biscuit.deinit(); | ||
var authorizer = try biscuit.authorizer(); | ||
defer authorizer.deinit(); | ||
var errors = std.ArrayList(AuthorizerError).init(allocator); | ||
defer errors.deinit(); | ||
try authorizer.authorize(&errors); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const std = @import("std"); | ||
const Fact = @import("fact.zig").Fact; | ||
const Rule = @import("rule.zig").Rule; | ||
const Check = @import("check.zig").Check; | ||
const Scope = @import("scope.zig").Scope; | ||
const Parser = @import("parser.zig").Parser; | ||
|
||
const log = std.log.scoped(.builder_block); | ||
|
||
/// Block builder that allows us to append blocks to a token | ||
pub const Block = struct { | ||
arena: std.mem.Allocator, | ||
context: ?[]const u8, | ||
facts: std.ArrayList(Fact), | ||
rules: std.ArrayList(Rule), | ||
checks: std.ArrayList(Check), | ||
scopes: std.ArrayList(Scope), | ||
|
||
/// Initialise a new block builder. | ||
/// | ||
/// This can take any std.mem.Allocator but by design allocations | ||
/// leak so the caller should pass in an ArenaAllocator or other | ||
/// allocator with arena-like properties. | ||
pub fn init(arena: std.mem.Allocator) Block { | ||
return .{ | ||
.arena = arena, | ||
.context = null, | ||
.facts = std.ArrayList(Fact).init(arena), | ||
.rules = std.ArrayList(Rule).init(arena), | ||
.checks = std.ArrayList(Check).init(arena), | ||
.scopes = std.ArrayList(Scope).init(arena), | ||
}; | ||
} | ||
|
||
pub fn addFact(block: *Block, input: []const u8) !void { | ||
log.debug("addFact = {s}", .{input}); | ||
defer log.debug("addFact = {s}", .{input}); | ||
|
||
var parser = Parser.init(block.arena, input); | ||
|
||
const fact = try parser.fact(); | ||
|
||
try block.facts.append(fact); | ||
} | ||
|
||
pub fn addRule(block: *Block, input: []const u8) !void { | ||
log.debug("addRule = {s}", .{input}); | ||
defer log.debug("addRule = {s}", .{input}); | ||
|
||
var parser = Parser.init(block.arena, input); | ||
|
||
const rule = try parser.rule(); | ||
|
||
try block.rules.append(rule); | ||
} | ||
|
||
pub fn addCheck(block: *Block, input: []const u8) !void { | ||
log.debug("addCheck = {s}", .{input}); | ||
defer log.debug("addCheck = {s}", .{input}); | ||
|
||
var parser = Parser.init(block.arena, input); | ||
|
||
const check = try parser.check(); | ||
|
||
try block.checks.append(check); | ||
} | ||
|
||
pub fn addScope(block: *Block, input: []const u8) !void { | ||
log.debug("addScope = {s}", .{input}); | ||
defer log.debug("addScope = {s}", .{input}); | ||
|
||
var parser = Parser.init(block.arena, input); | ||
|
||
const scope = try parser.scope(); | ||
|
||
try block.scopes.append(scope); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.