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();
}
$item->setUrlCloud($this->configService->getCloudAddress());
$item->setUrlCloud($this->configService->getCloudUrl());
return $item;
}

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

@ -804,7 +804,9 @@ class LocalController extends Controller {
$this->cacheActorService->setViewer($this->viewer);
} catch (Exception $e) {
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 {
$data['serverData']['cloudAddress'] = $this->configService->getCloudAddress();
$data['serverData']['cloudAddress'] = $this->configService->getCloudUrl();
} catch (SocialAppConfigException $e) {
$this->checkService->checkInstallationStatus();
$cloudAddress = $this->setupCloudAddress();
@ -157,7 +157,7 @@ class NavigationController extends Controller {
if ($data['serverData']['isAdmin']) {
$cloudAddress = $this->request->getParam('cloudAddress');
if ($cloudAddress !== null) {
$this->configService->setCloudAddress($cloudAddress);
$this->configService->setCloudUrl($cloudAddress);
} else {
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']) {
$checks = $this->checkService->checkDefault();
$data['serverData']['checks'] = $checks;
@ -188,16 +194,16 @@ class NavigationController extends Controller {
}
private function setupCloudAddress(): string {
// $frontControllerActive =
// ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true
// || getenv('front_controller_active') === 'true');
$frontControllerActive =
($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true
|| getenv('front_controller_active') === 'true');
$cloudAddress = rtrim($this->config->getSystemValue('overwrite.cli.url', ''), '/');
if ($cloudAddress !== '') {
// if (!$frontControllerActive) {
// $cloudAddress .= '/index.php';
// }
$this->configService->setCloudAddress($cloudAddress);
if (!$frontControllerActive) {
$cloudAddress .= '/index.php';
}
$this->configService->setCloudUrl($cloudAddress);
return $cloudAddress;
}
@ -229,7 +235,7 @@ class NavigationController extends Controller {
$setup = false;
try {
$this->configService->getCloudAddress(true);
$this->configService->getCloudUrl();
$setup = true;
} catch (SocialAppConfigException $e) {
}

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

@ -154,32 +154,57 @@ class SignatureService {
$localActor = $this->actorsRequest->getFromId($queue->getAuthor());
$localActorLink =
$this->configService->getUrlSocial() . '@' . $localActor->getPreferredUsername();
$headersElements = ['content-length', 'date', 'host', 'digest'];
$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());
$contentSize = strlen($request->getDataBody());
$signing = $this->generateHeaders($headersElements, $allElements, $request);
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);
$signature = $this->generateSignature($headersElements, $localActor->getId(), $signed);
$header = 'keyId="' . $localActorLink . '#main-key'
. '",algorithm="rsa-sha256",headers="content-length date digest host",signature="'
. $signed . '"';
$request->addHeader('Signature: ' . $signature);
}
$request->addHeader('Content-length: ' . $contentSize);
$request->addHeader('Host: ' . $path->getAddress());
$request->addHeader('Date: ' . $date);
$request->addHeader('Digest: ' . $digest);
$request->addHeader('Signature: ' . $header);
/**
* @param array $elements
* @param array $data
* @param Request $request
*
* @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 === ''
|| openssl_verify($estimated, $signed, $publicKey, $algorithm) !== 1) {
throw new SignatureException(
'signature cannot be checked key: ' . $publicKey . ' - algo: ' . $algorithm
. ' - estimated: ' . $estimated
'signature cannot be checked - signed: ' . $signed . ' - key: ' . $publicKey
. ' - algo: ' . $algorithm . ' - estimated: ' . $estimated
);
}
@ -396,7 +421,7 @@ class SignatureService {
$value = $request->getHeader($key);
if ($key === 'host') {
$value = $this->configService->getCloudAddress(true);
$value = $this->configService->getCloudHost();
}
$estimated .= $key . ': ' . $value . "\n";
@ -474,7 +499,6 @@ class SignatureService {
);
}
/**
* @param array $sign
*

Wyświetl plik

@ -52,7 +52,7 @@ try {
header('Content-type: application/xdr+xml');
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 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">' . "\n";
echo ' <Link rel="lrdd" type="application/xrd+xml" template="' . $url . '"/>' . "\n";

Wyświetl plik

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