Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions biscuit_auth.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,58 @@ class PublicKey:
# :rtype: list
def to_bytes(self) -> bytes: ...

# Serializes a public key to DER format
#
# :return: the public key DER bytes
# :rtype: bytes
def to_der(self) -> bytes: ...

# Serializes a public key to PEM format
#
# :return: the public key PEM string
# :rtype: str
def to_pem(self) -> str: ...

# Deserializes a public key from raw bytes
#
# :param data: the raw bytes
# :type data: bytes
# :return: the public key
# :rtype: PublicKey
@classmethod
def from_bytes(cls, data: bytes, alg: Algorithm) -> PublicKey: ...

# Deserializes a public key from DER bytes
#
# :param der: the DER bytes
# :type der: bytes
# :return: the public key
# :rtype: PublicKey
@classmethod
def from_der(cls, der: bytes) -> PublicKey: ...

# Deserializes a public key from a PEM string
#
# :param pem: the PEM string
# :type pem: str
# :return: the public key
# :rtype: PublicKey
@classmethod
def from_pem(cls, pem: str) -> PublicKey: ...

# Deserializes a public key from a hexadecimal string
#
# :param data: the hex-encoded string
# :type data: str
# :return: the public key
# :rtype: PublicKey
def __new__(cls, data: str) -> PublicKey: ...
# Serializes a public key to raw bytes
#
# :return: the public key bytes
# :rtype: list
def to_bytes(self) -> bytes: ...

# Deserializes a public key from raw bytes
#
# :param data: the raw bytes
Expand Down
32 changes: 32 additions & 0 deletions biscuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,35 @@ def test_read_third_party_facts():
facts = authorizer.query(rule)

assert facts[0].terms[0] == "56"

def test_private_key_to_and_from_pem():
keypair = KeyPair()
private_key_pem = keypair.private_key.to_pem()
assert type(private_key_pem) == str

private_key = PrivateKey.from_pem(pem=private_key_pem)
assert repr(keypair.private_key) == repr(private_key)

def test_private_key_to_and_from_der():
keypair = KeyPair()
private_key_der = keypair.private_key.to_der()
assert type(private_key_der) == bytes

private_key = PrivateKey.from_der(private_key_der)
assert repr(keypair.private_key) == repr(private_key)

def test_public_key_to_and_from_pem():
keypair = KeyPair()
public_key_pem = keypair.public_key.to_pem()
assert type(public_key_pem) == str

public_key = PublicKey.from_pem(pem=public_key_pem)
assert repr(keypair.public_key) == repr(public_key)

def test_public_key_to_and_from_der():
keypair = KeyPair()
public_key_der = keypair.public_key.to_der()
assert type(public_key_der) == bytes

public_key = PublicKey.from_der(public_key_der)
assert repr(keypair.public_key) == repr(public_key)
48 changes: 46 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,28 @@ impl PyPublicKey {
self.0.to_string()
}

/// Serializes a public key to DER format
///
/// :return: the public key DER bytes
/// :rtype: bytes
pub fn to_der(&self) -> PyResult<Vec<u8>> {
self.0
.to_der()
.map(|der| der.to_vec())
.map_err(|error| PyValueError::new_err(error.to_string()))
}

/// Serializes a public key to PEM format
///
/// :return: the public key PEM string
/// :rtype: str
pub fn to_pem(&self) -> PyResult<String> {
self.0
.to_pem()
.map(|pem| pem.to_string())
.map_err(|error| PyValueError::new_err(error.to_string()))
}

/// Deserializes a public key from raw bytes
///
/// :param data: the raw bytes
Expand Down Expand Up @@ -1230,14 +1252,36 @@ pub struct PyPrivateKey(PrivateKey);

#[pymethods]
impl PyPrivateKey {
/// Serializes a public key to raw bytes
/// Serializes a private key to raw bytes
///
/// :return: the public key bytes
/// :return: the private key bytes
/// :rtype: list
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().deref().clone()
}

/// Serializes a private key to DER format
///
/// :return: the private key DER bytes
/// :rtype: bytes
pub fn to_der(&self) -> PyResult<Vec<u8>> {
self.0
.to_der()
.map(|der| der.to_vec())
.map_err(|error| PyValueError::new_err(error.to_string()))
}

/// Serializes a private key to PEM format
///
/// :return: the private key PEM string
/// :rtype: str
pub fn to_pem(&self) -> PyResult<String> {
self.0
.to_pem()
.map(|pem| pem.to_string())
.map_err(|error| PyValueError::new_err(error.to_string()))
}

/// Serializes a private key to a hexadecimal string
///
/// :return: the private key bytes (hex-encoded)
Expand Down