Hook::callAll('zrl_init', $arr);
// Try to find the public contact entry of the visitor.
- $cid = Contact::getIdForURL($my_url);
- if (!$cid) {
- Logger::info('No contact record found for ' . $my_url);
+ $contact = Contact::getByURL($my_url, null, ['id', 'url', 'gsid']);
+ if (empty($contact)) {
+ Logger::info('No contact record found', ['url' => $my_url]);
return;
}
- $contact = DBA::selectFirst('contact',['id', 'url'], ['id' => $cid]);
+ if (DI::userSession()->getRemoteUserId() && DI::userSession()->getRemoteUserId() == $contact['id']) {
+ Logger::info('The visitor is already authenticated', ['url' => $my_url]);
+ return;
+ }
- if (DBA::isResult($contact) && DI::userSession()->getRemoteUserId() && DI::userSession()->getRemoteUserId() == $contact['id']) {
- Logger::info('The visitor ' . $my_url . ' is already authenticated');
+ $gserver = DBA::selectFirst('gserver', ['url', 'authredirect'], ['id' => $contact['gsid']]);
+ if (empty($gserver) || empty($gserver['authredirect'])) {
+ Logger::info('No server record found or magic path not defined for server', ['id' => $contact['gsid'], 'gserver' => $gserver]);
return;
}
DI::cache()->set($cachekey, true, Duration::MINUTE);
}
- Logger::info('Not authenticated. Invoking reverse magic-auth for ' . $my_url);
+ Logger::info('Not authenticated. Invoking reverse magic-auth', ['url' => $my_url]);
// Remove the "addr" parameter from the destination. It is later added as separate parameter again.
$addr_request = 'addr=' . urlencode($addr);
// The other instance needs to know where to redirect.
$dest = urlencode(DI::baseUrl() . '/' . $query);
- // We need to extract the basebath from the profile url
- // to redirect the visitors '/magic' module.
- $basepath = Contact::getBasepath($contact['url']);
+ if ($gserver['url'] != DI::baseUrl() && !strstr($dest, '/magic')) {
+ $magic_path = $gserver['authredirect'] . '?f=&rev=1&owa=1&dest=' . $dest . '&' . $addr_request;
- if ($basepath != DI::baseUrl() && !strstr($dest, '/magic')) {
- $magic_path = $basepath . '/magic' . '?owa=1&dest=' . $dest . '&' . $addr_request;
-
- // We have to check if the remote server does understand /magic without invoking something
- $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML, HttpClientOptions::REQUEST => HttpClientRequest::MAGICAUTH]);
- if ($serverret->isSuccess()) {
- Logger::info('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path);
- System::externalRedirect($magic_path);
- }
+ Logger::info('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path);
+ System::externalRedirect($magic_path);
}
}
use Friendica\Core\L10n;
use Friendica\App;
+use Friendica\Core\Protocol;
use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Core\Worker;
use Friendica\Database\Database;
use Friendica\Model\Contact;
use Friendica\Module\Response;
-use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
-use Friendica\Network\HTTPClient\Client\HttpClientAccept;
-use Friendica\Network\HTTPClient\Client\HttpClientOptions;
-use Friendica\Network\HTTPClient\Client\HttpClientRequest;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
+use Friendica\Worker\UpdateContact;
use Psr\Log\LoggerInterface;
class Redir extends \Friendica\BaseModule
private $database;
/** @var App */
private $app;
- /** @var ICanSendHttpRequests */
- private $httpClient;
- public function __construct(ICanSendHttpRequests $httpClient, App $app, Database $database, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
+ public function __construct(App $app, Database $database, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->session = $session;
$this->database = $database;
$this->app = $app;
- $this->httpClient = $httpClient;
}
protected function rawContent(array $request = [])
$this->logger->info('Got my url', ['visitor' => $visitor]);
}
- $contact = $this->database->selectFirst('contact', ['url'], ['id' => $cid]);
+ $contact = Contact::selectFirst(['id', 'url', 'gsid', 'alias', 'network'], ['id' => $cid]);
if (!$contact) {
$this->logger->info('Contact not found', ['id' => $cid]);
throw new HTTPException\NotFoundException($this->t('Contact not found.'));
- } else {
- $contact_url = $contact['url'];
- $this->checkUrl($contact_url, $url);
- $target_url = $url ?: $contact_url;
+ } elseif (empty($contact['gsid'])) {
+ $this->logger->info('Contact has got no server', ['id' => $cid]);
+ return;
}
- $basepath = Contact::getBasepath($contact_url);
+ $contact_url = Contact::getProfileLink($contact);
+ $this->checkUrl($contact_url, $url);
+ $target_url = $url ?: $contact_url;
+
+ $gserver = $this->database->selectFirst('gserver', ['url', 'network', 'openwebauth'], ['id' => $contact['gsid']]);
+ $basepath = $gserver['url'];
+
+ // This part can be removed, when all server entries had been updated. So removing it in 2025 should be safe.
+ if (empty($gserver['openwebauth']) && ($gserver['network'] == Protocol::DFRN)) {
+ $this->logger->notice('Magic path not provided. Contact update initiated.', ['gsid' => $contact['gsid']]);
+ // Update contact to assign the path to the server
+ UpdateContact::add(Worker::PRIORITY_MEDIUM, $contact['id']);
+ } elseif (empty($gserver['openwebauth'])) {
+ $this->logger->debug('Server does not support open web auth.', ['server' => $gserver]);
+ return;
+ }
// We don't use magic auth when there is no visitor, we are on the same system, or we visit our own stuff
if (empty($visitor) || Strings::compareLink($basepath, $this->baseUrl) || Strings::compareLink($contact_url, $visitor)) {
$this->app->redirect($target_url);
}
- // Test for magic auth on the target system
- $response = $this->httpClient->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML, HttpClientOptions::REQUEST => HttpClientRequest::MAGICAUTH]);
- if ($response->isSuccess()) {
- $separator = strpos($target_url, '?') ? '&' : '?';
- $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
+ $separator = strpos($target_url, '?') ? '&' : '?';
+ $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
- $this->logger->info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
- $this->app->redirect($target_url);
- } else {
- $this->logger->info('No magic for contact', ['contact' => $contact_url]);
- }
+ $this->logger->info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
+ $this->app->redirect($target_url);
}
/**
throw new HTTPException\BadRequestException($this->t('Bad Request.'));
}
- $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name'];
- $contact = $this->database->selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, $this->session->getLocalUserId()]]);
+ $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'alias', 'network'];
+ $contact = Contact::selectFirst($fields, ['id' => $cid, 'uid' => [0, $this->session->getLocalUserId()]]);
if (!$contact) {
throw new HTTPException\NotFoundException($this->t('Contact not found.'));
}
- $contact_url = $contact['url'];
+ $contact_url = Contact::getProfileLink($contact);
+ $this->checkUrl($contact_url, $url);
+ $target_url = $url ?: $contact_url;
if (!empty($this->app->getContactId()) && $this->app->getContactId() == $cid) {
// Local user is already authenticated.
- $this->checkUrl($contact_url, $url);
- $this->app->redirect($url ?: $contact_url);
+ $this->app->redirect($target_url);
}
if ($contact['uid'] == 0 && $this->session->getLocalUserId()) {
// Let's have a look if there is an established connection
// between the public contact we have found and the local user.
- $contact = $this->database->selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => $this->session->getLocalUserId()]);
+ $contact = Contact::selectFirst($fields, ['nurl' => $contact['nurl'], 'uid' => $this->session->getLocalUserId()]);
if ($contact) {
$cid = $contact['id'];
}
if (!empty($this->app->getContactId()) && $this->app->getContactId() == $cid) {
// Local user is already authenticated.
- $this->checkUrl($contact_url, $url);
- $target_url = $url ?: $contact_url;
- $this->logger->info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
+ $this->logger->info('Contact already authenticated. Redirecting to target url', ['url' => $contact['url'], 'name' => $contact['name'], 'target_url' => $target_url]);
$this->app->redirect($target_url);
}
}
// contact.
if (($host == $remotehost) && ($this->session->getRemoteContactID($this->session->get('visitor_visiting')) == $this->session->get('visitor_id'))) {
// Remote user is already authenticated.
- $this->checkUrl($contact_url, $url);
- $target_url = $url ?: $contact_url;
- $this->logger->info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
+ $this->logger->info('Contact already authenticated. Redirecting to target url', ['url' => $contact['url'], 'name' => $contact['name'], 'target_url' => $target_url]);
$this->app->redirect($target_url);
}
}
- if (empty($url)) {
- throw new HTTPException\BadRequestException($this->t('Bad Request.'));
- }
-
// If we don't have a connected contact, redirect with
// the 'zrl' parameter.
$my_profile = $this->session->getMyUrl();
- if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
- $separator = strpos($url, '?') ? '&' : '?';
+ if (!empty($my_profile) && !Strings::compareLink($my_profile, $target_url)) {
+ $separator = strpos($target_url, '?') ? '&' : '?';
- $url .= $separator . 'zrl=' . urlencode($my_profile);
+ $target_url .= $separator . 'zrl=' . urlencode($my_profile);
}
- $this->logger->info('redirecting to ' . $url);
- $this->app->redirect($url);
+ $this->logger->info('redirecting to ' . $target_url);
+ $this->app->redirect($target_url);
}
use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\HTTPSignature;
+use Friendica\Util\Network;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
use Friendica\Worker\UpdateContact;
$this->logger->debug('Invoked', ['request' => $request]);
- $addr = $request['addr'] ?? '';
- $dest = $request['dest'] ?? '';
- $bdest = $request['bdest'] ?? '';
- $owa = intval($request['owa'] ?? 0);
+ $addr = $request['addr'] ?? '';
+ $bdest = $request['bdest'] ?? '';
+ $dest = $request['dest'] ?? '';
+ $rev = intval($request['rev'] ?? 0);
+ $owa = intval($request['owa'] ?? 0);
+ $delegate = $request['delegate'] ?? '';
// bdest is preferred as it is hex-encoded and can survive url rewrite and argument parsing
if (!empty($bdest)) {
$this->app->redirect($dest);
}
+ $dest = Network::removeUrlParameter($dest, 'zid');
+ $dest = Network::removeUrlParameter($dest, 'f');
+
// OpenWebAuth
$owner = User::getOwnerDataById($this->userSession->getLocalUserId());
$header = [
'Accept' => 'application/x-zot+json',
- 'X-Open-Web-Auth' => Strings::getRandomHex()
+ 'Content-Type' => 'application/x-zot+json',
+ 'X-Open-Web-Auth' => Strings::getRandomHex(),
+ 'Host' => strtolower(parse_url($gserver['url'], PHP_URL_HOST)),
];
// Create a header that is signed with the local users private key.