_JEXEC & copyright

This commit is contained in:
Artem Vasilev 2024-03-09 19:03:09 +03:00
parent f53892aac5
commit 366ddf3534
5 changed files with 136 additions and 55 deletions

View File

@ -1,4 +1,11 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
@ -10,6 +17,8 @@ use Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel;
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\AccessToken;
\defined('_JEXEC') or die;
class AccessTokenRepository implements AccessTokenRepositoryInterface
{
private AccessTokenModel $accessTokenModel;
@ -33,7 +42,8 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
$accessToken->setClient($clientEntity);
$accessToken->setUserIdentifier($userIdentifier);
foreach ($scopes as $scope) {
foreach ($scopes as $scope)
{
$accessToken->addScope($scope);
}
@ -43,16 +53,21 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void
{
$found = false;
try {
try
{
/** @var AccessToken $accessTokenEntity */
$accessToken = $this->accessTokenModel->getItemByIdentifier($accessTokenEntity->getIdentifier());
if ($accessToken->id > 0) {
if ($accessToken->id > 0)
{
$found = true;
}
} catch (\Throwable $e) {
}
catch (\Throwable $e)
{
}
if ($found) {
if ($found)
{
throw UniqueTokenIdentifierConstraintViolationException::create();
}
@ -76,7 +91,8 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface
{
$accessToken = $this->accessTokenModel->getItemByIdentifier($tokenId);
if (!$accessToken->id) {
if (!$accessToken->id)
{
return true;
}

View File

@ -1,15 +1,23 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use Wamania\Snowball\NotFoundException;
use Webmasterskaya\Component\OauthServer\Administrator\Model\AuthCodeModel;
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\AuthCode;
\defined('_JEXEC') or die;
class AuthCodeRepository implements AuthCodeRepositoryInterface
{
private AuthCodeModel $authCodeModel;
@ -19,6 +27,7 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
/**
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\AuthCodeModel $authCodeModel
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel $clientModel
*
* @since version
*/
public function __construct(AuthCodeModel $authCodeModel, ClientModel $clientModel)
@ -35,17 +44,22 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
{
$found = false;
try {
try
{
$authCode = $this->authCodeModel->getItemByIdentifier($authCodeEntity->getIdentifier());
if ($authCode->id > 0) {
if ($authCode->id > 0)
{
$found = true;
}
} catch (\Throwable $e) {
}
catch (\Throwable $e)
{
}
if ($found) {
if ($found)
{
throw UniqueTokenIdentifierConstraintViolationException::create();
}
@ -68,7 +82,8 @@ class AuthCodeRepository implements AuthCodeRepositoryInterface
{
$authCode = $this->authCodeModel->getItemByIdentifier($codeId);
if (empty($authCode->id)) {
if (empty($authCode->id))
{
return true;
}

View File

@ -1,4 +1,11 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
@ -9,6 +16,8 @@ use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\Client;
\defined('_JEXEC') or die;
class ClientRepository implements ClientRepositoryInterface
{
private ClientModel $clientModel;
@ -20,6 +29,7 @@ class ClientRepository implements ClientRepositoryInterface
/**
* @param $clientIdentifier
*
* @return \League\OAuth2\Server\Entities\ClientEntityInterface|null
* @throws \Exception
* @since version
@ -28,7 +38,8 @@ class ClientRepository implements ClientRepositoryInterface
{
$item = $this->clientModel->getItemByIdentifier($clientIdentifier);
if (empty($item->id)) {
if (empty($item->id))
{
return null;
}
@ -39,19 +50,23 @@ class ClientRepository implements ClientRepositoryInterface
{
$item = $this->clientModel->getItemByIdentifier($clientIdentifier);
if (empty($item->id)) {
if (empty($item->id))
{
return false;
}
if (!$item->active) {
if (!$item->active)
{
return false;
}
if (!$this->isGrantSupported($item, $grantType)) {
if (!$this->isGrantSupported($item, $grantType))
{
return false;
}
if (!!$item->public || hash_equals((string)$item->secret, (string)$clientSecret)) {
if (!!$item->public || hash_equals((string) $item->secret, (string) $clientSecret))
{
return true;
}
@ -72,13 +87,15 @@ class ClientRepository implements ClientRepositoryInterface
private function isGrantSupported(\stdClass|CMSObject $client, ?string $grant): bool
{
if (null === $grant) {
if (null === $grant)
{
return true;
}
$grants = array_map('strval', (array) $client->grants);
if (empty($grants)) {
if (empty($grants))
{
return true;
}

View File

@ -1,4 +1,11 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
@ -9,6 +16,8 @@ use Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel;
use Webmasterskaya\Component\OauthServer\Administrator\Model\RefreshTokenModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\RefreshToken;
\defined('_JEXEC') or die;
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{
@ -19,6 +28,7 @@ class RefreshTokenRepository implements RefreshTokenRepositoryInterface
/**
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\RefreshTokenModel $refreshTokenModel
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel $accessTokenModel
*
* @since version
*/
public function __construct(RefreshTokenModel $refreshTokenModel, AccessTokenModel $accessTokenModel)
@ -36,16 +46,21 @@ class RefreshTokenRepository implements RefreshTokenRepositoryInterface
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
{
$found = false;
try {
try
{
$refreshToken = $this->refreshTokenModel->getItemByIdentifier($refreshTokenEntity->getIdentifier());
if ($refreshToken->id > 0) {
if ($refreshToken->id > 0)
{
$found = true;
}
} catch (\Exception $e) {
}
catch (\Exception $e)
{
}
if ($found) {
if ($found)
{
throw UniqueTokenIdentifierConstraintViolationException::create();
}
@ -68,7 +83,8 @@ class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{
$refreshToken = $this->refreshTokenModel->getItemByIdentifier($tokenId);
if (empty($refreshToken->id)) {
if (empty($refreshToken->id))
{
return true;
}

View File

@ -1,4 +1,11 @@
<?php
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
@ -12,6 +19,8 @@ use Webmasterskaya\Component\OauthServer\Administrator\Event\Scope\ScopeResolveE
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\Scope;
\defined('_JEXEC') or die;
class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterface
{
use DispatcherAwareTrait;
@ -27,7 +36,8 @@ class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterf
{
$defined = ['userinfo', 'email'];
if (!in_array($identifier, $defined)) {
if (!in_array($identifier, $defined))
{
return null;
}
@ -42,6 +52,7 @@ class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterf
* @param $grantType
* @param \League\OAuth2\Server\Entities\ClientEntityInterface $clientEntity
* @param null $userIdentifier
*
* @return mixed
* @throws \League\OAuth2\Server\Exception\OAuthServerException
* @since version
@ -69,6 +80,7 @@ class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterf
/**
* @param object $client
* @param array $requestedScopes
*
* @return array
* @throws \League\OAuth2\Server\Exception\OAuthServerException
* @since version
@ -77,26 +89,31 @@ class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterf
{
$clientScopes = $client->scopes;
if (empty($clientScopes)) {
if (empty($clientScopes))
{
return $requestedScopes;
}
$clientScopes = array_map(function ($item) {
$scope = new Scope();
$scope->setIdentifier((string) $item);
return $scope;
}, $clientScopes);
if (empty($requestedScopes)) {
if (empty($requestedScopes))
{
return $clientScopes;
}
$finalizedScopes = [];
$clientScopesAsStrings = array_map('strval', $clientScopes);
foreach ($requestedScopes as $requestedScope) {
foreach ($requestedScopes as $requestedScope)
{
$requestedScopeAsString = (string) $requestedScope;
if (!\in_array($requestedScopeAsString, $clientScopesAsStrings, true)) {
if (!\in_array($requestedScopeAsString, $clientScopesAsStrings, true))
{
throw OAuthServerException::invalidScope($requestedScopeAsString);
}