Skip to content

Commit f73437c

Browse files
authored
Merge pull request #15 from baranyildirim/main
Add from_private_key_der and from_private_key_pem to PyKeyPair
2 parents 4747d1e + 3f1195e commit f73437c

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

Cargo.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name = "biscuit_auth"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
biscuit-auth = "4.0.0"
11+
biscuit-auth = { version = "4.1.1", features = ["pem"] }
1212
pyo3 = { version = "0.18.3", features = ["extension-module", "chrono"]}
1313
hex = "0.4"
1414
base64 = "0.13.0"

biscuit_auth.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,23 @@ class KeyPair:
372372
@classmethod
373373
def from_private_key(cls, private_key: PrivateKey) -> KeyPair: ...
374374

375+
# Generate a keypair from a DER buffer
376+
#
377+
# :param bytes: private key bytes in DER format
378+
# :type private_key: PrivateKey
379+
# :return: the corresponding keypair
380+
# :rtype: KeyPair
381+
@classmethod
382+
def from_private_key_der(cls, der: bytes) -> KeyPair: ...
383+
384+
#
385+
# :param bytes: private key bytes in PEM format
386+
# :type private_key: PrivateKey
387+
# :return: the corresponding keypair
388+
# :rtype: KeyPair
389+
@classmethod
390+
def from_private_key_pem(cls, pem: str) -> KeyPair: ...
391+
375392
# The public key part
376393
@property
377394
def public_key(self) -> PublicKey: ...

biscuit_test.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from biscuit_auth import Authorizer, Biscuit, BiscuitBuilder, BlockBuilder, Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit
7+
from biscuit_auth import KeyPair,Authorizer, Biscuit, BiscuitBuilder, BlockBuilder, Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit
88

99
def test_fact():
1010
fact = Fact('fact(1, true, "", "Test", hex:aabbcc, 2023-04-29T01:00:00Z)')
@@ -418,3 +418,17 @@ def test_append_on_unverified():
418418

419419
utoken2 = utoken.append(BlockBuilder("check if true"))
420420
assert utoken2.block_source(3) == "check if true;\n"
421+
422+
423+
def test_keypair_from_private_key_der():
424+
private_key_der = bytes.fromhex("302e020100300506032b6570042204200499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65")
425+
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
426+
kp = KeyPair.from_private_key_der(der=private_key_der)
427+
assert kp.private_key.to_hex() == private_key_hex
428+
429+
430+
def test_keypair_from_private_key_pem():
431+
private_key_pem = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIASZaU0NoF3KxABSZj5x1QwVOUZfiSbf6SAzz3qq1T1l\n-----END PRIVATE KEY-----"
432+
private_key_hex = "0499694d0da05dcac40052663e71d50c1539465f8926dfe92033cf7aaad53d65"
433+
kp = KeyPair.from_private_key_pem(pem=private_key_pem)
434+
assert kp.private_key.to_hex() == private_key_hex

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,32 @@ impl PyKeyPair {
701701
PyKeyPair(KeyPair::from(&private_key.0))
702702
}
703703

704+
/// Generate a keypair from a DER buffer
705+
///
706+
/// :param bytes: private key bytes in DER format
707+
/// :type private_key: PrivateKey
708+
/// :return: the corresponding keypair
709+
/// :rtype: KeyPair
710+
#[classmethod]
711+
pub fn from_private_key_der(_: &PyType, der: &[u8]) -> PyResult<Self> {
712+
let kp = KeyPair::from_private_key_der(der)
713+
.map_err(|e: error::Format| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
714+
Ok(PyKeyPair(kp))
715+
}
716+
717+
/// Generate a keypair from a PEM buffer
718+
///
719+
/// :param bytes: private key bytes in PEM format
720+
/// :type private_key: PrivateKey
721+
/// :return: the corresponding keypair
722+
/// :rtype: KeyPair
723+
#[classmethod]
724+
pub fn from_private_key_pem(_: &PyType, pem: &str) -> PyResult<Self> {
725+
let kp = KeyPair::from_private_key_pem(pem)
726+
.map_err(|e: error::Format| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
727+
Ok(PyKeyPair(kp))
728+
}
729+
704730
/// The public key part
705731
#[getter]
706732
pub fn public_key(&self) -> PyPublicKey {

0 commit comments

Comments
 (0)