diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2020-11-30 23:58:32 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2020-11-30 23:58:32 -0100 |
commit | 68660884319e2bb03ebcbe7cdd26434b3473c50b (patch) | |
tree | ac00c2137fb19bac684201a361fe4636a8b2ec9f /lib | |
parent | da20d5901368e7a78be3926f827c6199f285aff1 (diff) |
using TNC20Request
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ActivityPubController.php | 5 | ||||
-rw-r--r-- | lib/Controller/OAuthController.php | 1 | ||||
-rw-r--r-- | lib/Search/UnifiedSearchProvider.php | 5 | ||||
-rw-r--r-- | lib/Service/ActivityService.php | 9 | ||||
-rw-r--r-- | lib/Service/CacheActorService.php | 18 | ||||
-rw-r--r-- | lib/Service/CacheDocumentService.php | 11 | ||||
-rw-r--r-- | lib/Service/ConfigService.php | 20 | ||||
-rw-r--r-- | lib/Service/CurlService.php | 112 | ||||
-rw-r--r-- | lib/Service/SearchService.php | 9 | ||||
-rw-r--r-- | lib/Service/SignatureService.php | 28 | ||||
-rw-r--r-- | lib/Service/TestService.php | 8 |
11 files changed, 120 insertions, 106 deletions
diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 1138bc64..0462b3da 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; +use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use daita\MySmallPhpTools\Traits\TAsync; use daita\MySmallPhpTools\Traits\TStringTools; @@ -65,6 +66,7 @@ class ActivityPubController extends Controller { use TNCDataResponse; use TStringTools; use TAsync; +use TNC20Logger; /** @var SocialPubController */ @@ -246,10 +248,9 @@ class ActivityPubController extends Controller { * @return Response */ public function inbox(string $username): Response { - try { $body = file_get_contents('php://input'); - $this->miscService->log('[<<] inbox: ' . $body, 1); + $this->debug('[<<] inbox', ['body' => $body]); $requestTime = 0; $origin = $this->signatureService->checkRequest($this->request, $body, $requestTime); diff --git a/lib/Controller/OAuthController.php b/lib/Controller/OAuthController.php index 3f46cdc9..23a22c88 100644 --- a/lib/Controller/OAuthController.php +++ b/lib/Controller/OAuthController.php @@ -268,6 +268,7 @@ class OAuthController extends Controller { ], Http::STATUS_OK ); } catch (Exception $e) { + $this->miscService->log($e->getMessage() . ' ' . get_class($e)); return new DataResponse(['error' => $e->getMessage()], Http::STATUS_UNAUTHORIZED); } } diff --git a/lib/Search/UnifiedSearchProvider.php b/lib/Search/UnifiedSearchProvider.php index 9ef522cc..13eae127 100644 --- a/lib/Search/UnifiedSearchProvider.php +++ b/lib/Search/UnifiedSearchProvider.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Search; +use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\Exceptions\AccountDoesNotExistException; @@ -62,6 +63,7 @@ class UnifiedSearchProvider implements IProvider { use TArrayTools; + use TNC20Logger; /** @var IL10N */ @@ -217,7 +219,8 @@ class UnifiedSearchProvider implements IProvider { private function convertAccounts(array $accounts): array { $result = []; foreach ($accounts as $account) { - $icon = ($account->hasIcon()) ? $account->getIcon()->getUrl() : ''; + $icon = ($account->hasIcon()) ? $account->getIcon() + ->getUrl() : ''; $result[] = new UnifiedSearchResult( $icon, $account->getPreferredUsername(), diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 41372d98..9e2d5f38 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Exceptions\RequestNetworkException; use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; use daita\MySmallPhpTools\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; @@ -365,9 +366,9 @@ class ActivityService { /** * @param RequestQueue $queue * - * @return Request + * @return NC20Request */ - private function generateRequestFromQueue(RequestQueue $queue): Request { + private function generateRequestFromQueue(RequestQueue $queue): NC20Request { $path = $queue->getInstance(); $requestType = Request::TYPE_GET; @@ -377,10 +378,10 @@ class ActivityService { $requestType = Request::TYPE_POST; } - $request = new Request($path->getPath(), $requestType); + $request = new NC20Request($path->getPath(), $requestType); $request->setTimeout($queue->getTimeout()); $request->setDataJson($queue->getActivity()); - $request->setAddress($path->getAddress()); + $request->setHost($path->getAddress()); $request->setProtocol($path->getProtocol()); return $request; diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index b448a682..bfb594e8 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -36,6 +36,7 @@ use daita\MySmallPhpTools\Exceptions\RequestNetworkException; use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; use daita\MySmallPhpTools\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\AP; @@ -53,10 +54,16 @@ use OCA\Social\Model\ActivityPub\Actor\Person; use OCP\IURLGenerator; +/** + * Class CacheActorService + * + * @package OCA\Social\Service + */ class CacheActorService { use TArrayTools; + use TNC20Logger; /** @var IURLGenerator */ @@ -129,7 +136,6 @@ class CacheActorService { * @throws UnauthorizedFediverseException */ public function getFromId(string $id, bool $refresh = false): Person { - $posAnchor = strpos($id, '#'); if ($posAnchor !== false) { $id = substr($id, 0, $posAnchor); @@ -144,6 +150,8 @@ class CacheActorService { } catch (CacheActorDoesNotExistException $e) { $object = $this->curlService->retrieveObject($id); + $this->debug('object retrieved', ['id' => $id, 'object' => $object]); + /** @var Person $actor */ $actor = AP::$activityPub->getItemFromData($object); if (!AP::$activityPub->isActor($actor)) { @@ -220,9 +228,15 @@ class CacheActorService { } catch (CacheActorDoesNotExistException $e) { } + $this->debug('getFromAccount', ['account' => $account, 'retrieve' => $retrieve]); + try { $actor = $this->cacheActorsRequest->getFromAccount($account); + + $this->debug('Found Actor', ['account' => $account, 'actor' => $actor]); } catch (CacheActorDoesNotExistException $e) { + $this->debug('Actor not found', ['account' => $account]); + if (!$retrieve) { throw new CacheActorDoesNotExistException(); } @@ -230,6 +244,8 @@ class CacheActorService { $actor = $this->curlService->retrieveAccount($account); $actor->setAccount($account); try { + $this->warning('Saving Actor', false, ['actor' => $actor]); + $this->save($actor); } catch (Exception $e) { throw new InvalidResourceException($e->getMessage()); diff --git a/lib/Service/CacheDocumentService.php b/lib/Service/CacheDocumentService.php index 22fb640d..358cc81b 100644 --- a/lib/Service/CacheDocumentService.php +++ b/lib/Service/CacheDocumentService.php @@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Exceptions\RequestContentException; use daita\MySmallPhpTools\Exceptions\RequestNetworkException; use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; use daita\MySmallPhpTools\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TStringTools; @@ -270,16 +271,14 @@ class CacheDocumentService { * @throws SocialAppConfigException * @throws UnauthorizedFediverseException */ - public function retrieveContent(string $url) { + public function retrieveContent(string $url): string { $url = parse_url($url); $this->mustContains(['path', 'host', 'scheme'], $url); - $request = new Request($url['path'], Request::TYPE_GET, true); - $request->setAddress($url['host']); + $request = new NC20Request($url['path'], Request::TYPE_GET, true); + $request->setHost($url['host']); $request->setProtocol($url['scheme']); - $content = $this->curlService->doRequest($request); - - return $content; + return $this->curlService->doRequest($request); } } diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index b77fda3e..38cac425 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Service; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; @@ -436,10 +437,25 @@ class ConfigService { /** - * @param Request $request + * @param NC20Request $request */ - public function configureRequest(Request $request) { + public function configureRequest(NC20Request $request) { $request->setVerifyPeer($this->getAppValue(ConfigService::SOCIAL_SELF_SIGNED) !== '1'); + + if ($request->getType() === Request::TYPE_GET) { + $request->addHeader( + 'Accept', 'application/json; profile="https://www.w3.org/ns/activitystreams"' + ); + } + + if ($request->getType() === Request::TYPE_POST) { + $request->addHeader( + 'Content-Type', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + ); + } + + $request->setLocalAddressAllowed(true); + $request->setFollowLocation(true); } diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index 2da6f0cf..a3b3b45a 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -37,10 +37,11 @@ use daita\MySmallPhpTools\Exceptions\RequestNetworkException; use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; use daita\MySmallPhpTools\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Model\Request; +use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; -use daita\MySmallPhpTools\Traits\TRequest; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\HostMetaException; @@ -58,14 +59,11 @@ class CurlService { use TArrayTools; use TPathTools; - use TRequest { - initRequestPost as initRequestPostOrig; - initRequestGet as initRequestGetOrig; + use TNC20Request { retrieveJson as retrieveJsonOrig; doRequest as doRequestOrig; } - const ASYNC_REQUEST_TOKEN = '/async/request/{token}'; const USER_AGENT = 'Nextcloud Social'; @@ -96,6 +94,7 @@ class CurlService { $maxDlSize = $this->configService->getAppValue(ConfigService::SOCIAL_MAX_SIZE) * (1024 * 1024); $this->setMaxDownloadSize($maxDlSize); + $this->setup('app', 'social'); } @@ -113,6 +112,7 @@ class CurlService { * @throws UnauthorizedFediverseException */ public function webfingerAccount(string &$account): array { + $this->debug('webfingerAccount', ['account' => $account]); $account = $this->withoutBeginAt($account); // we consider an account is like an email @@ -132,12 +132,14 @@ class CurlService { $path = '/.well-known/webfinger'; } - $request = new Request($path); - $request->addData('resource', 'acct:' . $account); - $request->setAddress($host); + $request = new NC20Request($path); + $request->addParam('resource', 'acct:' . $account); + $request->setHost($host); $request->setProtocols($protocols); $result = $this->retrieveJson($request); + $this->notice('webfingerAccount, request result', false, ['request' => $request]); + $subject = $this->get('subject', $result, ''); list($type, $temp) = explode(':', $subject, 2); if ($type === 'acct') { @@ -156,14 +158,17 @@ class CurlService { * @throws HostMetaException */ public function hostMeta(string &$host, array &$protocols): string { - $request = new Request('/.well-known/host-meta'); - $request->setAddress($host); + $request = new NC20Request('/.well-known/host-meta'); + $request->setHost($host); $request->setProtocols($protocols); + $this->debug('hostMeta', ['host' => $host, 'protocols' => $protocols]); + try { $result = $this->retrieveJson($request); } catch (Exception $e) { - $this->miscService->log('hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0); + $this->exception($e, self::$NOTICE, ['request' => $request]); + throw new HostMetaException(get_class($e) . ' - ' . $e->getMessage()); } @@ -194,6 +199,7 @@ class CurlService { * @throws UnauthorizedFediverseException */ public function retrieveAccount(string &$account): Person { + $this->debug('retrieveAccount', ['account' => $account]); $result = $this->webfingerAccount($account); try { @@ -205,10 +211,12 @@ class CurlService { $id = $this->get('href', $link, ''); $data = $this->retrieveObject($id); + $this->debug('retrieveAccount, details', ['link' => $link, 'data' => $data, 'account' => $account]); + /** @var Person $actor */ $actor = AP::$activityPub->getItemFromData($data); if (!AP::$activityPub->isActor($actor)) { - throw new ItemUnknownException(); + throw new ItemUnknownException(json_encode($actor) . ' is not an Actor'); } if (strtolower($actor->getId()) !== strtolower($id)) { @@ -235,15 +243,20 @@ class CurlService { * @throws UnauthorizedFediverseException */ public function retrieveObject($id): array { + $this->debug('retrieveObject', ['id' => $id]); $url = parse_url($id); $this->mustContains(['path', 'host', 'scheme'], $url); - $request = new Request($url['path'], Request::TYPE_GET); - $request->setAddress($url['host']); + $request = new NC20Request($url['path'], Request::TYPE_GET); + $request->setHost($url['host']); $request->setProtocol($url['scheme']); + $this->debug('retrieveObject', ['request' => $request]); + $result = $this->retrieveJson($request); + $this->notice('retrieveObject, request result', false, ['request' => $request]); + if (is_array($result)) { - $result['_host'] = $request->getAddress(); + $result['_host'] = $request->getHost(); } return $result; @@ -251,35 +264,24 @@ class CurlService { /** - * @param Request $request + * @param NC20Request $request * * @return array * @throws RequestContentException * @throws RequestNetworkException - * @throws RequestResultNotJsonException - * @throws RequestResultSizeException - * @throws RequestServerException */ - public function retrieveJson(Request $request): array { - $this->configService->configureRequest($request); - $this->assignUserAgent($request); - + public function retrieveJson(NC20Request $request): array { try { - $result = $this->retrieveJsonOrig($request); - } catch (RequestResultSizeException | RequestResultNotJsonException $e) { - $this->miscService->log( - '[!!] request: ' . json_encode($request) . ' - content-type: ' - . $request->getContentType() . ' - ' . $e->getMessage(), 1 - ); + return $this->retrieveJsonOrig($request); + } catch (RequestNetworkException | RequestContentException $e) { + $this->exception($e, self::$NOTICE, ['request' => $request]); throw $e; } - - return $result; } /** - * @param Request $request + * @param NC20Request $request * * @return mixed * @throws SocialAppConfigException @@ -289,7 +291,8 @@ class CurlService { * @throws RequestResultSizeException * @throws RequestServerException */ - public function doRequest(Request $request) { + // migration ? + public function doRequest(NC20Request $request) { $this->fediverseService->authorized($request->getAddress()); $this->configService->configureRequest($request); $this->assignUserAgent($request); @@ -299,9 +302,9 @@ class CurlService { /** - * @param Request $request + * @param NC20Request $request */ - public function assignUserAgent(Request $request) { + public function assignUserAgent(NC20Request $request) { $request->setUserAgent( self::USER_AGENT . ' ' . $this->configService->getAppValue('installed_version') ); @@ -320,8 +323,8 @@ class CurlService { $path .= $this->withoutBeginSlash(self::ASYNC_REQUEST_TOKEN); $path = str_replace('{token}', $token, $path); - $request = new Request($path, Request::TYPE_POST); - $request->setAddress($this->configService->getCloudHost()); + $request = new NC20Request($path, Request::TYPE_POST); + $request->setHost($this->configService->getCloudHost()); $request->setProtocol(parse_url($address, PHP_URL_SCHEME)); try { @@ -336,40 +339,5 @@ class CurlService { } - /** - * @param Request $request - */ - protected function initRequestGet(Request $request) { - if ($request->getType() !== Request::TYPE_GET) { - return; - } - - $request->addHeader( - 'Accept: application/json; profile="https://www.w3.org/ns/activitystreams"' - ); - $request->addHeader( - 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"' - ); - - $this->initRequestGetOrig($request); - } - - - /** - * @param resource $curl - * @param Request $request - */ - protected function initRequestPost($curl, Request $request) { - if ($request->getType() !== Request::TYPE_POST) { - return; - } - - $request->addHeader( - 'Content-Type: application/ld+json; profile="https://www.w3.org/ns/activitystreams"' - ); - - $this->initRequestPostOrig($curl, $request); - } - } diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index 9dacaa34..3ae3df86 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -31,15 +31,22 @@ declare(strict_types=1); namespace OCA\Social\Service; +use daita\MySmallPhpTools\Traits\Nextcloud\nc20\TNC20Logger; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\Model\ActivityPub\Actor\Person; +/** + * Class SearchService + * + * @package OCA\Social\Service + */ class SearchService { use TArrayTools; + use TNC20Logger; const SEARCH_ACCOUNTS = 1; @@ -88,6 +95,7 @@ class SearchService { public function searchAccounts(string $search): array { $result = []; $type = $this->getTypeFromSearch($search); + if ($search === '' || !$type & self::SEARCH_ACCOUNTS) { return $result; } @@ -99,6 +107,7 @@ class SearchService { try { $this->cacheActorService->getFromAccount($search); } catch (Exception $e) { + $this->exception($e, self::$NOTICE, ['search' => $search]); } return $this->cacheActorService->searchCachedAccounts($search); diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index 1c5bd6c8..0ffadb79 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -32,7 +32,12 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\DateTimeException; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; -use daita\MySmallPhpTools\Model\Request; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; @@ -46,11 +51,6 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\LinkedDataSignatureMissingException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SignatureException; use OCA\Social\Exceptions\SignatureIsGoneException; use OCA\Social\Exceptions\SocialAppConfigException; @@ -142,13 +142,13 @@ class SignatureService { /** - * @param Request $request + * @param NC20Request $request * @param RequestQueue $queue * * @throws ActorDoesNotExistException - * @throws SocialAppConfigException + * @throws SocialAppConfigException // TODO: implement in TNC20Request ? */ - public function signRequest(Request $request, RequestQueue $queue) { + public function signRequest(NC20Request $request, RequestQueue $queue) { $date = gmdate(self::DATE_HEADER); $path = $queue->getInstance(); @@ -169,22 +169,22 @@ class SignatureService { $signed = base64_encode($signed); $signature = $this->generateSignature($headersElements, $localActor->getId(), $signed); - $request->addHeader('Signature: ' . $signature); + $request->addHeader('Signature', $signature); } /** * @param array $elements * @param array $data - * @param Request $request + * @param NC20Request $request * * @return string */ - private function generateHeaders(array $elements, array $data, Request $request): string { + private function generateHeaders(array $elements, array $data, NC20Request $request): string { $signingElements = []; foreach ($elements as $element) { $signingElements[] = $element . ': ' . $data[$element]; - $request->addHeader($element . ': ' . $data[$element]); + $request->addHeader($element, $data[$element]); } return implode("\n", $signingElements); @@ -196,7 +196,7 @@ class SignatureService { * @param string $actorId * @param string $signed * - * @return array + * @return string */ private function generateSignature(array $elements, string $actorId, string $signed): string { $signatureElements[] = 'keyId="' . $actorId . '#main-key"'; diff --git a/lib/Service/TestService.php b/lib/Service/TestService.php index e96daf39..3fce8a3a 100644 --- a/lib/Service/TestService.php +++ b/lib/Service/TestService.php @@ -31,7 +31,7 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; -use daita\MySmallPhpTools\Model\Request; +use daita\MySmallPhpTools\Model\Nextcloud\nc20\NC20Request; use daita\MySmallPhpTools\Model\SimpleDataStore; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; @@ -108,9 +108,9 @@ class TestService { $tests->aObj('tests', $testHostMeta); - $request = new Request($path); - $request->addData('resource', 'acct:' . $account); - $request->setAddress($host); + $request = new NC20Request($path); + $request->addParam('resource', 'acct:' . $account); + $request->setHost($host); $request->setProtocols($protocols); $testWebfinger = new Test('webfinger', Test::SEVERITY_MANDATORY); |