From 29c772098526c42bfa1697bb8e71d87b1ea8bf2a Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 26 Aug 2023 13:08:00 +1200 Subject: [PATCH] activitypub - signature testing --- app/activitypub/util.py | 1 + test_signature.py | 39 ++++++++++ testing_data/body_1.json | 1 + testing_data/body_1_formatted.json | 68 ++++++++++++++++++ testing_data/body_2.json | 1 + testing_data/body_2_formatted.json | 7 ++ testing_data/body_3.json | 1 + testing_data/body_3_formatted.json | 110 +++++++++++++++++++++++++++++ testing_data/digest_1 | 1 + testing_data/digest_2 | 1 + testing_data/digest_3 | 1 + testing_data/signature_1 | 1 + testing_data/signature_2 | 1 + testing_data/signature_3 | 1 + 14 files changed, 234 insertions(+) create mode 100644 test_signature.py create mode 100644 testing_data/body_1.json create mode 100644 testing_data/body_1_formatted.json create mode 100644 testing_data/body_2.json create mode 100644 testing_data/body_2_formatted.json create mode 100644 testing_data/body_3.json create mode 100644 testing_data/body_3_formatted.json create mode 100644 testing_data/digest_1 create mode 100644 testing_data/digest_2 create mode 100644 testing_data/digest_3 create mode 100644 testing_data/signature_1 create mode 100644 testing_data/signature_2 create mode 100644 testing_data/signature_3 diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 5cd38db0..771587c2 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -185,5 +185,6 @@ def find_actor_or_create(actor): user = User.query.filter_by(ap_profile_id=actor).first() # finds users formatted like https://kbin.social/u/tables if user is None: # todo: retrieve user details via webfinger, etc + ... else: return user diff --git a/test_signature.py b/test_signature.py new file mode 100644 index 00000000..5d52b457 --- /dev/null +++ b/test_signature.py @@ -0,0 +1,39 @@ +from app.activitypub.signature import HttpSignature + +def file_get_contents(filename): + with open(filename, 'r') as file: + contents = file.read() + return contents + +body_1 = file_get_contents('testing_data/body_1.json') +body_2 = file_get_contents('testing_data/body_2.json') +body_3 = file_get_contents('testing_data/body_3.json') +digest_1 = file_get_contents('testing_data/digest_1') +digest_2 = file_get_contents('testing_data/digest_2') +digest_3 = file_get_contents('testing_data/digest_3') +signature_1 = file_get_contents('testing_data/signature_1') +signature_2 = file_get_contents('testing_data/signature_2') +signature_3 = file_get_contents('testing_data/signature_3') + +assert digest_1 == HttpSignature.calculate_digest(body_1.encode()) +assert digest_2 == HttpSignature.calculate_digest(body_2.encode()) +assert digest_3 == HttpSignature.calculate_digest(body_3.encode()) + +parsed = HttpSignature.parse_signature(signature_1) +original_signature = sorted(signature_1.split(',')) +processed_signature = sorted(HttpSignature.compile_signature(parsed).split(',')) +assert original_signature == processed_signature + +parsed = HttpSignature.parse_signature(signature_2) +original_signature = sorted(signature_2.split(',')) +processed_signature = sorted(HttpSignature.compile_signature(parsed).split(',')) +assert original_signature == processed_signature + +parsed = HttpSignature.parse_signature(signature_3) +original_signature = sorted(signature_3.split(',')) +processed_signature = sorted(HttpSignature.compile_signature(parsed).split(',')) +assert original_signature == processed_signature + + + +print('Done') diff --git a/testing_data/body_1.json b/testing_data/body_1.json new file mode 100644 index 00000000..21e8c436 --- /dev/null +++ b/testing_data/body_1.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"lemmy":"https://join-lemmy.org/ns#","litepub":"http://litepub.social/ns#","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org/","ChatMessage":"litepub:ChatMessage","commentsEnabled":"pt:commentsEnabled","sensitive":"as:sensitive","matrixUserId":"lemmy:matrixUserId","postingRestrictedToMods":"lemmy:postingRestrictedToMods","removeData":"lemmy:removeData","stickied":"lemmy:stickied","moderators":{"@type":"@id","@id":"lemmy:moderators"},"expires":"as:endTime","distinguished":"lemmy:distinguished","language":"sc:inLanguage","identifier":"sc:identifier"}],"actor":"https://lemmy.ml/c/memes","to":["https://www.w3.org/ns/activitystreams#Public"],"object":{"id":"https://lemmy.blahaj.zone/activities/like/41bed132-3cb9-4571-bdb8-a8c42782b461","actor":"https://lemmy.blahaj.zone/u/RenegadeRangerX","@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"lemmy":"https://join-lemmy.org/ns#","litepub":"http://litepub.social/ns#","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org/","ChatMessage":"litepub:ChatMessage","commentsEnabled":"pt:commentsEnabled","sensitive":"as:sensitive","matrixUserId":"lemmy:matrixUserId","postingRestrictedToMods":"lemmy:postingRestrictedToMods","removeData":"lemmy:removeData","stickied":"lemmy:stickied","moderators":{"@type":"@id","@id":"lemmy:moderators"},"expires":"as:endTime","distinguished":"lemmy:distinguished","language":"sc:inLanguage","identifier":"sc:identifier"}],"object":"https://kbin.social/m/memes@lemmy.ml/t/371636/-/comment/1843783","type":"Like","audience":"https://lemmy.ml/c/memes"},"cc":["https://lemmy.ml/c/memes/followers"],"type":"Announce","id":"https://lemmy.ml/activities/announce/7530ce36-2d23-442d-a2b5-211f615f63ba"} \ No newline at end of file diff --git a/testing_data/body_1_formatted.json b/testing_data/body_1_formatted.json new file mode 100644 index 00000000..7cdbd6a9 --- /dev/null +++ b/testing_data/body_1_formatted.json @@ -0,0 +1,68 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "lemmy": "https://join-lemmy.org/ns#", + "litepub": "http://litepub.social/ns#", + "pt": "https://joinpeertube.org/ns#", + "sc": "http://schema.org/", + "ChatMessage": "litepub:ChatMessage", + "commentsEnabled": "pt:commentsEnabled", + "sensitive": "as:sensitive", + "matrixUserId": "lemmy:matrixUserId", + "postingRestrictedToMods": "lemmy:postingRestrictedToMods", + "removeData": "lemmy:removeData", + "stickied": "lemmy:stickied", + "moderators": { + "@type": "@id", + "@id": "lemmy:moderators" + }, + "expires": "as:endTime", + "distinguished": "lemmy:distinguished", + "language": "sc:inLanguage", + "identifier": "sc:identifier" + } + ], + "actor": "https://lemmy.ml/c/memes", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "object": { + "id": "https://lemmy.blahaj.zone/activities/like/41bed132-3cb9-4571-bdb8-a8c42782b461", + "actor": "https://lemmy.blahaj.zone/u/RenegadeRangerX", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "lemmy": "https://join-lemmy.org/ns#", + "litepub": "http://litepub.social/ns#", + "pt": "https://joinpeertube.org/ns#", + "sc": "http://schema.org/", + "ChatMessage": "litepub:ChatMessage", + "commentsEnabled": "pt:commentsEnabled", + "sensitive": "as:sensitive", + "matrixUserId": "lemmy:matrixUserId", + "postingRestrictedToMods": "lemmy:postingRestrictedToMods", + "removeData": "lemmy:removeData", + "stickied": "lemmy:stickied", + "moderators": { + "@type": "@id", + "@id": "lemmy:moderators" + }, + "expires": "as:endTime", + "distinguished": "lemmy:distinguished", + "language": "sc:inLanguage", + "identifier": "sc:identifier" + } + ], + "object": "https://kbin.social/m/memes@lemmy.ml/t/371636/-/comment/1843783", + "type": "Like", + "audience": "https://lemmy.ml/c/memes" + }, + "cc": [ + "https://lemmy.ml/c/memes/followers" + ], + "type": "Announce", + "id": "https://lemmy.ml/activities/announce/7530ce36-2d23-442d-a2b5-211f615f63ba" +} \ No newline at end of file diff --git a/testing_data/body_2.json b/testing_data/body_2.json new file mode 100644 index 00000000..49fd722b --- /dev/null +++ b/testing_data/body_2.json @@ -0,0 +1 @@ +{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","id":"https:\/\/kbin.social\/f\/object\/503928e8-0474-45bf-84f1-c56b7c838970","type":"Like","actor":"https:\/\/kbin.social\/u\/Infiltrated_ad8271","object":"https:\/\/lemmy.world\/comment\/2742626"} \ No newline at end of file diff --git a/testing_data/body_2_formatted.json b/testing_data/body_2_formatted.json new file mode 100644 index 00000000..62de799a --- /dev/null +++ b/testing_data/body_2_formatted.json @@ -0,0 +1,7 @@ +{ + "@context": "https:\/\/www.w3.org\/ns\/activitystreams", + "id": "https:\/\/kbin.social\/f\/object\/503928e8-0474-45bf-84f1-c56b7c838970", + "type": "Like", + "actor": "https:\/\/kbin.social\/u\/Infiltrated_ad8271", + "object": "https:\/\/lemmy.world\/comment\/2742626" +} \ No newline at end of file diff --git a/testing_data/body_3.json b/testing_data/body_3.json new file mode 100644 index 00000000..9a16ef69 --- /dev/null +++ b/testing_data/body_3.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"lemmy":"https://join-lemmy.org/ns#","litepub":"http://litepub.social/ns#","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org/","ChatMessage":"litepub:ChatMessage","commentsEnabled":"pt:commentsEnabled","sensitive":"as:sensitive","matrixUserId":"lemmy:matrixUserId","postingRestrictedToMods":"lemmy:postingRestrictedToMods","removeData":"lemmy:removeData","stickied":"lemmy:stickied","moderators":{"@type":"@id","@id":"lemmy:moderators"},"expires":"as:endTime","distinguished":"lemmy:distinguished","language":"sc:inLanguage","identifier":"sc:identifier"}],"actor":"https://lemmy.ml/c/asklemmy","to":["https://www.w3.org/ns/activitystreams#Public"],"object":{"id":"https://lemmy.world/activities/create/c22cc7ca-f1fc-4bf4-b591-4a3313430fc0","actor":"https://lemmy.world/u/Mojave","@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"lemmy":"https://join-lemmy.org/ns#","litepub":"http://litepub.social/ns#","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org/","ChatMessage":"litepub:ChatMessage","commentsEnabled":"pt:commentsEnabled","sensitive":"as:sensitive","matrixUserId":"lemmy:matrixUserId","postingRestrictedToMods":"lemmy:postingRestrictedToMods","removeData":"lemmy:removeData","stickied":"lemmy:stickied","moderators":{"@type":"@id","@id":"lemmy:moderators"},"expires":"as:endTime","distinguished":"lemmy:distinguished","language":"sc:inLanguage","identifier":"sc:identifier"}],"to":["https://www.w3.org/ns/activitystreams#Public"],"object":{"type":"Note","id":"https://lemmy.world/comment/2744248","attributedTo":"https://lemmy.world/u/Mojave","to":["https://www.w3.org/ns/activitystreams#Public"],"cc":["https://lemmy.ml/c/asklemmy","https://lemm.ee/u/handsatinizer"],"content":"
No, I’m mentally stable
\n","inReplyTo":"https://lemm.ee/post/5204840","mediaType":"text/html","source":{"content":"No, I'm mentally stable","mediaType":"text/markdown"},"published":"2023-08-24T06:45:08.987710+00:00","tag":[{"href":"https://lemm.ee/u/handsatinizer","name":"@handsatinizer@lemm.ee","type":"Mention"}],"distinguished":false,"audience":"https://lemmy.ml/c/asklemmy"},"cc":["https://lemmy.ml/c/asklemmy","https://lemm.ee/u/handsatinizer"],"tag":[{"href":"https://lemm.ee/u/handsatinizer","name":"@handsatinizer@lemm.ee","type":"Mention"}],"type":"Create","audience":"https://lemmy.ml/c/asklemmy"},"cc":["https://lemmy.ml/c/asklemmy/followers"],"type":"Announce","id":"https://lemmy.ml/activities/announce/187b8b67-611f-43fb-b256-09090e29cff5"} \ No newline at end of file diff --git a/testing_data/body_3_formatted.json b/testing_data/body_3_formatted.json new file mode 100644 index 00000000..44c50aa5 --- /dev/null +++ b/testing_data/body_3_formatted.json @@ -0,0 +1,110 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "lemmy": "https://join-lemmy.org/ns#", + "litepub": "http://litepub.social/ns#", + "pt": "https://joinpeertube.org/ns#", + "sc": "http://schema.org/", + "ChatMessage": "litepub:ChatMessage", + "commentsEnabled": "pt:commentsEnabled", + "sensitive": "as:sensitive", + "matrixUserId": "lemmy:matrixUserId", + "postingRestrictedToMods": "lemmy:postingRestrictedToMods", + "removeData": "lemmy:removeData", + "stickied": "lemmy:stickied", + "moderators": { + "@type": "@id", + "@id": "lemmy:moderators" + }, + "expires": "as:endTime", + "distinguished": "lemmy:distinguished", + "language": "sc:inLanguage", + "identifier": "sc:identifier" + } + ], + "actor": "https://lemmy.ml/c/asklemmy", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "object": { + "id": "https://lemmy.world/activities/create/c22cc7ca-f1fc-4bf4-b591-4a3313430fc0", + "actor": "https://lemmy.world/u/Mojave", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "lemmy": "https://join-lemmy.org/ns#", + "litepub": "http://litepub.social/ns#", + "pt": "https://joinpeertube.org/ns#", + "sc": "http://schema.org/", + "ChatMessage": "litepub:ChatMessage", + "commentsEnabled": "pt:commentsEnabled", + "sensitive": "as:sensitive", + "matrixUserId": "lemmy:matrixUserId", + "postingRestrictedToMods": "lemmy:postingRestrictedToMods", + "removeData": "lemmy:removeData", + "stickied": "lemmy:stickied", + "moderators": { + "@type": "@id", + "@id": "lemmy:moderators" + }, + "expires": "as:endTime", + "distinguished": "lemmy:distinguished", + "language": "sc:inLanguage", + "identifier": "sc:identifier" + } + ], + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "object": { + "type": "Note", + "id": "https://lemmy.world/comment/2744248", + "attributedTo": "https://lemmy.world/u/Mojave", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://lemmy.ml/c/asklemmy", + "https://lemm.ee/u/handsatinizer" + ], + "content": "No, I’m mentally stable
\n", + "inReplyTo": "https://lemm.ee/post/5204840", + "mediaType": "text/html", + "source": { + "content": "No, I'm mentally stable", + "mediaType": "text/markdown" + }, + "published": "2023-08-24T06:45:08.987710+00:00", + "tag": [ + { + "href": "https://lemm.ee/u/handsatinizer", + "name": "@handsatinizer@lemm.ee", + "type": "Mention" + } + ], + "distinguished": false, + "audience": "https://lemmy.ml/c/asklemmy" + }, + "cc": [ + "https://lemmy.ml/c/asklemmy", + "https://lemm.ee/u/handsatinizer" + ], + "tag": [ + { + "href": "https://lemm.ee/u/handsatinizer", + "name": "@handsatinizer@lemm.ee", + "type": "Mention" + } + ], + "type": "Create", + "audience": "https://lemmy.ml/c/asklemmy" + }, + "cc": [ + "https://lemmy.ml/c/asklemmy/followers" + ], + "type": "Announce", + "id": "https://lemmy.ml/activities/announce/187b8b67-611f-43fb-b256-09090e29cff5" +} \ No newline at end of file diff --git a/testing_data/digest_1 b/testing_data/digest_1 new file mode 100644 index 00000000..268fbff8 --- /dev/null +++ b/testing_data/digest_1 @@ -0,0 +1 @@ +SHA-256=UAcIS2MuGuDJg5kc/vz64rs6p8T+NAWFONxNDoXUmmc= \ No newline at end of file diff --git a/testing_data/digest_2 b/testing_data/digest_2 new file mode 100644 index 00000000..3233343b --- /dev/null +++ b/testing_data/digest_2 @@ -0,0 +1 @@ +SHA-256=Pa8VfLnz1+Nf53IDe1UccNsCeBhaJ1EHYxOXyObXXqM= \ No newline at end of file diff --git a/testing_data/digest_3 b/testing_data/digest_3 new file mode 100644 index 00000000..3d8da9f4 --- /dev/null +++ b/testing_data/digest_3 @@ -0,0 +1 @@ +SHA-256=FiqS8ZvoDZlukuYpgmGwhAtl2o0PiQKBXDQLXZXJdLg= \ No newline at end of file diff --git a/testing_data/signature_1 b/testing_data/signature_1 new file mode 100644 index 00000000..a58552a6 --- /dev/null +++ b/testing_data/signature_1 @@ -0,0 +1 @@ +keyId="https://lemmy.ml/c/memes#main-key",algorithm="hs2019",headers="(request-target) content-type date digest host",signature="q7zG1Nqc5S5K0boiPjs5IKbOSNsOad0EGL6AMd3F2BQ7AVY881SCVH4zbsker16L55g6W26OWE6vWMaMUXZWFAlY1kY2bCSI+/x81jqSC8DPoXPnX0zVhro+iOkGtB4yphVJBtTqO/HIxgvL4LI7Zgcpfb4VuunsUYChYDRjO+Tu2fFBnfg5JbUgn1kWq/y6OHHqEGoj0zmD5unBPa96v8T2AcKfLDju3ySufySsKF+RBfrTLzjHHEN9uFlZ31nlkj0AYhH/zEXFdhtpOubsl6o2bE0zbq7Ty2+q67LCRJmKF8iO0ZVUDAbOsoiAT3P5p50tv4W/hzfMd4a088GLoQ==" \ No newline at end of file diff --git a/testing_data/signature_2 b/testing_data/signature_2 new file mode 100644 index 00000000..4f9fdad5 --- /dev/null +++ b/testing_data/signature_2 @@ -0,0 +1 @@ +keyId="https://kbin.social/u/Infiltrated_ad8271#main-key",headers="(request-target) date host digest",algorithm="rsa-sha256",signature="e5zneRjEQVQyQhnXqDr6gG+uvM6slbGfbS5XBxkyIXiJhbP3zJYf6OREqyuFW+kUqVhShLWalFTu9/LfIWBOuy+RnG9baogCTSE2eB5VN3PeI8p7CegtshcvGFPpWIEaBaoTmL1C7IU1DSm2CBG9AHuFyQlQURFpbrUImbrD54flQgYPzA3gJnspTSJVzh5s3IBr0cS9ns3VnVrvfn7ti+Gf07xYHH4Z7q7/plJ0vb9CHj9JHJGffATzzaILiB4i2MYcwr+l1WJFtN5hn3eGWB7oXgG9mWC0mZ7q/qoxXB2fTj/Tr98kCwQ+kuz630obh44cGb8Uq4PElB2efeb1Sa37eU/1Hd3KU1AYLQn/SLAYMT2ip37LO1oPGdpwwSIewXlgBjSZgIEftiCayGSqh7MJsoYtxppbQMfLZkpu35/7hl08QftllFI7rHQlil5y4xewSQ1BiN+9t08OCOm7nbKht8NILifkrUCeczTEzkUG7BqVTgHbceMA65y79lK3JDICB+n2bPU++ExnF7SVqTHtSJWA0gBe+O/KgfAb0YqMK5mUIYT82QtA8IF4ueDYiH5pmPxPDXa+RtuXcwiJ8Y5rEWKD4BI0HAe17PyVo+2Cn6FGmy6GAnBXVH2wOGDWtjqOnCp8UBZWb+A0j/CA1Xw0grnFDa7RNY70xtQvwCk=" \ No newline at end of file diff --git a/testing_data/signature_3 b/testing_data/signature_3 new file mode 100644 index 00000000..35fbcc52 --- /dev/null +++ b/testing_data/signature_3 @@ -0,0 +1 @@ +keyId="https://lemmy.ml/c/asklemmy#main-key",algorithm="hs2019",headers="(request-target) content-type date digest host",signature="m4Y7PkL8RsGZ8iRXuaiu4skxEPz37zaFqh9rF10RZCwzDqvy3gxLnbN0l/s1swePMkEG+ZeCCmr3+kGlI3dEke2TmY3GDzUCnibVVXLwrwUiymLIyrZsIZSYFW4y+vs/wmYL7unWLe3Z1YCxaWbMb+APaDAKUv03fc9s4wTq40G99xxVRZB+IY1cuGC2yCulAQSRJwgqQ2KiFTV6B7CoCwWMCNH4JwNNJaZd1Y3a0wEYd5yRAjXTWPVi0oa9AnLBZRQpTbG0k/lkXjJ3/6MDYvLRlVGZ61iT5cJZjwrix4XJllqF0248eEQFnYMTPplgvjrUPlv3fvLY0qO5AQKkuw==" \ No newline at end of file