44
55import pytest
66
7- from biscuit_auth import KeyPair , Authorizer , AuthorizerBuilder , Biscuit , BiscuitBuilder , BlockBuilder , Check , Fact , KeyPair , Policy , PrivateKey , PublicKey , Rule , UnverifiedBiscuit
7+ from biscuit_auth import Algorithm , KeyPair , Authorizer , AuthorizerBuilder , Biscuit , BiscuitBuilder , BlockBuilder , Check , Fact , KeyPair , Policy , PrivateKey , PublicKey , Rule , UnverifiedBiscuit
88
99def test_fact ():
1010 fact = Fact ('fact(1, true, "", "Test", hex:aabbcc, 2023-04-29T01:00:00Z)' )
@@ -13,7 +13,7 @@ def test_fact():
1313
1414def test_biscuit_builder ():
1515 kp = KeyPair ()
16- pubkey = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
16+ pubkey = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
1717
1818 builder = BiscuitBuilder (
1919 """
@@ -81,7 +81,7 @@ def test_biscuit_builder():
8181 assert True
8282
8383def test_block_builder ():
84- pubkey = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
84+ pubkey = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
8585 builder = BlockBuilder (
8686 """
8787 string({str});
@@ -134,7 +134,7 @@ def test_block_builder():
134134"""
135135
136136def test_authorizer_builder ():
137- pubkey = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
137+ pubkey = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
138138 builder = AuthorizerBuilder (
139139 """
140140 string({str});
@@ -207,7 +207,7 @@ def test_authorizer_limits():
207207
208208
209209def test_key_selection ():
210- private_key = PrivateKey . from_hex ( " 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
210+ private_key = PrivateKey ( "ed25519/ 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
211211 root = KeyPair .from_private_key (private_key )
212212 other_root = KeyPair ()
213213
@@ -238,7 +238,7 @@ def choose_key(kid):
238238 pass
239239
240240def test_complete_lifecycle ():
241- private_key = PrivateKey . from_hex ( " 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
241+ private_key = PrivateKey ( "ed25519/ 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
242242 root = KeyPair .from_private_key (private_key )
243243
244244 biscuit_builder = BiscuitBuilder ("user({id})" , { 'id' : "1234" })
@@ -267,7 +267,7 @@ def test_complete_lifecycle():
267267 assert facts [0 ].terms == ["1234" ]
268268
269269def test_snapshot ():
270- private_key = PrivateKey . from_hex ( " 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
270+ private_key = PrivateKey ( "ed25519/ 473b5189232f3f597b5c2f3f9b0d5e28b1ee4e7cce67ec6b7fbf5984157a6b97" )
271271 root = KeyPair .from_private_key (private_key )
272272
273273 biscuit_builder = BiscuitBuilder ("user({id})" , { 'id' : "1234" })
@@ -318,46 +318,46 @@ def test_snapshot():
318318
319319def test_public_keys ():
320320 # Happy path (hex to bytes and back)
321- public_key_from_hex = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
321+ public_key_from_hex = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
322322 public_key_bytes = bytes (public_key_from_hex .to_bytes ())
323- public_key_from_bytes = PublicKey .from_bytes (public_key_bytes )
324- assert public_key_from_bytes . to_hex ( ) == "acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189"
323+ public_key_from_bytes = PublicKey .from_bytes (public_key_bytes , Algorithm . Ed25519 )
324+ assert repr ( public_key_from_bytes ) == "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189"
325325
326326 # Not valid hex
327327 with pytest .raises (ValueError ):
328- PublicKey . from_hex ("notarealkey" )
328+ PublicKey ("notarealkey" )
329329
330330 # Valid hex, but too short
331331 with pytest .raises (ValueError ):
332- PublicKey . from_hex ( " deadbeef1234" )
332+ PublicKey ( "ed25519/ deadbeef1234" )
333333
334334 # Not enough bytes
335335 with pytest .raises (ValueError ):
336- PublicKey .from_bytes (b"1230fw9ia3" )
336+ PublicKey .from_bytes (b"1230fw9ia3" , Algorithm . Ed25519 )
337337
338338def test_private_keys ():
339339 # Happy path (hex to bytes and back)
340- private_key_from_hex = PrivateKey . from_hex ( " 12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a" )
340+ private_key_from_hex = PrivateKey ( "ed25519/ 12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a" )
341341 private_key_bytes = bytes (private_key_from_hex .to_bytes ())
342- private_key_from_bytes = PrivateKey .from_bytes (private_key_bytes )
343- assert private_key_from_bytes . to_hex ( ) == "12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a"
342+ private_key_from_bytes = PrivateKey .from_bytes (private_key_bytes , Algorithm . Ed25519 )
343+ assert repr ( private_key_from_bytes ) == "ed25519/ 12aca40167fbdd1a11037e9fd440e3d510d9d9dea70a6646aa4aaf84d718d75a"
344344
345345 # Not valid hex
346346 with pytest .raises (ValueError ):
347- PrivateKey . from_hex ("notarealkey" )
347+ PrivateKey ("notarealkey" )
348348
349349 # Valid hex, but too short
350350 with pytest .raises (ValueError ):
351- PrivateKey . from_hex ( " deadbeef1234" )
351+ PrivateKey ( "ed25519/ deadbeef1234" )
352352
353353 # Not enough bytes
354354 with pytest .raises (ValueError ):
355- PrivateKey .from_bytes (b"1230fw9ia3" )
355+ PrivateKey .from_bytes (b"1230fw9ia3" , Algorithm . Ed25519 )
356356
357357
358358def test_biscuit_inspection ():
359359 kp = KeyPair ()
360- pubkey = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
360+ pubkey = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
361361
362362 builder = BiscuitBuilder (
363363 """
@@ -391,7 +391,7 @@ def test_biscuit_inspection():
391391 assert utoken2 .block_source (1 ) == "test(false);\n "
392392
393393def test_biscuit_revocation_ids ():
394- public_key = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
394+ public_key = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
395395
396396 token = Biscuit .from_base64 ("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==" , public_key )
397397 utoken = UnverifiedBiscuit .from_base64 ("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==" )
@@ -408,7 +408,7 @@ def test_biscuit_revocation_ids():
408408 ]
409409
410410def test_unverified_biscuit_signature_check ():
411- public_key = PublicKey . from_hex ( " acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
411+ public_key = PublicKey ( "ed25519/ acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189" )
412412
413413 utoken = UnverifiedBiscuit .from_base64 ("Ep8BCjUKB3VzZXJfaWQKBWFsaWNlCgVmaWxlMRgDIgoKCAiACBIDGIEIIg4KDAgHEgMYgQgSAxiCCBIkCAASINFHns5iUW6aZiXA0GoqXyrpvFXrquiRGBZjPy3VJPoHGkAC0oew5bIngBkvg1FThYPBf30CAOBksyofzweJnmT_sQ5N4yT1xevHLImmPkJDFyJs9VXrQtroGy_UY5z3WREIGsgBCl4KATAKATEYAyouCgsIBBIDCIMIEgIYABIHCAISAwiDCBIICIAIEgMIhAgSDAgHEgMIhAgSAwiDCDIkCiIKAggbEgcIAhIDCIMIEgYIAxICGAASCwgEEgMIgwgSAhgAEiQIABIg2QCord1Cw30xC2Oea3AuAOPZ2Mvm9-EQmV3zN7zXwQEaQCLnXqIAz3srYrOJKY_g3slzt_nH5U52w8QYEdcuqCxoInvJB5t9BZht4X75MBzM3Aj1AjRVOGmH0ebuQ5GxnwYagwEKGQoFZmlsZTIYAyIOCgwIBxIDGIEIEgMYhQgSJAgAEiAMOoeV68xL1RTh_y4VeK3DUDBP_gnlPSsckzo87Pf7ihpAFAo2Mf7K5VC1HlC5uCK5R_tIXIAHCzRIL6EWzepWAUAWSh0KlZtA_tinJ-L2LAtXY1dgxIjIvw7agO5ZFVjECSIiCiBuSznFYC0NJn8VmDlZmiq1GpBSOERAwHjLZoQJG_24NA==" )
414414
@@ -423,13 +423,13 @@ def test_append_on_unverified():
423423
424424def test_keypair_from_private_key_der ():
425425 private_key_der = bytes .fromhex ("302e020100300506032b6570042204200499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65" )
426- private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
427- kp = KeyPair .from_private_key_der ( der = private_key_der )
428- assert kp .private_key . to_hex ( ) == private_key_hex
426+ private_key_hex = "ed25519/ 0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
427+ kp = KeyPair .from_private_key ( PrivateKey . from_der ( der = private_key_der ) )
428+ assert repr ( kp .private_key ) == private_key_hex
429429
430430
431431def test_keypair_from_private_key_pem ():
432432 private_key_pem = "-----BEGIN PRIVATE KEY-----\n MC4CAQAwBQYDK2VwBCIEIASZaU0NoF3KxABSZj5x1QwVOUZfiSbf6SAzz3qq1T1l\n -----END PRIVATE KEY-----"
433- private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
434- kp = KeyPair .from_private_key_pem ( pem = private_key_pem )
435- assert kp .private_key . to_hex ( ) == private_key_hex
433+ private_key_hex = "ed25519/ 0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
434+ kp = KeyPair .from_private_key ( PrivateKey . from_pem ( pem = private_key_pem ) )
435+ assert repr ( kp .private_key ) == private_key_hex
0 commit comments