-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
319 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import json | ||
import logging | ||
from typing import (Dict, Any) | ||
import aiohttp | ||
import requests | ||
from settings import BASE_URL | ||
from models.user import UserProfile | ||
from activityPub.data_signature import generate_signature, sign_headers, HTTPSignaturesAuthRequest | ||
from activityPub.key import CryptoKey | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def push_to_remote_actor(target: UserProfile, body: Dict) -> Any: | ||
|
||
""" | ||
Send activity to target inbox | ||
""" | ||
|
||
# Create Key | ||
k = CryptoKey(body["actor"]) | ||
k.new() | ||
generate_signature(body, k) | ||
|
||
data = json.dumps(body) | ||
print(data) | ||
|
||
auth = HTTPSignaturesAuthRequest(k) | ||
r = requests.post(target.uris.inbox, json=body, auth = auth) | ||
print(r.status_code) | ||
#print(r.request.headers) | ||
#print(r.content) | ||
if r.status_code < 400: | ||
return True | ||
|
||
return False | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import base64 | ||
from typing import (Any, Dict, Optional) | ||
|
||
from Crypto.PublicKey import RSA | ||
from Crypto.Util import number | ||
|
||
from database import DATABASE | ||
from keys import import_keys | ||
|
||
|
||
class CryptoKey: | ||
|
||
DEFAULT_KEY_SIZE = 2048 | ||
|
||
def __init__(self, owner: str, id_: Optional[str]=None) -> None: | ||
self.owner = owner | ||
self.privkey_pem: Optional[str] = None | ||
self.pubkey_pem: Optional[str] = None | ||
self.privkey: Optional[Any] = None | ||
self.pubkey: Optional[Any] = None | ||
self.id_ = id_ | ||
|
||
def new(self) -> None: | ||
self.load(import_keys()["actorKeys"]["privateKey"]) | ||
|
||
def key_id(self) -> str: | ||
return f'{self.owner}#main-key' | ||
|
||
def load_pub(self, pubkey_pem: str) -> None: | ||
self.pubkey_pem = pubkey_pem | ||
self.pubkey = RSA.importKey(pubkey_pem) | ||
|
||
def load(self, privkey_pem: str) -> None: | ||
self.privkey_pem = privkey_pem | ||
self.privkey = RSA.importKey(self.privkey_pem) | ||
self.pubkey_pem = self.privkey.publickey().exportKey("PEM").decode("utf-8") | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
return { | ||
"id": self.key_id(), | ||
"owner": self.owner, | ||
"publicKeyPem": self.pubkey_pem, | ||
} | ||
|
||
@classmethod | ||
def from_dict(cls, data): | ||
try: | ||
k = cls(data["owner"], data["id"]) | ||
k.load_pub(data["publicKeyPem"]) | ||
except KeyError: | ||
raise ValueError(f"bad key data {data!r}") | ||
return k | ||
|
||
def to_magic_key(self) -> str: | ||
mod = base64.urlsafe_b64encode( | ||
number.long_to_bytes(self.privkey.n) # type: ignore | ||
).decode("utf-8") | ||
pubexp = base64.urlsafe_b64encode( | ||
number.long_to_bytes(self.privkey.e) # type: ignore | ||
).decode("utf-8") | ||
return f"data:application/magic-public-key,RSA.{mod}.{pubexp}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.