Merge pull request #627 from nextcloud/enhancement/noid/fix-social-url

rewrite url/id system
feature/noid/sql-rewrite-0929
Maxence Lange 2019-07-11 20:35:52 -01:00 zatwierdzone przez GitHub
commit 623facfb04
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
15 zmienionych plików z 168 dodań i 110 usunięć

Wyświetl plik

@ -382,7 +382,7 @@ class AP {
throw new ItemUnknownException(); throw new ItemUnknownException();
} }
$item->setUrlCloud($this->configService->getCloudAddress()); $item->setUrlCloud($this->configService->getCloudUrl());
return $item; return $item;
} }

Wyświetl plik

@ -124,7 +124,7 @@ class Reset extends Base {
$output->writeln(''); $output->writeln('');
$cloudAddress = $this->configService->getCloudAddress(); $cloudAddress = $this->configService->getCloudUrl();
$question = new Question( $question = new Question(
'<info>Now is a good time to change the base address of your cloud: </info> (' '<info>Now is a good time to change the base address of your cloud: </info> ('
. $cloudAddress . ') ', . $cloudAddress . ') ',
@ -137,7 +137,7 @@ class Reset extends Base {
return; return;
} }
$this->configService->setCloudAddress($newCloudAddress); $this->configService->setCloudUrl($newCloudAddress);
$output->writeln(''); $output->writeln('');
$output->writeln('New address: <info>' . $newCloudAddress . '</info>'); $output->writeln('New address: <info>' . $newCloudAddress . '</info>');
} }

Wyświetl plik

@ -47,7 +47,7 @@ class ConfigController extends Controller {
* @return DataResponse * @return DataResponse
*/ */
public function setCloudAddress(string $cloudAddress): DataResponse { public function setCloudAddress(string $cloudAddress): DataResponse {
$this->configService->setCloudAddress($cloudAddress); $this->configService->setCloudUrl($cloudAddress);
return new DataResponse([]); return new DataResponse([]);
} }

Wyświetl plik

@ -804,7 +804,9 @@ class LocalController extends Controller {
$this->cacheActorService->setViewer($this->viewer); $this->cacheActorService->setViewer($this->viewer);
} catch (Exception $e) { } catch (Exception $e) {
if ($exception) { if ($exception) {
throw new AccountDoesNotExistException(); throw new AccountDoesNotExistException(
'unable to initViewer - ' . get_class($e) . ' - ' . $e->getMessage()
);
} }
} }
} }

Wyświetl plik

