2018-09-28 11:41:24 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Service;
|
|
|
|
|
|
|
|
|
2018-10-12 06:44:35 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2018-09-28 11:41:24 +00:00
|
|
|
use Exception;
|
|
|
|
use OC\User\NoUserException;
|
|
|
|
use OCA\Social\Db\ActorsRequest;
|
|
|
|
use OCA\Social\Db\CacheActorsRequest;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Exceptions\AccountAlreadyExistsException;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
|
|
|
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Exceptions\RequestException;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Model\InstancePath;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
class ActorService {
|
|
|
|
|
|
|
|
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
/** @var InstanceService */
|
|
|
|
private $instanceService;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
/** @var ConfigService */
|
|
|
|
private $configService;
|
|
|
|
|
|
|
|
/** @var ActorsRequest */
|
|
|
|
private $actorsRequest;
|
|
|
|
|
|
|
|
/** @var CacheActorsRequest */
|
|
|
|
private $cacheActorsRequest;
|
|
|
|
|
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* ActorService constructor.
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
|
|
|
* @param ActorsRequest $actorsRequest
|
|
|
|
* @param CacheActorsRequest $cacheActorsRequest
|
2018-10-01 12:14:23 +00:00
|
|
|
* @param InstanceService $instanceService
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param ConfigService $configService
|
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ActorsRequest $actorsRequest,
|
2018-10-01 12:14:23 +00:00
|
|
|
CacheActorsRequest $cacheActorsRequest, InstanceService $instanceService,
|
2018-09-28 11:41:24 +00:00
|
|
|
ConfigService $configService, MiscService $miscService
|
|
|
|
) {
|
|
|
|
$this->configService = $configService;
|
2018-10-01 12:14:23 +00:00
|
|
|
$this->instanceService = $instanceService;
|
2018-09-28 11:41:24 +00:00
|
|
|
$this->actorsRequest = $actorsRequest;
|
|
|
|
$this->cacheActorsRequest = $cacheActorsRequest;
|
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Actor
|
|
|
|
* @throws ActorDoesNotExistException
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
public function getActor(string $username): Actor {
|
|
|
|
$actor = $this->actorsRequest->getFromUsername($username);
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $userId
|
|
|
|
*
|
|
|
|
* @return Actor
|
|
|
|
* @throws ActorDoesNotExistException
|
|
|
|
* @throws NoUserException
|
|
|
|
*/
|
|
|
|
public function getActorFromUserId(string $userId): Actor {
|
|
|
|
$this->miscService->confirmUserId($userId);
|
|
|
|
$actor = $this->actorsRequest->getFromUserId($userId);
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uriId
|
|
|
|
*
|
|
|
|
* @return Actor
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-09-28 11:41:24 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getFromUri(string $uriId) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
$cache = $this->cacheActorsRequest->getFromUrl($uriId);
|
|
|
|
|
|
|
|
return $this->generateActor($cache->getActor());
|
|
|
|
} catch (CacheActorDoesNotExistException $e) {
|
2018-10-01 12:14:23 +00:00
|
|
|
$object = $this->instanceService->retrieveObject($uriId);
|
2018-09-28 11:41:24 +00:00
|
|
|
$actor = $this->generateActor($object);
|
|
|
|
$this->cacheActorsRequest->create($actor, $object);
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
|
|
|
* @param int $type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPathFromActor(Actor $actor, int $type) {
|
|
|
|
switch ($type) {
|
|
|
|
case InstancePath::INBOX:
|
|
|
|
return parse_url($actor->getInbox(), PHP_URL_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param array $object
|
|
|
|
*
|
|
|
|
* @return Actor
|
|
|
|
*/
|
|
|
|
public function generateActor(array $object) {
|
|
|
|
$actor = new Actor();
|
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
$actor->setId($this->get('id', $object));
|
|
|
|
$actor->setFollowers($this->get('followers', $object));
|
|
|
|
$actor->setFollowing($this->get('following', $object));
|
|
|
|
$actor->setInbox($this->get('inbox', $object));
|
|
|
|
$actor->setOutbox($this->get('outbox', $object));
|
|
|
|
$actor->setPublicKey($object['publicKey']['publicKeyPem']);
|
|
|
|
$actor->setPreferredUsername($this->get('preferredUsername', $object));
|
|
|
|
$actor->setAccount('@' . $actor->getPreferredUsername() . '@' . $object['_address']);
|
|
|
|
|
|
|
|
// $actor->setSharedInbox($this->get(''))
|
|
|
|
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Method should be called by the frontend and will generate a fresh Social account for
|
|
|
|
* the user, using the userId and the username.
|
|
|
|
*
|
|
|
|
* Pair of keys are created at this point.
|
|
|
|
*
|
|
|
|
* Return exceptions if an account already exist for this user or if the username is already
|
|
|
|
* taken
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param string $userId
|
2018-10-01 12:14:23 +00:00
|
|
|
* @param string $username
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws AccountAlreadyExistsException
|
2018-09-28 11:41:24 +00:00
|
|
|
* @throws NoUserException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function createActor(string $userId, string $username) {
|
|
|
|
|
|
|
|
$this->miscService->confirmUserId($userId);
|
|
|
|
$this->checkActorUsername($username);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->actorsRequest->getFromUsername($username);
|
2018-10-01 12:14:23 +00:00
|
|
|
throw new AccountAlreadyExistsException('actor with that name already exist');
|
2018-09-28 11:41:24 +00:00
|
|
|
} catch (ActorDoesNotExistException $e) {
|
|
|
|
/* we do nohtin */
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->actorsRequest->getFromUserId($userId);
|
2018-10-01 12:14:23 +00:00
|
|
|
throw new AccountAlreadyExistsException('account for this user already exist');
|
2018-09-28 11:41:24 +00:00
|
|
|
} catch (ActorDoesNotExistException $e) {
|
|
|
|
/* we do nohtin */
|
|
|
|
}
|
|
|
|
|
2018-10-02 10:25:36 +00:00
|
|
|
$this->configService->setCoreValue('public_webfinger', 'social/lib/webfinger.php');
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
$actor = new Actor();
|
|
|
|
$actor->setUserId($userId);
|
|
|
|
$actor->setPreferredUsername($username);
|
|
|
|
|
|
|
|
$this->generateKeys($actor);
|
|
|
|
$this->actorsRequest->create($actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $username
|
|
|
|
*/
|
|
|
|
private function checkActorUsername($username) {
|
2018-10-01 12:14:23 +00:00
|
|
|
$accepted = 'qwertyuiopasdfghjklzxcvbnm';
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
|
|
|
*/
|
|
|
|
private function generateKeys(Actor &$actor) {
|
|
|
|
$res = openssl_pkey_new(
|
|
|
|
[
|
|
|
|
"digest_alg" => "rsa",
|
|
|
|
"private_key_bits" => 2048,
|
|
|
|
"private_key_type" => OPENSSL_KEYTYPE_RSA,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
openssl_pkey_export($res, $privateKey);
|
|
|
|
$publicKey = openssl_pkey_get_details($res)['key'];
|
|
|
|
|
|
|
|
$actor->setPublicKey($publicKey);
|
|
|
|
$actor->setPrivateKey($privateKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|