-
Notifications
You must be signed in to change notification settings - Fork 6
/
biscuit_test.py
444 lines (359 loc) · 18.9 KB
/
biscuit_test.py
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import json
import os
from datetime import datetime, timedelta, timezone
import pytest
from biscuit_auth import KeyPair,Authorizer, Biscuit, BiscuitBuilder, BlockBuilder, Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit
def test_fact():
fact = Fact('fact(1, true, "", "Test", hex:aabbcc, 2023-04-29T01:00:00Z)')
assert fact.name == "fact"
assert fact.terms == [1, True, "", "Test", [0xaa, 0xbb, 0xcc], datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc)]
def test_biscuit_builder():
kp = KeyPair()
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = BiscuitBuilder(
"""
string({str});
int({int});
bool({bool});
bytes({bytes});
datetime({datetime});
set({set});
check if true trusting {pubkey};
""",
{ 'str': "1234",
'int': 1,
'bool': True,
'bytes': [0xaa, 0xbb],
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
'set': {2, True, "Test", datetime(2023, 4, 29, 1, 0, 0, tzinfo = timezone.utc) },
},
{ 'pubkey': pubkey }
)
builder.add_fact(Fact("fact(false)"));
builder.add_fact(Fact("fact({f})", { 'f': True }));
builder.add_rule(Rule("head($var) <- fact($var, {f})", { 'f': True }));
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.merge(BlockBuilder('builder(true);'))
builder.add_code("add_code(true);")
builder.add_code("add_code(true, {f});", { 'f': True})
builder.add_code("check if add_code(true, {f}) trusting {pubkey};", { 'f': True}, { 'pubkey': pubkey })
assert repr(builder) == """// no root key id set
string("1234");
int(1);
bool(true);
bytes(hex:aabb);
datetime(2023-04-03T10:00:00Z);
set([2, "Test", 2023-04-29T01:00:00Z, true]);
fact(false);
fact(true);
builder(true);
add_code(true);
add_code(true, true);
head($var) <- fact($var, true);
head($var) <- fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if add_code(true, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""
builder.build(kp.private_key)
assert True
def test_block_builder():
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = BlockBuilder(
"""
string({str});
int({int});
bool({bool});
bytes({bytes});
datetime({datetime});
check if true trusting {pubkey};
""",
{ 'str': "1234",
'int': 1,
'bool': True,
'bytes': [0xaa, 0xbb],
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
},
{ 'pubkey': pubkey }
)
builder.add_fact(Fact("fact(false)"));
builder.add_fact(Fact("fact({f})", { 'f': True }));
builder.add_rule(Rule("head($var) <- fact($var, {f})", { 'f': True }));
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.merge(BlockBuilder('builder(true);'))
builder.add_code("add_code(true);")
builder.add_code("add_code(true, {f});", { 'f': True})
builder.add_code("check if add_code(true, {f}) trusting {pubkey};", { 'f': True}, { 'pubkey': pubkey })
assert repr(builder) == """string("1234");
int(1);
bool(true);
bytes(hex:aabb);
datetime(2023-04-03T10:00:00Z);
fact(false);
fact(true);
builder(true);
add_code(true);
add_code(true, true);
head($var) <- fact($var, true);
head($var) <- fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if add_code(true, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""
def test_authorizer_builder():
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = Authorizer(
"""
string({str});
int({int});
bool({bool});
bytes({bytes});
datetime({datetime});
check if true trusting {pubkey};
allow if true;
allow if true trusting {pubkey};
""",
{ 'str': "1234",
'int': 1,
'bool': True,
'bytes': [0xaa, 0xbb],
'datetime': datetime(2023, 4, 3, 10, 0, 0, tzinfo = timezone.utc),
},
{ 'pubkey': pubkey }
)
builder.add_fact(Fact("fact(false)"));
builder.add_fact(Fact("fact({f})", { 'f': True }));
builder.add_rule(Rule("head($var) <- fact($var, {f})", { 'f': True }));
builder.add_rule(Rule(
"head($var) <- fact($var, {f}) trusting {pubkey}",
{ 'f': True },
{ 'pubkey': pubkey }
));
builder.add_check(Check("check if fact($var, {f})", { 'f': True }));
builder.add_check(Check("check if fact($var, {f}) trusting {pubkey}", { 'f': True }, { 'pubkey': pubkey }));
builder.add_policy(Policy("allow if fact($var, {f})", { 'f': True}))
builder.add_policy(Policy("allow if fact($var, {f}) trusting {pubkey}", { 'f': True}, { 'pubkey': pubkey }))
builder.merge_block(BlockBuilder('builder(true);'))
builder.merge(Authorizer('builder(false);'))
builder.add_code("add_code(true);")
builder.add_code("add_code(true, {f});", { 'f': True})
builder.add_code("check if add_code(true, {f}) trusting {pubkey};", { 'f': True}, { 'pubkey': pubkey })
try:
builder.authorize()
except:
pass
assert repr(builder) == """// Facts:
// origin: authorizer
add_code(true);
add_code(true, true);
bool(true);
builder(false);
builder(true);
bytes(hex:aabb);
datetime(2023-04-03T10:00:00Z);
fact(false);
fact(true);
int(1);
string("1234");
// Rules:
// origin: authorizer
head($var) <- fact($var, true);
head($var) <- fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
// Checks:
// origin: authorizer
check if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if fact($var, true);
check if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
check if add_code(true, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
// Policies:
allow if true;
allow if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
allow if fact($var, true);
allow if fact($var, true) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;
"""
def test_authorizer_limits():
auth = Authorizer("")
limits = auth.limits()
limits.max_time = timedelta(microseconds=2000)
auth.set_limits(limits)
limits = auth.limits()
assert limits.max_time.microseconds == 2000
def test_key_selection():
private_key = PrivateKey.from_hex("473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97")
root = KeyPair.from_private_key(private_key)
other_root = KeyPair()
def choose_key(kid):
if kid is None:
return other_root.public_key
elif kid == 1:
return root.public_key
else:
raise Exception("Unknown key identifier")
biscuit_builder0 = BiscuitBuilder("user({id})", { 'id': "1234" })
token0 = biscuit_builder0.build(other_root.private_key).to_base64()
biscuit_builder1 = BiscuitBuilder("user({id})", { 'id': "1234" })
biscuit_builder1.set_root_key_id(1)
token1 = biscuit_builder1.build(private_key).to_base64()
biscuit_builder2 = BiscuitBuilder("user({id})", { 'id': "1234" })
biscuit_builder2.set_root_key_id(2)
token2 = biscuit_builder2.build(private_key).to_base64()
Biscuit.from_base64(token0, choose_key)
Biscuit.from_base64(token0, other_root.public_key)
Biscuit.from_base64(token1, choose_key)
try:
Biscuit.from_base64(token2, choose_key)
except:
pass
def test_complete_lifecycle():
private_key = PrivateKey.from_hex("473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97")
root = KeyPair.from_private_key(private_key)
biscuit_builder = BiscuitBuilder("user({id})", { 'id': "1234" })
for right in ["read", "write"]:
biscuit_builder.add_fact(Fact("fact({right})", { 'right': right}))
token = biscuit_builder.build(private_key).append(BlockBuilder('check if user($u)')).to_base64()
parsedToken = Biscuit.from_base64(token, root.public_key)
authorizer = Authorizer("allow if user({id})", { 'id': "1234" })
print(authorizer)
authorizer.add_token(parsedToken)
policy = authorizer.authorize()
assert policy == 0
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
facts = authorizer.query(rule)
assert len(facts) == 1
assert facts[0].name == "u"
assert facts[0].terms == ["1234"]
def test_snapshot():
private_key = PrivateKey.from_hex("473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97")
root = KeyPair.from_private_key(private_key)
biscuit_builder = BiscuitBuilder("user({id})", { 'id': "1234" })
for right in ["read", "write"]:
biscuit_builder.add_fact(Fact("fact({right})", { 'right': right}))
token = biscuit_builder.build(private_key).append(BlockBuilder('check if user($u)')).to_base64()
parsedToken = Biscuit.from_base64(token, root.public_key)
authorizer = Authorizer("allow if user({id})", { 'id': "1234" })
print(authorizer)
authorizer.add_token(parsedToken)
snapshot = authorizer.base64_snapshot()
parsed = Authorizer.from_base64_snapshot(snapshot)
assert repr(authorizer) == repr(parsed)
policy = parsed.authorize()
assert policy == 0
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
facts = parsed.query(rule)
assert len(facts) == 1
assert facts[0].name == "u"
assert facts[0].terms == ["1234"]
# raw_snapshot() returns a list of bytes, not a `bytes` value directly
return
raw_snapshot = authorizer.raw_snapshot()
parsed_from_raw = Authorizer.from_raw_snapshot(raw_snapshot)
assert repr(authorizer) == repr(parsed_from_raw)
raw_policy = raw_parsed.authorize()
assert raw_policy == 0
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
raw_facts = raw_parsed.query(rule)
assert len(raw_facts) == 1
assert raw_facts[0].name == "u"
assert raw_facts[0].terms == ["1234"]
def test_public_keys():
# Happy path (hex to bytes and back)
public_key_from_hex = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
public_key_bytes = bytes(public_key_from_hex.to_bytes())
public_key_from_bytes = PublicKey.from_bytes(public_key_bytes)
assert public_key_from_bytes.to_hex() == "acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189"
# Not valid hex
with pytest.raises(ValueError):
PublicKey.from_hex("notarealkey")
# Valid hex, but too short
with pytest.raises(ValueError):
PublicKey.from_hex("deadbeef1234")
# Not enough bytes
with pytest.raises(ValueError):
PublicKey.from_bytes(b"1230fw9ia3")
def test_private_keys():
# Happy path (hex to bytes and back)
private_key_from_hex = PrivateKey.from_hex("12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a")
private_key_bytes = bytes(private_key_from_hex.to_bytes())
private_key_from_bytes = PrivateKey.from_bytes(private_key_bytes)
assert private_key_from_bytes.to_hex() == "12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a"
# Not valid hex
with pytest.raises(ValueError):
PrivateKey.from_hex("notarealkey")
# Valid hex, but too short
with pytest.raises(ValueError):
PrivateKey.from_hex("deadbeef1234")
# Not enough bytes
with pytest.raises(ValueError):
PrivateKey.from_bytes(b"1230fw9ia3")
def test_biscuit_inspection():
kp = KeyPair()
pubkey = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
builder = BiscuitBuilder(
"""
test({bool});
check if true trusting {pubkey};
""",
{ 'bool': True },
{ 'pubkey': pubkey }
)
print(builder)
token1 = builder.build(kp.private_key)
print(token1.to_base64())
builder.set_root_key_id(42)
token2 = builder.build(kp.private_key).append(BlockBuilder('test(false);'))
print(token2.to_base64())
utoken1 = UnverifiedBiscuit.from_base64(token1.to_base64())
utoken2 = UnverifiedBiscuit.from_base64(token2.to_base64())
assert utoken1.root_key_id() is None
assert utoken2.root_key_id() == 42
assert utoken1.block_count() == 1
assert utoken2.block_count() == 2
assert utoken1.block_source(0) == "test(true);\ncheck if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;\n"
assert utoken2.block_source(0) == "test(true);\ncheck if true trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;\n"
assert utoken2.block_source(1) == "test(false);\n"
def test_biscuit_revocation_ids():
public_key = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
token = Biscuit.from_base64("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==", public_key)
utoken = UnverifiedBiscuit.from_base64("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==")
assert token.revocation_ids == [
"02d287b0e5b22780192f8351538583c17f7d0200e064b32a1fcf07899e64ffb10e4de324f5c5ebc72c89a63e424317226cf555eb42dae81b2fd4639cf7591108",
"22e75ea200cf7b2b62b389298fe0dec973b7f9c7e54e76c3c41811d72ea82c68227bc9079b7d05986de17ef9301cccdc08f5023455386987d1e6ee4391b19f06",
"140a3631fecae550b51e50b9b822b947fb485c80070b34482fa116cdea560140164a1d0a959b40fed8a727e2f62c0b57635760c488c8bf0eda80ee591558c409"
]
assert utoken.revocation_ids == [
"02d287b0e5b22780192f8351538583c17f7d0200e064b32a1fcf07899e64ffb10e4de324f5c5ebc72c89a63e424317226cf555eb42dae81b2fd4639cf7591108",
"22e75ea200cf7b2b62b389298fe0dec973b7f9c7e54e76c3c41811d72ea82c68227bc9079b7d05986de17ef9301cccdc08f5023455386987d1e6ee4391b19f06",
"140a3631fecae550b51e50b9b822b947fb485c80070b34482fa116cdea560140164a1d0a959b40fed8a727e2f62c0b57635760c488c8bf0eda80ee591558c409"
]
def test_unverified_biscuit_signature_check():
public_key = PublicKey.from_hex("acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189")
utoken = UnverifiedBiscuit.from_base64("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==")
utoken.verify(public_key)
def test_append_on_unverified():
utoken = UnverifiedBiscuit.from_base64("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==")
utoken2 = utoken.append(BlockBuilder("check if true"))
assert utoken2.block_source(3) == "check if true;\n"
def test_keypair_from_private_key_der():
private_key_der = bytes.fromhex("302e020100300506032b6570042204200499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65")
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
kp = KeyPair.from_private_key_der(der=private_key_der)
assert kp.private_key.to_hex() == private_key_hex
def test_keypair_from_private_key_pem():
private_key_pem = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIASZaU0NoF3KxABSZj5x1QwVOUZfiSbf6SAzz3qq1T1l\n-----END PRIVATE KEY-----"
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
kp = KeyPair.from_private_key_pem(pem=private_key_pem)
assert kp.private_key.to_hex() == private_key_hex