@ -145,7 +145,7 @@ class NavigationController extends Controller {
]; ];
try { try {
$data['serverData']['cloudAddress'] = $this->configService->getCloudAddress(); $data['serverData']['cloudAddress'] = $this->configService->getCloudUrl();
} catch (SocialAppConfigException $e) { } catch (SocialAppConfigException $e) {
$this->checkService->checkInstallationStatus(); $this->checkService->checkInstallationStatus();
$cloudAddress = $this->setupCloudAddress(); $cloudAddress = $this->setupCloudAddress();
@ -157,7 +157,7 @@ class NavigationController extends Controller {
if ($data['serverData']['isAdmin']) { if ($data['serverData']['isAdmin']) {
$cloudAddress = $this->request->getParam('cloudAddress'); $cloudAddress = $this->request->getParam('cloudAddress');
if ($cloudAddress !== null) { if ($cloudAddress !== null) {
$this->configService->setCloudAddress($cloudAddress); $this->configService->setCloudUrl($cloudAddress);
} else { } else {
return new TemplateResponse(Application::APP_NAME, 'main', $data); return new TemplateResponse(Application::APP_NAME, 'main', $data);
} }
@ -165,6 +165,12 @@ class NavigationController extends Controller {
} }
} }
try {
$this->configService->getSocialUrl();
} catch (SocialAppConfigException $e) {
$this->configService->setSocialUrl('');
}
if ($data['serverData']['isAdmin']) { if ($data['serverData']['isAdmin']) {
$checks = $this->checkService->checkDefault(); $checks = $this->checkService->checkDefault();
$data['serverData']['checks'] = $checks; $data['serverData']['checks'] = $checks;
@ -188,16 +194,16 @@ class NavigationController extends Controller {
} }
private function setupCloudAddress(): string { private function setupCloudAddress(): string {
// $frontControllerActive = $frontControllerActive =
// ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true
// || getenv('front_controller_active') === 'true'); || getenv('front_controller_active') === 'true');
$cloudAddress = rtrim($this->config->getSystemValue('overwrite.cli.url', ''), '/'); $cloudAddress = rtrim($this->config->getSystemValue('overwrite.cli.url', ''), '/');
if ($cloudAddress !== '') { if ($cloudAddress !== '') {
// if (!$frontControllerActive) { if (!$frontControllerActive) {
// $cloudAddress .= '/index.php'; $cloudAddress .= '/index.php';
// } }
$this->configService->setCloudAddress($cloudAddress); $this->configService->setCloudUrl($cloudAddress);
return $cloudAddress; return $cloudAddress;
} }
@ -229,7 +235,7 @@ class NavigationController extends Controller {
$setup = false; $setup = false;
try { try {
$this->configService->getCloudAddress(true); $this->configService->getCloudUrl();
$setup = true; $setup = true;
} catch (SocialAppConfigException $e) { } catch (SocialAppConfigException $e) {
} }

Wyświetl plik

@ -67,7 +67,7 @@ class ActorsRequest extends ActorsRequestBuilder {
*/ */
public function create(Person $actor): string { public function create(Person $actor): string {
$id = $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername(); $id = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername();
$qb = $this->getActorsInsertSql(); $qb = $this->getActorsInsertSql();
$qb->setValue('id', $qb->createNamedParameter($id)) $qb->setValue('id', $qb->createNamedParameter($id))

Wyświetl plik

@ -108,7 +108,7 @@ class ActorsRequestBuilder extends CoreRequestBuilder {
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */
protected function parseActorsSelectSql($data): Person { protected function parseActorsSelectSql($data): Person {
$root = $this->configService->getUrlSocial(); $root = $this->configService->getSocialUrl();
$actor = new Person(); $actor = new Person();
$actor->importFromDatabase($data); $actor->importFromDatabase($data);
@ -122,7 +122,7 @@ class ActorsRequestBuilder extends CoreRequestBuilder {
->setLocal(true) ->setLocal(true)
->setAvatarVersion($this->getInt('avatar_version', $data, -1)) ->setAvatarVersion($this->getInt('avatar_version', $data, -1))
->setAccount( ->setAccount(
$actor->getPreferredUsername() . '@' . $this->configService->getCloudAddress(true) $actor->getPreferredUsername() . '@' . $this->configService->getSocialAddress()
); );
$actor->setUrlSocial($root) $actor->setUrlSocial($root)
->setUrl($actor->getId()); ->setUrl($actor->getId());

Wyświetl plik

@ -50,9 +50,13 @@ class ConfigService {
use TPathTools; use TPathTools;
use TArrayTools; use TArrayTools;
const CLOUD_URL = 'cloud_url';
const CLOUD_ADDRESS = 'address'; const SOCIAL_URL = 'social_url';
const SOCIAL_ADDRESS = 'social_address'; const SOCIAL_ADDRESS = 'social_address';
// deprecated -> CLOUD_URL
const CLOUD_ADDRESS = 'address';
const SOCIAL_SERVICE = 'service'; const SOCIAL_SERVICE = 'service';
const SOCIAL_MAX_SIZE = 'max_size'; const SOCIAL_MAX_SIZE = 'max_size';
const SOCIAL_ACCESS_TYPE = 'access_type'; const SOCIAL_ACCESS_TYPE = 'access_type';
@ -65,8 +69,10 @@ class ConfigService {
/** @var array */ /** @var array */
public $defaults = [ public $defaults = [
self::CLOUD_ADDRESS => '', self::CLOUD_URL => '',
self::SOCIAL_URL => '',
self::SOCIAL_ADDRESS => '', self::SOCIAL_ADDRESS => '',
self::CLOUD_ADDRESS => '',
self::SOCIAL_SERVICE => 1, self::SOCIAL_SERVICE => 1,
self::SOCIAL_MAX_SIZE => 10, self::SOCIAL_MAX_SIZE => 10,
self::SOCIAL_ACCESS_TYPE => 'all_but', self::SOCIAL_ACCESS_TYPE => 'all_but',
@ -257,44 +263,66 @@ class ConfigService {
/** /**
* @param string $cloudAddress * getCloudHost - cloud.example.com
*/
public function setCloudAddress(string $cloudAddress) {
$this->setAppValue(self::CLOUD_ADDRESS, $cloudAddress);
}
/**
* @param bool $host
* *
* @return string * @return string
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */
public function getCloudAddress(bool $host = false) { public function getCloudHost(): string {
$address = $this->getAppValue(self::CLOUD_ADDRESS); $url = $this->getCloudUrl();
return parse_url($url, PHP_URL_HOST);
}
/**
* getCloudUrl - https://cloud.example.com/index.php
* - https://cloud.example.com
*
* @param bool $noPhp
*
* @return string
* @throws SocialAppConfigException
*/
public function getCloudUrl(bool $noPhp = false) {
$address = $this->getAppValue(self::CLOUD_URL);
if ($address === '') { if ($address === '') {
throw new SocialAppConfigException(); throw new SocialAppConfigException();
} }
// fixing address for alpha2 if ($noPhp) {
if (substr($address, -10) === '/index.php') { $pos = strpos($address, '/index.php');
$address = substr($address, 0, -10); if ($pos) {
$this->setCloudAddress($address); $address = substr($address, 0, $pos);
} }
if ($host === true) {
$parsed = parse_url($address);
$result = $this->get('host', $parsed, '');
$port = $this->get('port', $parsed, '');
// if ($port !== '') {
// $result .= ':' . $port;
// }
return $result;
} }
return $this->withoutEndSlash($address, false, false); return $this->withoutEndSlash($address, false, false);
} }
/**
* @param string $cloudAddress
*/
public function setCloudUrl(string $cloudAddress) {
$this->setAppValue(self::CLOUD_URL, $cloudAddress);
}
/**
* getSocialAddress - example.com
*
* @return string
* @throws SocialAppConfigException
*/
public function getSocialAddress(): string {
$address = $this->getAppValue(self::SOCIAL_ADDRESS);
if ($address === '') {
return $this->getCloudHost();
}
return $address;
}
/** /**
* @param string $address * @param string $address
@ -303,34 +331,35 @@ class ConfigService {
$this->setAppValue(self::SOCIAL_ADDRESS, $address); $this->setAppValue(self::SOCIAL_ADDRESS, $address);
} }
/**
* @return string
* @throws SocialAppConfigException
*/
public function getSocialAddress(): string {
$address = $this->getAppValue(self::SOCIAL_ADDRESS);
if ($address === '') {
return $this->getCloudAddress(true);
}
return $address;
}
/** /**
* @param string $path * getSocialUrl - https://cloud.example.com/apps/social/
* *
* @return string * @return string
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */
public function getUrlSocial(string $path = ''): string { public function getSocialUrl(): string {
if ($path === '') { $socialUrl = $this->getAppValue(self::SOCIAL_URL);
$path = $this->urlGenerator->linkToRoute('social.Navigation.navigate'); if ($socialUrl === '') {
throw new SocialAppConfigException();
} }
return $this->getCloudAddress() . $path; return $socialUrl;
// return 'https://' . $this->getCloudAddress(true) . $path; }
/**
* @param string $url
*
* @throws SocialAppConfigException
*/
public function setSocialUrl(string $url = '') {
if ($url === '') {
$url = $this->getCloudUrl() . $this->urlGenerator->linkToRoute(
'social.Navigation.navigate'
);
}
$this->setAppValue(self::SOCIAL_URL, $url);
} }
@ -344,7 +373,7 @@ class ConfigService {
public function generateId(string $path = '', $generateId = true): string { public function generateId(string $path = '', $generateId = true): string {
$path = $this->withoutBeginSlash($this->withEndSlash($path)); $path = $this->withoutBeginSlash($this->withEndSlash($path));
$id = $this->getUrlSocial() . $path; $id = $this->getSocialUrl() . $path;
if ($generateId === true) { if ($generateId === true) {
$id .= time() . crc32(uniqid()); $id .= time() . crc32(uniqid());
} }

Wyświetl plik

@ -329,17 +329,15 @@ class CurlService {
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */
public function asyncWithToken(string $token) { public function asyncWithToken(string $token) {
$address = $this->configService->getUrlSocial(); $address = $this->configService->getSocialUrl();
$parse = parse_url($address);
$host = $this->get('host', $parse, ''); $path = $this->withEndSlash(parse_url($address, PHP_URL_PATH));
$path = $this->withEndSlash($this->get('path', $parse, '')) . $this->withoutBeginSlash( $path .= $this->withoutBeginSlash(self::ASYNC_REQUEST_TOKEN);
self::ASYNC_REQUEST_TOKEN
);
$path = str_replace('{token}', $token, $path); $path = str_replace('{token}', $token, $path);
$request = new Request($path, Request::TYPE_POST); $request = new Request($path, Request::TYPE_POST);
$request->setAddress($host); $request->setAddress($this->configService->getCloudHost());
$request->setProtocol($this->get('scheme', $parse, 'https')); $request->setProtocol(parse_url($address, PHP_URL_SCHEME));
try { try {
$this->request($request); $this->request($request);

Wyświetl plik

@ -131,7 +131,7 @@ class FediverseService {
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */
public function isLocal(string $address): bool { public function isLocal(string $address): bool {
$local = $this->configService->getCloudAddress(true); $local = $this->configService->getCloudHost();
return ($local === $address); return ($local === $address);
} }

Wyświetl plik

@ -254,7 +254,7 @@ class NoteService {
$note->addTag( $note->addTag(
[ [
'type' => 'Hashtag', 'type' => 'Hashtag',
'href' => $this->configService->getCloudAddress() . '/tag/' . strtolower( 'href' => $this->configService->getSocialUrl() . 'tag/' . strtolower(
$hashtag $hashtag
), ),
'name' => '#' . $hashtag 'name' => '#' . $hashtag

Wyświetl plik

@ -109,9 +109,8 @@ class PostService {
$actor = $post->getActor(); $actor = $post->getActor();
$this->noteService->assignItem($note, $actor, $post->getType()); $this->noteService->assignItem($note, $actor, $post->getType());
$note->setAttributedTo( $note->setAttributedTo($actor->getId());
$this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername() // $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername()
);
$note->setContent(htmlentities($post->getContent(), ENT_QUOTES)); $note->setContent(htmlentities($post->getContent(), ENT_QUOTES));

Wyświetl plik

@ -154,32 +154,57 @@ class SignatureService {
$localActor = $this->actorsRequest->getFromId($queue->getAuthor()); $localActor = $this->actorsRequest->getFromId($queue->getAuthor());
$localActorLink = $headersElements = ['content-length', 'date', 'host', 'digest'];
$this->configService->getUrlSocial() . '@' . $localActor->getPreferredUsername(); $allElements = [
'(request-target)' => 'post ' . $path->getPath(),
'date' => $date,
'host' => $path->getAddress(),
'digest' => $this->generateDigest($request->getDataBody()),
'content-length' => strlen($request->getDataBody())
];
$digest = $this->generateDigest($request->getDataBody()); $signing = $this->generateHeaders($headersElements, $allElements, $request);
$contentSize = strlen($request->getDataBody()); openssl_sign($signing, $signed, $localActor->getPrivateKey(), OPENSSL_ALGO_SHA256);
$signature = '';
// $signature .= "(request-target): post " . $path->getPath() . "\n";
$signature .= 'content-length: ' . $contentSize . "\n";
$signature .= 'date: ' . $date . "\n";
$signature .= 'digest: ' . $digest . "\n";
$signature .= 'host: ' . $path->getAddress();
openssl_sign($signature, $signed, $localActor->getPrivateKey(), OPENSSL_ALGO_SHA256);
$signed = base64_encode($signed); $signed = base64_encode($signed);
$signature = $this->generateSignature($headersElements, $localActor->getId(), $signed);
$header = 'keyId="' . $localActorLink . '#main-key' $request->addHeader('Signature: ' . $signature);
. '",algorithm="rsa-sha256",headers="content-length date digest host",signature="' }
. $signed . '"';
$request->addHeader('Content-length: ' . $contentSize); /**
$request->addHeader('Host: ' . $path->getAddress()); * @param array $elements
$request->addHeader('Date: ' . $date); * @param array $data
$request->addHeader('Digest: ' . $digest); * @param Request $request
$request->addHeader('Signature: ' . $header); *
* @return string
*/
private function generateHeaders(array $elements, array $data, Request $request): string {
$signingElements = [];
foreach ($elements as $element) {
$signingElements[] = $element . ': ' . $data[$element];
$request->addHeader($element . ': ' . $data[$element]);
}
return implode("\n", $signingElements);
}
/**
* @param array $elements
* @param string $actorId
* @param string $signed
*
* @return array
*/
private function generateSignature(array $elements, string $actorId, string $signed): string {
$signatureElements[] = 'keyId="' . $actorId . '#main-key"';
$signatureElements[] = 'algorithm="rsa-sha256"';
$signatureElements[] = 'headers="' . implode(' ', $elements) . '"';
$signatureElements[] = 'signature="' . $signed . '"';
return implode(',', $signatureElements);
} }
@ -361,8 +386,8 @@ class SignatureService {
if ($publicKey === '' if ($publicKey === ''
|| openssl_verify($estimated, $signed, $publicKey, $algorithm) !== 1) { || openssl_verify($estimated, $signed, $publicKey, $algorithm) !== 1) {
throw new SignatureException( throw new SignatureException(
'signature cannot be checked key: ' . $publicKey . ' - algo: ' . $algorithm 'signature cannot be checked - signed: ' . $signed . ' - key: ' . $publicKey
. ' - estimated: ' . $estimated . ' - algo: ' . $algorithm . ' - estimated: ' . $estimated
); );
} }
@ -396,7 +421,7 @@ class SignatureService {
$value = $request->getHeader($key); $value = $request->getHeader($key);
if ($key === 'host') { if ($key === 'host') {
$value = $this->configService->getCloudAddress(true); $value = $this->configService->getCloudHost();
} }
$estimated .= $key . ': ' . $value . "\n"; $estimated .= $key . ': ' . $value . "\n";
@ -474,7 +499,6 @@ class SignatureService {
); );
} }
/** /**
* @param array $sign * @param array $sign
* *

Wyświetl plik

@ -52,7 +52,7 @@ try {
header('Content-type: application/xdr+xml'); header('Content-type: application/xdr+xml');
try { try {
$url = $configService->getCloudAddress() . '/.well-known/webfinger?resource={uri}'; $url = $configService->getCloudUrl(true) . '/.well-known/webfinger?resource={uri}';
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">' . "\n"; echo '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">' . "\n";
echo ' <Link rel="lrdd" type="application/xrd+xml" template="' . $url . '"/>' . "\n"; echo ' <Link rel="lrdd" type="application/xrd+xml" template="' . $url . '"/>' . "\n";

Wyświetl plik

@ -64,12 +64,12 @@ try {
$fediverseService->jailed(); $fediverseService->jailed();
if ($configService->getSocialAddress() !== $instance) { if ($configService->getSocialAddress() !== $instance) {
if ($configService->getCloudHost() === $instance) {
if ($configService->getCloudAddress(true) === $instance) {
$instance = $configService->getSocialAddress(); $instance = $configService->getSocialAddress();
} else { } else {
throw new Exception( throw new Exception(
'instance is ' . $instance . ', expected ' . $configService->getSocialAddress() 'instance is ' . $instance . ', expected ' . $configService->getSocialAddress()
. ' or ' . $configService->getCloudHost()
); );
} }
} }
@ -82,9 +82,9 @@ try {
exit; exit;
} }
$href = $configService->getUrlSocial( $href = $configService->getSocialUrl() . '@' . $username;
$urlGenerator->linkToRoute('social.ActivityPub.actorAlias', ['username' => $username]) // $urlGenerator->linkToRoute('social.ActivityPub.actorAlias', ['username' => $username])
); //);
if (substr($href, -1) === '/') { if (substr($href, -1) === '/') {
$href = substr($href, 0, -1); $href = substr($href, 0, -1);