# HTTP Signatures in Python

**URL:** <https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689>\
**Category:** Python\
**Created:** [November 23, 2022, 5:20pm UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689 "2022-11-23T17:20:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CartyBoston](https://socialhub.activitypub.rocks/letter_avatar_proxy/v4/letter/c/7ab992/32.png) [@CartyBoston](https://socialhub.activitypub.rocks/u/CartyBoston)\
**Post date:** [November 23, 2022, 5:20pm UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689/1 "2022-11-23T17:20:05Z")

</div>

OK I am making progress, deleted my original note so I’d not waste people’s time, I’ll be back, with any luck, with a working example.

---

<div class="post-metadata">

**Author:** ![mro](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/mro/32/1711_2.png) [@mro](https://socialhub.activitypub.rocks/u/mro)\
**Post date:** [November 25, 2022, 11:46am UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689/4 "2022-11-25T11:46:26Z")

</div>

purging is sad, rather tell about your progress, please.

Are you aware of [Help Needed: HTTP Signatures - #24 by mro](https://socialhub.activitypub.rocks/t/help-needed-http-signatures/2458/24)

---

<div class="post-metadata">

**Author:** ![CartyBoston](https://socialhub.activitypub.rocks/letter_avatar_proxy/v4/letter/c/7ab992/32.png) [@CartyBoston](https://socialhub.activitypub.rocks/u/CartyBoston)\
**Post date:** [November 25, 2022, 7:12pm UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689/5 "2022-11-25T19:12:48Z")

</div>

> [@mro](#):
>
> purging is sad, rather tell about your progress, please.

Fair enough, I had been using python package requests-http-signature but was having trouble specifying which headers got encrypted in the signature.

There is an alternative package written by some folks at Funkwhale who have an ActivityPub server, it’s named requests-http-message-signatures and I’ve had better luck.

I am getting a 404 from my mastodon server with the message:

```auto
Verification failed for CartyBoston@farmer.roundpond.net https://farmer.roundpond.net/actor.json using rsa-sha256 (RSASSA-PKCS

```

python script:

```auto
import requests_http_message_signatures

message_file = open("/var/www/html/hello-world.json", "r")
message = message_file.read()

private_key_file = open("/home/carty/keys/private.pem", "rb")
private_key = private_key_file.read()

public_key_file = open("/home/carty/keys/public.pem", "rb")
public_key = public_key_file.read()

auth = requests_http_message_signatures.HTTPSignatureHeaderAuth(
        headers=["(request-target)", "user-agent", "host", "date"],
        algorithm="rsa-sha256",
        key = private_key,
        key_id = "https://farmer.roundpond.net/actor.json#main-key")

r = requests.post("https://mastodon.roundpond.net/inbox", auth=auth, json = message)

```

[https://farmer.roundpond.net/actor.json:](https://farmer.roundpond.net/actor.json:)

```auto
{
        "@context": [
                "https://www.w3.org/ns/activitystreams",
                "https://w3id.org/security/v1"
        ],

        "id": "https://farmer.roundpond.net/actor.json",
        "type": "Person",
        "preferredUsername": "CartyBoston",
        "inbox": "https://farmer.roundpond.net/inbox",

        "publicKey": {
                "id": "https://farmer.roundpond.net/actor.json#main-key",
                "owner": "https://farmer.roundpond.net/actor.json",
                "publicKeyPem": "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnAvHQk/uSeezTMF5kgFtuK3k/YXmipII22z7z5M6zkIJfRomqbNBCL85DUt1y5x6rEwXxmGgS91PVn/FoAqITi5+Z/dHSk5X55S0SB0uC9cx0nJ6SwVHAM3p8iVdh5x5aLNK40fSfc3h85WXMQXhbPEk8MD7lEgIMU5rpR3JCaWsK3ZCNfkCBvI/V59mKE3Ij8o3Fti/VcZJvbGSAaCY4eY9C/OXLgOiaB13svq99oyYFhfnST+baYPLgt6rWO1pcsUikNbO4755i+ZdXPgIOj5Uk3zuUXAlQKaByq4slkL47vZldFVL29IjGNiBeL52M26LTWnScB5LFDBJTkBljwIDAQAB-----END PUBLIC KEY-----"
        }
}

```

Which appears to be the correct public key, my message doc:

```auto
{
	"@context": "https://www.w3.org/ns/activitystreams",

	"id": "https://farmer.roundpond.net/hello-world.json",
	"type": "Create",
	"actor": "https://farmer.roundpond.net/actor",

	"object": {
		"id": "https://farmer.roundpond.net/hello-world.json",
		"type": "Note",
		"published": "2022-11-24T10:33:29+00:00",
		"attributedTo": "https://farmer.roundpond.net/actor",
		"inReplyTo": "https://mastodon.roundpond.net/@CartyBoston/109389086134482031",
		"content": "<p>Carty's blood pressure is 138/88 y'all"</p>",
		"to": "https://www.w3.org/ns/activitystreams#Public"
	}
}

```

Request looks like:

```auto
{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '659', 'Content-Type': 'application/json', 'Date': 'Fri, 25 Nov 2022 18:54:50 GMT', 'Digest': 'SHA-256=USMTIsSA/Ye1xhThe0PKpQx7Y/RL7mkvfoJB9D4u3t0=', 'Signature': 'keyId="https://farmer.roundpond.net/actor.json#main-key",algorithm="rsa-sha256",headers="(request-target) user-agent host date digest",signature="DAt9UpVJ98KpeL8aH3Js3Z7/9ktZ+rfFcW3x++duiAUjQjca7eiVUgLdE4nHKVtGZkORQXBb/0FG3Kf2qJ1GInW4mSzrNTQQdmjqnzVF7nWsmBv53m9vl+Xt1Az44beYgt+YFr59y8t7S/kj8ze1xR+PKY9E6ebjpwMRN0+SsmJy2O2eR8lbz83BnfDlAQr5OHYe5BBnwkvAAbquQeIoj0iTxD+AEYc2CUXDb33PbHkvV5jmQOgRkXamw/iI6PyxdBXXEs5d/u07M2fxdOYJ7hHRwVcWC52SyO3FCgzRuv05cHFkD6Qi6sO6cYJiD0PEfjodvUKr49nmQT8l8tszNw=="'}

```

Any help or further debugging tips very much appreciated, I will come back and post a python version of script in [How to implement a basic ActivityPub server - Mastodon Blog](https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/) as soon as it works.

---

<div class="post-metadata">

**Author:** ![mro](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/mro/32/1711_2.png) [@mro](https://socialhub.activitypub.rocks/u/mro)\
**Post date:** [November 25, 2022, 9:15pm UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689/6 "2022-11-25T21:15:06Z")

</div>

I do similar succesfully [implemented in OCaml](https://codeberg.org/mro/seppo/src/commit/fe41519548f641e94f3b588a0ad05bef219985b5/lib/http.ml#L252) - the signed headers might have to come first.

---

<div class="post-metadata">

**Author:** ![CartyBoston](https://socialhub.activitypub.rocks/letter_avatar_proxy/v4/letter/c/7ab992/32.png) [@CartyBoston](https://socialhub.activitypub.rocks/u/CartyBoston)\
**Post date:** [December 6, 2022, 7:41pm UTC](https://socialhub.activitypub.rocks/t/http-signatures-in-python/2689/7 "2022-12-06T19:41:12Z")

</div>

Got it, see [Python Mastodon Server POST with HTTP Signature - #2 by CartyBoston](https://socialhub.activitypub.rocks/t/python-mastodon-server-post-with-http-signature/2757/2)
