joomla-oauth-server/plg_system_oauthserver/src/Extension/Plugin.php

86 lines
2.0 KiB
PHP
Raw Normal View History

2024-03-02 04:44:33 +03:00
<?php
2024-03-11 00:16:56 +03:00
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
2024-03-02 04:44:33 +03:00
namespace Webmasterskaya\Plugin\System\OauthServer\Extension;
2024-03-14 22:00:33 +03:00
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Factory;
2024-03-02 04:44:33 +03:00
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\SiteRouter;
2024-03-02 04:44:33 +03:00
use Joomla\CMS\Uri\Uri;
use Joomla\Event\SubscriberInterface;
2024-03-11 00:16:56 +03:00
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
2024-03-02 04:44:33 +03:00
class Plugin extends CMSPlugin implements SubscriberInterface
{
2024-03-12 11:57:28 +03:00
public static function getSubscribedEvents(): array
{
2024-03-14 22:00:33 +03:00
return [
'onAfterInitialise' => 'attachOauthRouter',
];
2024-03-12 11:57:28 +03:00
}
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
/**
* @return void
* @since version
*/
public function attachOauthRouter(): void
{
2024-03-14 22:00:33 +03:00
/** @var SiteApplication $app */
2024-03-12 11:57:28 +03:00
$app = $this->getApplication();
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
if (!$app->isClient('site'))
{
return;
}
2024-03-11 00:16:56 +03:00
2024-03-14 22:00:33 +03:00
/** @var SiteRouter $siteRouter */
2024-03-12 11:57:28 +03:00
$siteRouter = Factory::getContainer()->get(SiteRouter::class);
$siteRouter->attachParseRule([$this, 'parseOauthRoute'], $siteRouter::PROCESS_BEFORE);
}
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
/**
2024-03-14 22:00:33 +03:00
* @param SiteRouter $router
* @param Uri $uri
2024-03-12 11:57:28 +03:00
*
* @return void
* @since version
*/
public function parseOauthRoute(SiteRouter &$router, Uri &$uri): void
{
$route = trim($uri->getPath(), '/');
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
if (empty($route))
{
return;
}
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
if (!str_starts_with($route, 'login/oauth'))
{
return;
}
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
$segments = explode('/', $route);
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
$uri->setVar('option', 'com_oauthserver');
$uri->setVar('task', 'login.' . $segments[2]);
if ($segments[2] !== 'authorize')
{
$uri->setVar('format', 'json');
}
$uri->setVar('view', 'default');
2024-03-11 00:16:56 +03:00
2024-03-12 11:57:28 +03:00
$uri->setPath('');
}
}