Skip to content

Commit

Permalink
Merge pull request biscuit-auth#15 from baranyildirim/main
Browse files Browse the repository at this point in the history
Add from_private_key_der and from_private_key_pem to PyKeyPair
  • Loading branch information
divarvel authored Feb 17, 2024
2 parents 4747d1e + 3f1195e commit f73437c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "biscuit_auth"
crate-type = ["cdylib"]

[dependencies]
biscuit-auth = "4.0.0"
biscuit-auth = { version = "4.1.1", features = ["pem"] }
pyo3 = { version = "0.18.3", features = ["extension-module", "chrono"]}
hex = "0.4"
base64 = "0.13.0"
Expand Down
17 changes: 17 additions & 0 deletions biscuit_auth.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,23 @@ class KeyPair:
@classmethod
def from_private_key(cls, private_key: PrivateKey) -> KeyPair: ...

# Generate a keypair from a DER buffer
#
# :param bytes: private key bytes in DER format
# :type private_key: PrivateKey
# :return: the corresponding keypair
# :rtype: KeyPair
@classmethod
def from_private_key_der(cls, der: bytes) -> KeyPair: ...

#
# :param bytes: private key bytes in PEM format
# :type private_key: PrivateKey
# :return: the corresponding keypair
# :rtype: KeyPair
@classmethod
def from_private_key_pem(cls, pem: str) -> KeyPair: ...

# The public key part
@property
def public_key(self) -> PublicKey: ...
Expand Down
16 changes: 15 additions & 1 deletion biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from biscuit_auth import Authorizer, Biscuit, BiscuitBuilder, BlockBuilder, Check, Fact, KeyPair, Policy, PrivateKey, PublicKey, Rule, UnverifiedBiscuit
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)')
Expand Down Expand Up @@ -418,3 +418,17 @@ def test_append_on_unverified():

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
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,32 @@ impl PyKeyPair {
PyKeyPair(KeyPair::from(&private_key.0))
}

/// Generate a keypair from a DER buffer
///
/// :param bytes: private key bytes in DER format
/// :type private_key: PrivateKey
/// :return: the corresponding keypair
/// :rtype: KeyPair
#[classmethod]
pub fn from_private_key_der(_: &PyType, der: &[u8]) -> PyResult<Self> {
let kp = KeyPair::from_private_key_der(der)
.map_err(|e: error::Format| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
Ok(PyKeyPair(kp))
}

/// Generate a keypair from a PEM buffer
///
/// :param bytes: private key bytes in PEM format
/// :type private_key: PrivateKey
/// :return: the corresponding keypair
/// :rtype: KeyPair
#[classmethod]
pub fn from_private_key_pem(_: &PyType, pem: &str) -> PyResult<Self> {
let kp = KeyPair::from_private_key_pem(pem)
.map_err(|e: error::Format| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
Ok(PyKeyPair(kp))
}

/// The public key part
#[getter]
pub fn public_key(&self) -> PyPublicKey {
Expand Down

0 comments on commit f73437c

Please sign in to comment.