Refactor Magic module

This commit is contained in:
Art4 2024-11-18 08:28:32 +00:00
parent 78444ff25c
commit 9422b016d7

View file

@ -63,31 +63,27 @@ class Magic extends BaseModule
$this->logger->debug('Invoked', ['request' => $request]); $this->logger->debug('Invoked', ['request' => $request]);
$addr = $request['addr'] ?? ''; $addr = (string) $request['addr'] ?? '';
$bdest = $request['bdest'] ?? ''; $bdest = (string) $request['bdest'] ?? '';
$dest = $request['dest'] ?? ''; $dest = (string) $request['dest'] ?? '';
$rev = intval($request['rev'] ?? 0);
$owa = intval($request['owa'] ?? 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 // bdest is preferred as it is hex-encoded and can survive url rewrite and argument parsing
if (!empty($bdest)) { if ($bdest !== '') {
$dest = hex2bin($bdest); $dest = hex2bin($bdest);
$this->logger->debug('bdest detected', ['dest' => $dest]); $this->logger->debug('bdest detected', ['dest' => $dest]);
} }
$target = $dest ?: $addr; $target = $dest ?: $addr;
if ($addr ?: $dest) { $contact = Contact::getByURL($addr ?: $dest);
$contact = Contact::getByURL($addr ?: $dest); if ($contact === [] && $owa === 0) {
# code...
$this->logger->info('No contact record found, no oWA, redirecting to destination.', ['request' => $request, 'server' => $_SERVER, 'dest' => $dest]);
$this->app->redirect($dest);
} }
if (empty($contact)) { if ($contact !== []) {
if (!$owa) {
$this->logger->info('No contact record found, no oWA, redirecting to destination.', ['request' => $request, 'server' => $_SERVER, 'dest' => $dest]);
$this->app->redirect($dest);
}
} else {
// Redirect if the contact is already authenticated on this site. // Redirect if the contact is already authenticated on this site.
if ($this->app->getContactId() && strpos($contact['nurl'], Strings::normaliseLink($this->baseUrl)) !== false) { if ($this->app->getContactId() && strpos($contact['nurl'], Strings::normaliseLink($this->baseUrl)) !== false) {
$this->logger->info('Contact is already authenticated, redirecting to destination.', ['dest' => $dest]); $this->logger->info('Contact is already authenticated, redirecting to destination.', ['dest' => $dest]);
@ -97,14 +93,14 @@ class Magic extends BaseModule
$this->logger->debug('Contact found', ['url' => $contact['url']]); $this->logger->debug('Contact found', ['url' => $contact['url']]);
} }
if (!$this->userSession->getLocalUserId() || !$owa) { if (!$this->userSession->getLocalUserId() || $owa === 0) {
$this->logger->notice('Not logged in or not OWA, redirecting to destination.', ['uid' => $this->userSession->getLocalUserId(), 'owa' => $owa, 'dest' => $dest]); $this->logger->notice('Not logged in or not OWA, redirecting to destination.', ['uid' => $this->userSession->getLocalUserId(), 'owa' => $owa, 'dest' => $dest]);
$this->app->redirect($dest); $this->app->redirect($dest);
} }
$dest = Network::removeUrlParameter($dest, 'zid'); $dest = Network::removeUrlParameter($dest, 'zid');
$dest = Network::removeUrlParameter($dest, 'f'); $dest = Network::removeUrlParameter($dest, 'f');
// OpenWebAuth // OpenWebAuth
$owner = User::getOwnerDataById($this->userSession->getLocalUserId()); $owner = User::getOwnerDataById($this->userSession->getLocalUserId());