diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-19 11:25:40 -0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-19 11:25:40 -0100 |
commit | 86e90ffef8f7ae85e3115c9a0fba887b636b8138 (patch) | |
tree | 6fdd9d920cd3b4be01e3ae5a5c384219265fd53a /lib | |
parent | 596669104e13fd1057120efe95ebbfe460e697ae (diff) | |
parent | 6eb656173704809de0eabbe96ff4463d42eb5697 (diff) |
Merge pull request #37 from nextcloud-gmbh/fixing-verification
source verification
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Db/FollowsRequest.php | 25 | ||||
-rw-r--r-- | lib/Exceptions/ActivityCantBeVerifiedException.php | 8 | ||||
-rw-r--r-- | lib/Model/ActivityPub/ACore.php | 26 | ||||
-rw-r--r-- | lib/Service/ActivityPub/FollowService.php | 28 | ||||
-rw-r--r-- | lib/Service/ActivityPub/NoteService.php | 16 |
5 files changed, 74 insertions, 29 deletions
diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php index 66058064..c5e2e8f1 100644 --- a/lib/Db/FollowsRequest.php +++ b/lib/Db/FollowsRequest.php @@ -33,7 +33,6 @@ namespace OCA\Social\Db; use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Model\ActivityPub\Follow; -use OCA\Social\Model\ActivityPub\Person; /** @@ -74,22 +73,22 @@ class FollowsRequest extends FollowsRequestBuilder { /** - * @param Person $actor - * @param Person $remote + * @param string $actorId + * @param string $remoteActorId * * @return Follow * @throws FollowDoesNotExistException */ - public function getByPersons(Person $actor, Person $remote) { + public function getByPersons(string $actorId, string $remoteActorId) { $qb = $this->getFollowsSelectSql(); - $this->limitToActorId($qb, $actor->getId()); - $this->limitToObjectId($qb, $remote->getId()); + $this->limitToActorId($qb, $actorId); + $this->limitToObjectId($qb, $remoteActorId); $cursor = $qb->execute(); $data = $cursor->fetch(); $cursor->closeCursor(); - if ($data === false) { + $this->miscService->log('does not exisst ?'); throw new FollowDoesNotExistException(); } @@ -108,5 +107,17 @@ class FollowsRequest extends FollowsRequestBuilder { } + /** + * @param Follow $follow + */ + public function deleteByPersons(Follow $follow) { + $qb = $this->getFollowsDeleteSql(); + $this->limitToActorId($qb, $follow->getActorId()); + $this->limitToObjectId($qb, $follow->getObjectId()); + + $qb->execute(); + } + + } diff --git a/lib/Exceptions/ActivityCantBeVerifiedException.php b/lib/Exceptions/ActivityCantBeVerifiedException.php new file mode 100644 index 00000000..9fe5f222 --- /dev/null +++ b/lib/Exceptions/ActivityCantBeVerifiedException.php @@ -0,0 +1,8 @@ +<?php + +namespace OCA\Social\Exceptions; + +class ActivityCantBeVerifiedException extends \Exception { + +} + diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index 9fc4ee80..64f59360 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -32,6 +32,7 @@ namespace OCA\Social\Model\ActivityPub; use daita\MySmallPhpTools\Traits\TArrayTools; use JsonSerializable; +use OCA\Social\Exceptions\ActivityCantBeVerifiedException; use OCA\Social\Model\InstancePath; use OCA\Social\Service\ICoreService; @@ -184,20 +185,23 @@ abstract class ACore implements JsonSerializable { /** * @param string $url * - * @return bool + * @throws ActivityCantBeVerifiedException */ - public function verify(string $url): bool { - if (parse_url($this->getId(), PHP_URL_HOST) !== - parse_url($url, PHP_URL_HOST)) - return false; + public function verify(string $url) { + $url1 = parse_url($this->getId()); + $url2 = parse_url($url); - \OC::$server->getLogger()->log(2, '#### ' . json_encode(parse_url($this->getId(), PHP_URL_PORT))); -// if (parse_url($this->getId(), PHP_URL_PORT) !== -// parse_url($url, PHP_URL_HOST)) -// return false; -// + if ($this->get('host', $url1, '1') !== $this->get('host', $url2, '2')) { + throw new ActivityCantBeVerifiedException('activity cannot be verified'); + } - return true; + if ($this->get('scheme', $url1, '1') !== $this->get('scheme', $url2, '2')) { + throw new ActivityCantBeVerifiedException('activity cannot be verified'); + } + + if ($this->getInt('port', $url1, 1) !== $this->getInt('port', $url2, 1)) { + throw new ActivityCantBeVerifiedException('activity cannot be verified'); + } } diff --git a/lib/Service/ActivityPub/FollowService.php b/lib/Service/ActivityPub/FollowService.php index 347e9104..4b716860 100644 --- a/lib/Service/ActivityPub/FollowService.php +++ b/lib/Service/ActivityPub/FollowService.php @@ -106,8 +106,9 @@ class FollowService implements ICoreService { $follow->setObjectId($remoteActor->getId()); try { - $this->followsRequest->getByPersons($actor, $remoteActor); + $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId()); } catch (FollowDoesNotExistException $e) { + $this->miscService->log('CREATE NEW ONE !'); $this->followsRequest->save($follow); $follow->addInstancePath(new InstancePath($remoteActor->getInbox())); @@ -126,7 +127,7 @@ class FollowService implements ICoreService { $remoteActor = $this->personService->getFromAccount($account); try { - $follow = $this->followsRequest->getByPersons($actor, $remoteActor); + $follow = $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId()); $this->followsRequest->delete($follow); } catch (FollowDoesNotExistException $e) { } @@ -180,25 +181,32 @@ class FollowService implements ICoreService { */ public function save(ACore $follow) { /** @var Follow $follow */ - if ($follow->isRoot()) { - $this->followsRequest->save($follow); - $this->confirmFollowRequest($follow); + $follow->verify($follow->getActorId()); + try { + $this->followsRequest->getByPersons($follow->getActorId(), $follow->getObjectId()); + } catch (FollowDoesNotExistException $e) { + $this->followsRequest->save($follow); + $this->confirmFollowRequest($follow); + } } else { $parent = $follow->getParent(); if ($parent->isRoot() === false) { return; } - if ($parent->getType() === Undo::TYPE && $parent->verify($follow->getActorId())) { - $this->followsRequest->delete($follow); + if ($parent->getType() === Undo::TYPE) { + $parent->verify($follow->getActorId()); + $this->followsRequest->deleteByPersons($follow); } - if ($parent->getType() === Reject::TYPE && $parent->verify($follow->getObjectId())) { - $this->followsRequest->delete($follow); + if ($parent->getType() === Reject::TYPE) { + $parent->verify($follow->getObjectId()); + $this->followsRequest->deleteByPersons($follow); } - if ($parent->getType() === Accept::TYPE && $parent->verify($follow->getObjectId())) { + if ($parent->getType() === Accept::TYPE) { + $parent->verify($follow->getObjectId()); $this->followsRequest->accepted($follow); } diff --git a/lib/Service/ActivityPub/NoteService.php b/lib/Service/ActivityPub/NoteService.php index e143ef01..86cefa1d 100644 --- a/lib/Service/ActivityPub/NoteService.php +++ b/lib/Service/ActivityPub/NoteService.php @@ -37,6 +37,7 @@ use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\RequestException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Activity\Create; use OCA\Social\Model\ActivityPub\Note; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Model\InstancePath; @@ -229,7 +230,20 @@ class NoteService implements ICoreService { */ public function save(ACore $note) { /** @var Note $note */ - $this->notesRequest->save($note); + if ($note->isRoot()) { + return; + } + + $parent = $note->getParent(); + if ($parent->isRoot() === false) { + return; + } + + if ($parent->getType() === Create::TYPE) { + $parent->verify(($note->getAttributedTo())); + $this->notesRequest->save($note); + } + } |