1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
//! Procedural macros to create tokens and authorizers
//!
//! ```rust
//! use biscuit_auth::KeyPair;
//! use biscuit_auth::macros::{authorizer, biscuit, block};
//! use std::time::{Duration, SystemTime};
//!
//! let root = KeyPair::new();
//!
//! let user_id = "1234";
//! let biscuit = biscuit!(
//! r#"
//! // you can directly reference in-scope variables
//! user({user_id});
//! right({user_id}, "file1", {operation});
//! "#,
//! // you can also declare bindings manually
//! operation = "read",
//! ).build(&root).expect("Failed to create biscuit");
//!
//! let new_biscuit = biscuit.append(block!(
//! r#"
//! check if time($time), $time < {expiration};
//! "#,
//! expiration = SystemTime::now() + Duration::from_secs(86_400),
//! )).expect("Failed to append block");
//!
//! authorizer!(
//! r#"
//! time({now});
//! operation({operation});
//! resource({resource});
//!
//! is_allowed($user_id) <- right($user_id, $resource, $operation),
//! resource($resource),
//! operation($operation);
//!
//! allow if is_allowed({user_id});
//! "#,
//! now = SystemTime::now(),
//! operation = "read",
//! resource = "file1",
//! user_id = "1234",
//! )
//! .build(&new_biscuit)
//! .expect("failed to build the authorizer")
//! .authorize()
//! .expect("Failed to authorize biscuit");
//! ```
/// Create an `Authorizer` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// block building.
///
/// ```rust
/// use biscuit_auth::macros::authorizer;
/// use std::time::SystemTime;
///
/// let a = authorizer!(
/// r#"
/// time({now});
/// allow if true;
/// "#,
/// now = SystemTime::now(),
/// );
/// ```
pub use authorizer;
/// Merge facts, rules, checks, and policies into an `Authorizer` from a datalog
/// string and optional parameters. The datalog string is parsed at compile time
/// and replaced by manual block building.
///
/// ```rust
/// use biscuit_auth::macros::{authorizer, authorizer_merge};
/// use std::time::SystemTime;
///
/// let mut b = authorizer!(
/// r#"
/// time({now});
/// "#,
/// now = SystemTime::now()
/// );
///
/// b = authorizer_merge!(
/// b,
/// r#"
/// allow if true;
/// "#
/// );
/// ```
pub use authorizer_merge;
/// Create an `BiscuitBuilder` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// block building.
///
/// ```rust
/// use biscuit_auth::{Biscuit, KeyPair};
/// use biscuit_auth::macros::biscuit;
/// use std::time::{SystemTime, Duration};
///
/// let root = KeyPair::new();
/// let biscuit = biscuit!(
/// r#"
/// user({user_id});
/// check if time($time), $time < {expiration}
/// "#,
/// user_id = "1234",
/// expiration = SystemTime::now() + Duration::from_secs(86_400)
/// ).build(&root);
/// ```
pub use biscuit;
/// Merge facts, rules, and checks into a `BiscuitBuilder` from a datalog
/// string and optional parameters. The datalog string is parsed at compile time
/// and replaced by manual block building.
///
/// ```rust
/// use biscuit_auth::{Biscuit, KeyPair};
/// use biscuit_auth::macros::{biscuit, biscuit_merge};
/// use std::time::{SystemTime, Duration};
///
/// let root = KeyPair::new();
///
/// let mut b = biscuit!(
/// r#"
/// user({user_id});
/// "#,
/// user_id = "1234"
/// );
///
/// b = biscuit_merge!(
/// b,
/// r#"
/// check if time($time), $time < {expiration}
/// "#,
/// expiration = SystemTime::now() + Duration::from_secs(86_400)
/// );
///
/// let biscuit = b.build(&root);
/// ```
pub use biscuit_merge;
/// Create a `BlockBuilder` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// block building.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::block;
///
/// let b = block!(
/// r#"
/// user({user_id});
/// check if user($id);
/// "#,
/// user_id = "1234"
/// );
/// ```
pub use block;
/// Merge facts, rules, and checks into a `BlockBuilder` from a datalog
/// string and optional parameters. The datalog string is parsed at compile time
/// and replaced by manual block building.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::{block, block_merge};
///
/// let mut b = block!(
/// r#"
/// user({user_id});
/// "#,
/// user_id = "1234"
/// );
///
/// b = block_merge!(
/// b,
/// r#"
/// check if user($id);
/// "#
/// );
/// ```
pub use block_merge;
/// Create a `Rule` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// builder calls.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::rule;
///
/// let r = rule!(
/// r#"is_allowed($operation) <- user({user_id}), right({user_id}, $operation)
/// "#,
/// user_id = "1234"
/// );
/// ```
pub use rule;
/// Create a `Fact` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// builder calls.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::fact;
///
/// let f = fact!(
/// r#"user({user_id})"#,
/// user_id = "1234"
/// );
/// ```
pub use fact;
/// Create a `Check` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// builder calls.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::check;
///
/// let c = check!(
/// r#"check if user({user_id})"#,
/// user_id = "1234"
/// );
/// ```
pub use check;
/// Create a `Policy` from a datalog string and optional parameters.
/// The datalog string is parsed at compile time and replaced by manual
/// builder calls.
///
/// ```rust
/// use biscuit_auth::Biscuit;
/// use biscuit_auth::macros::policy;
///
/// let p = policy!(
/// r#"allow if user({user_id})"#,
/// user_id = "1234"
/// );
/// ```
pub use policy;