bovine.crypto
This module includes wrappers for creating cryptographic identities and the functionality to verify signatures of http requests. BovineClient and BovineActor take care of making properly signed requests.
bovine.crypto
build_validate_http_signature
build_validate_http_signature(
key_retriever: Callable[
[str],
Awaitable[
Tuple[str | None, str | None]
| CryptographicIdentifier
| None
],
],
skip_digest_check: bool = False,
)
Creates a validate_signature function. validate_signature takes the request as parameter and returns the owner if the http signature is valid. If you do not wish to use quart (or a compatible framework), you should use build_validate_http_signature_raw.
Example for the key_retriever
argument.
from bovine.crypto.types import CryptographicIdentifier
async def retrieve(key_id):
async with aiohttp.ClientSession() as session:
response = await session.get(key_id)
data = await response.json()
return CryptographicIdentifier.from_publickey(
data.get("publicKey", data)
)
validator = build_validate_http_signature(retrieve)
validator
then accepts as argument a werzeug.wrappers.Request
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key_retriever
|
Callable[[str], Awaitable[Tuple[str | None, str | None] | CryptographicIdentifier | None]]
|
A coroutine that given a key id returns the corresponding CryptographicIdentifier or a tuple |
required |
skip_digest_check
|
bool
|
Set to true to skip digest check |
False
|
Returns:
Type | Description |
---|---|
The coroutine SignatureChecker.validate_signature_request |
Source code in bovine/bovine/crypto/__init__.py
build_validate_http_signature_raw
build_validate_http_signature_raw(
key_retriever: Callable[
[str],
Awaitable[
Tuple[str | None, str | None]
| CryptographicIdentifier
| None
],
],
skip_digest_check: bool = False,
)
Creates a validate_signature function. validate_signature takes
(method, url, headers, body)
as parameters and returns
the owner if the http signature is valid.
The rest of behavior is as build_validate_http_signature
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
skip_digest_check
|
bool
|
Set to true to skip digest check |
False
|
Returns:
Type | Description |
---|---|
The coroutine SignatureChecker.validate_signature |
Source code in bovine/bovine/crypto/__init__.py
generate_ed25519_private_key
generate_ed25519_private_key() -> str
Returns a multicodec/multibase encoded ed25519 private key
generate_rsa_public_private_key
Generates a new pair of RSA public and private keys.
Returns:
Type | Description |
---|---|
Tuple[str, str]
|
pem encoded public and private key |
Source code in bovine/bovine/crypto/__init__.py
private_key_to_did_key
Computes public key in did key form of Ed25519 private key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key_str
|
str
|
multibase/multicodec encoded Ed25519 private key |
required |
Returns:
Type | Description |
---|---|
str
|
did:key |
Source code in bovine/bovine/crypto/__init__.py
validate_moo_auth_signature
async
Validates the Moo-Auth-1 <https://blog.mymath.rocks/2023-03-15/BIN1_Moo_Authentication_and_Authoriation>
_ signature of the request.
Returns the did-key if the signature is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
The request to validate the signature for. |
required | |
domain
|
The domain the request is made to. |
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], Optional[str]]
|
On success the did key and domain, on failure None, None When no domain is passed the did key and None is returned |
Source code in bovine/bovine/crypto/__init__.py
bovine.crypto.digest
This package contains helpers for dealing with digest headers in the various Fediverse settings.
See RFC 9530 Digest Fields for the current relevant RFC.
digest_multibase
Implements the multibase multihash digest, see here. This was proposed to use in the Fediverse in FEP-ef61: Portable Objects.
Source code in bovine/bovine/crypto/digest.py
validate_digest
Validates the digest. First checks the digest
header
then the content-digest
header.
>>> validate_digest({"digest": "sha-256=Kch/yJ/aOjLud24QANj5EK/SfmpAubIsE9BbRcaT5D4="},
... b'mooo')
True
>>> validate_digest({"digest": "SHA-256=Kch/yJ/aOjLud24QANj5EK/SfmpAubIsE9BbRcaT5D4="},
... b'mooo')
True
>>> validate_digest({"content-digest": 'sha-256=:R9+ukoir89XSJSq/sL1qyWYmN9ZG5t+dXSdLwzbierw=:'},
... b'moo')
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers
|
dict
|
The headers of the request |
required |
body
|
bytes
|
The body of the request, currently a warning is raised if body is of type str |
required |
Returns:
Type | Description |
---|---|
bool
|
True if digest is present and valid |
Source code in bovine/bovine/crypto/digest.py
bovine.crypto.multibase
MultiCodec
multibase_58btc_encode
Encodes data
in base 58 using the bitcoin alphabet
and adds the prefix z