diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-12-20 11:22:48 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-12-20 11:22:48 -0100 |
commit | 2a41f1ea1dd90b7f381ea6a0bd2e11edee5694a4 (patch) | |
tree | e747c75e42fc85dea46411bd62744e624fa0579a /lib | |
parent | a8fea82b230aeb620c9225222da90d3700c72913 (diff) |
signed header generation moved to SignatureService
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Model/ActivityPub/Object/Note.php | 3 | ||||
-rw-r--r-- | lib/Service/ActivityService.php | 34 | ||||
-rw-r--r-- | lib/Service/SignatureService.php | 48 |
3 files changed, 49 insertions, 36 deletions
diff --git a/lib/Model/ActivityPub/Object/Note.php b/lib/Model/ActivityPub/Object/Note.php index 185f4ec9..dfdc3a11 100644 --- a/lib/Model/ActivityPub/Object/Note.php +++ b/lib/Model/ActivityPub/Object/Note.php @@ -34,6 +34,7 @@ use DateTime; use JsonSerializable; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Service\ActivityService; +use OCA\Social\Service\SignatureService; class Note extends ACore implements JsonSerializable { @@ -195,7 +196,7 @@ class Note extends ACore implements JsonSerializable { */ public function convertPublished() { $dTime = new DateTime($this->getPublished()); - $dTime->format(ActivityService::DATE_FORMAT); + $dTime->format(SignatureService::DATE_FORMAT); $this->setPublishedTime($dTime->getTimestamp()); } diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 30d5e757..0542ac19 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -63,8 +63,6 @@ class ActivityService { const TIMEOUT_ASYNC = 5; const TIMEOUT_SERVICE = 10; - const DATE_FORMAT = 'D, d M Y H:i:s T'; - /** @var NotesRequest */ private $notesRequest; @@ -353,22 +351,6 @@ class ActivityService { public function generateRequestFromQueue(RequestQueue $queue): array { $path = $queue->getInstance(); -// $document = json_encode($activity); - $date = gmdate(self::DATE_FORMAT); - $localActor = $this->getActorFromAuthor($queue->getAuthor()); - - // TODO: move this to SignatureService ? - $localActorLink = - $this->configService->getUrlSocial() . '@' . $localActor->getPreferredUsername(); - $signature = "(request-target): post " . $path->getPath() . "\nhost: " . $path->getAddress() - . "\ndate: " . $date; - openssl_sign($signature, $signed, $localActor->getPrivateKey(), OPENSSL_ALGO_SHA256); - - $signed = base64_encode($signed); - $header = - 'keyId="' . $localActorLink . '",headers="(request-target) host date",signature="' - . $signed . '"'; - $requestType = Request::TYPE_GET; if ($path->getType() === InstancePath::TYPE_INBOX || $path->getType() === InstancePath::TYPE_GLOBAL @@ -379,12 +361,12 @@ class ActivityService { $request = new Request($path->getPath(), $requestType); $request->setTimeout($queue->getTimeout()); $request->addHeader('Host: ' . $path->getAddress()); - $request->addHeader('Date: ' . $date); - $request->addHeader('Signature: ' . $header); $request->setDataJson($queue->getActivity()); $request->setAddress($path->getAddress()); + $this->signatureService->signRequest($request, $queue); + return $this->curlService->request($request); } @@ -407,18 +389,6 @@ class ActivityService { /** - * @param string $author - * - * @return Person - * @throws SocialAppConfigException - * @throws ActorDoesNotExistException - */ - private function getActorFromAuthor(string $author): Person { - return $this->accountService->getFromId($author); - } - - - /** * @param ACore $activity */ private function saveActivity(ACore $activity) { diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index a1424758..799bf2de 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -31,9 +31,12 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; +use OCA\Social\Db\ActorsRequest; +use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\LinkedDataSignatureMissingException; @@ -47,6 +50,7 @@ use OCA\Social\Exceptions\UnknownItemException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\LinkedDataSignature; +use OCA\Social\Model\RequestQueue; use OCP\IRequest; class SignatureService { @@ -59,12 +63,16 @@ class SignatureService { const ORIGIN_SIGNATURE = 2; + const DATE_FORMAT = 'D, d M Y H:i:s T'; const DATE_DELAY = 30; /** @var CacheActorService */ private $cacheActorService; + /** @var ActorsRequest */ + private $actorsRequest; + /** @var CurlService */ private $curlService; @@ -78,15 +86,18 @@ class SignatureService { /** * ActivityService constructor. * + * @param ActorsRequest $actorsRequest * @param CacheActorService $cacheActorService * @param CurlService $curlService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - CacheActorService $cacheActorService, CurlService $curlService, + ActorsRequest $actorsRequest, CacheActorService $cacheActorService, + CurlService $curlService, ConfigService $configService, MiscService $miscService ) { + $this->actorsRequest = $actorsRequest; $this->cacheActorService = $cacheActorService; $this->curlService = $curlService; $this->configService = $configService; @@ -115,6 +126,35 @@ class SignatureService { /** + * @param Request $request + * @param RequestQueue $queue + * + * @throws ActorDoesNotExistException + * @throws SocialAppConfigException + */ + public function signRequest(Request $request, RequestQueue $queue) { + $date = gmdate(self::DATE_FORMAT); + $path = $queue->getInstance(); + + $localActor = $this->actorsRequest->getFromId($queue->getAuthor()); + + $localActorLink = + $this->configService->getUrlSocial() . '@' . $localActor->getPreferredUsername(); + $signature = "(request-target): post " . $path->getPath() . "\nhost: " . $path->getAddress() + . "\ndate: " . $date; + + openssl_sign($signature, $signed, $localActor->getPrivateKey(), OPENSSL_ALGO_SHA256); + $signed = base64_encode($signed); + + $header = 'keyId="' . $localActorLink . '",headers="(request-target) host date",signature="' + . $signed . '"'; + + $request->addHeader('Date: ' . $date); + $request->addHeader('Signature: ' . $header); + } + + + /** * @param IRequest $request * * @return string @@ -130,7 +170,7 @@ class SignatureService { */ public function checkRequest(IRequest $request): string { $dTime = new DateTime($request->getHeader('date')); - $dTime->format(ActivityService::DATE_FORMAT); + $dTime->format(self::DATE_FORMAT); if ($dTime->getTimestamp() < (time() - self::DATE_DELAY)) { throw new SignatureException('object is too old'); @@ -167,7 +207,9 @@ class SignatureService { $signature->import(json_decode($object->getSource(), true)); $signature->setPublicKey($this->retrieveKey($actorId)); if ($signature->verify()) { - $object->setOrigin($this->getKeyOrigin($actorId), SignatureService::ORIGIN_SIGNATURE); + $object->setOrigin( + $this->getKeyOrigin($actorId), SignatureService::ORIGIN_SIGNATURE + ); return true; } |