Skip to content
Snippets Groups Projects
Commit b7707442 authored by Ide Braakman's avatar Ide Braakman Committed by Bojan Bogdanovic
Browse files

Issue #3479129: Fix phpstan issues reported in pipeline

parent 9bb8fdaf
No related branches found
No related tags found
1 merge request!144Issue #3479129: Fix phpstan issues reported in pipeline
Pipeline #305931 passed with warnings
......@@ -11,6 +11,8 @@ parameters:
- updated_at
- zoneinfo
services:
_defaults:
autoconfigure: true
logger.channel.simple_oauth:
parent: logger.channel_base
arguments: [ 'simple_oauth' ]
......@@ -65,7 +67,7 @@ services:
arguments: [ '@entity_type.manager', '@simple_oauth.oauth2_scope.provider' ]
simple_oauth.repositories.access_token:
class: Drupal\simple_oauth\Repositories\AccessTokenRepository
arguments: [ '@entity_type.manager', '@serializer' ]
arguments: [ '@entity_type.manager', '@serializer', '@class_resolver' ]
simple_oauth.repositories.refresh_token:
class: Drupal\simple_oauth\Repositories\RefreshTokenRepository
arguments: [ '@entity_type.manager', '@serializer' ]
......
......@@ -3,11 +3,12 @@
namespace Drupal\simple_oauth\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\simple_oauth\Authentication\TokenAuthUser;
use Drupal\simple_oauth\Entities\JwksEntity;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -17,6 +18,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
*/
class Jwks implements ContainerInjectionInterface {
use AutowireTrait;
/**
* The authenticated user.
*
......@@ -31,29 +34,11 @@ class Jwks implements ContainerInjectionInterface {
*/
private $config;
/**
* Jwks constructor.
*
* @param \Drupal\Core\Session\AccountProxyInterface $user
* The user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
private function __construct(AccountProxyInterface $user, ConfigFactoryInterface $config_factory) {
private function __construct(AccountProxyInterface $user, ConfigFactoryInterface $config_factory, protected ClassResolverInterface $classResolver) {
$this->user = $user->getAccount();
$this->config = $config_factory->get('simple_oauth.settings');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('config.factory')
);
}
/**
* The controller.
*
......@@ -67,7 +52,7 @@ class Jwks implements ContainerInjectionInterface {
if ($this->config->get('disable_openid_connect')) {
throw new NotFoundHttpException('Not Found');
}
return new JsonResponse((new JwksEntity())->getKeys());
return new JsonResponse(($this->classResolver->getInstanceFromDefinition(JwksEntity::class))->getKeys());
}
}
......@@ -2,6 +2,10 @@
namespace Drupal\simple_oauth\Entities;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
......@@ -10,13 +14,21 @@ use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
/**
* The entity for the Access token.
*/
class AccessTokenEntity implements AccessTokenEntityInterface {
class AccessTokenEntity implements AccessTokenEntityInterface, ContainerInjectionInterface, LoggerAwareInterface {
use AccessTokenTrait, TokenEntityTrait, EntityTrait;
use AccessTokenTrait, AutowireTrait, LoggerAwareTrait, TokenEntityTrait, EntityTrait;
public function __construct(
protected ModuleHandlerInterface $moduleHandler,
protected TimeInterface $time,
) {
}
/**
* {@inheritdoc}
......@@ -24,8 +36,7 @@ class AccessTokenEntity implements AccessTokenEntityInterface {
// phpcs:ignore
public function convertToJWT() {
$private_claims = [];
\Drupal::moduleHandler()
->alter('simple_oauth_private_claims', $private_claims, $this);
$this->moduleHandler->alter('simple_oauth_private_claims', $private_claims, $this);
if (!is_array($private_claims)) {
$message = 'An implementation of hook_simple_oauth_private_claims_alter ';
$message .= 'returns an invalid $private_claims value. $private_claims ';
......@@ -34,7 +45,7 @@ class AccessTokenEntity implements AccessTokenEntityInterface {
}
$id = $this->getIdentifier();
$now = new \DateTimeImmutable('@' . \Drupal::time()->getCurrentTime());
$now = new \DateTimeImmutable('@' . $this->time->getCurrentTime());
$key = InMemory::plainText($this->privateKey->getKeyContents());
$config = Configuration::forSymmetricSigner(new Sha256(), $key);
$user_id = $this->getUserIdentifier();
......@@ -68,11 +79,10 @@ class AccessTokenEntity implements AccessTokenEntityInterface {
$builder->withClaim($claim_name, $value);
}
catch (\Exception $e) {
\Drupal::logger('simple_oauth')
->error('Could not add private claim @claim_name to token: @error_message', [
'@claim_name' => $claim_name,
'@error_message' => $e->getMessage(),
]);
$this->logger->error('Could not add private claim @claim_name to token: @error_message', [
'@claim_name' => $claim_name,
'@error_message' => $e->getMessage(),
]);
}
}
......
......@@ -2,10 +2,23 @@
namespace Drupal\simple_oauth\Entities;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\File\FileSystemInterface;
/**
* A JSON Web Key Store entity.
*/
class JwksEntity {
class JwksEntity implements ContainerInjectionInterface {
use AutowireTrait;
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected FileSystemInterface $fileSystem,
) {
}
/**
* Returns the keys in JWK (JSON Web Key) format.
......@@ -18,21 +31,19 @@ class JwksEntity {
public function getKeys() {
$json_data = [];
// Get the public key from simple_oauth settings.
$config = \Drupal::config('simple_oauth.settings');
if (!empty($config)) {
$public_key_real = \Drupal::service('file_system')->realpath($config->get('public_key'));
if (!empty($public_key_real)) {
$key_info = openssl_pkey_get_details(openssl_pkey_get_public(file_get_contents($public_key_real)));
$json_data = [
'keys' => [
[
'kty' => 'RSA',
'n' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($key_info['rsa']['n'])), '='),
'e' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($key_info['rsa']['e'])), '='),
],
$config = $this->configFactory->get('simple_oauth.settings');
$public_key_real = $this->fileSystem->realpath($config->get('public_key'));
if (!empty($public_key_real)) {
$key_info = openssl_pkey_get_details(openssl_pkey_get_public(file_get_contents($public_key_real)));
$json_data = [
'keys' => [
[
'kty' => 'RSA',
'n' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($key_info['rsa']['n'])), '='),
'e' => rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($key_info['rsa']['e'])), '='),
],
];
}
],
];
}
return $json_data;
}
......
......@@ -2,14 +2,9 @@
namespace Drupal\simple_oauth\Entity\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for deleting Access Token entities.
......@@ -18,42 +13,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class Oauth2TokenDeleteForm extends ContentEntityConfirmFormBase {
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a ContentEntityForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?TimeInterface $time = NULL, MessengerInterface $messenger) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
......@@ -81,7 +40,7 @@ class Oauth2TokenDeleteForm extends ContentEntityConfirmFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->messenger->addMessage(
$this->messenger()->addMessage(
$this->t('content @type: deleted @label.',
[
'@type' => $this->entity->bundle(),
......
......@@ -10,11 +10,15 @@ use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\UserEntityInterface;
use OpenIDConnectServer\Entities\ClaimSetInterface;
use OpenIDConnectServer\IdTokenResponse;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
/**
* OpenId Connect id token response.
*/
class OpenIdConnectIdTokenResponse extends IdTokenResponse {
class OpenIdConnectIdTokenResponse extends IdTokenResponse implements LoggerAwareInterface {
use LoggerAwareTrait;
/**
* {@inheritdoc}
......@@ -63,11 +67,10 @@ class OpenIdConnectIdTokenResponse extends IdTokenResponse {
$builder->withClaim($claim_name, $value);
}
catch (\Exception $e) {
\Drupal::logger('simple_oauth')
->error('Could not add private claim @claim_name to token: @error_message', [
'@claim_name' => $claim_name,
'@error_message' => $e->getMessage(),
]);
$this->logger->error('Could not add private claim @claim_name to token: @error_message', [
'@claim_name' => $claim_name,
'@error_message' => $e->getMessage(),
]);
}
}
......
......@@ -2,10 +2,13 @@
namespace Drupal\simple_oauth\Repositories;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\simple_oauth\Entities\AccessTokenEntity;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* The access token repository.
......@@ -14,6 +17,13 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface {
use RevocableTokenRepositoryTrait;
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected SerializerInterface $serializer,
protected ClassResolverInterface $classResolver,
) {
}
/**
* The bundle ID.
*
......@@ -26,14 +36,14 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface {
*
* @var string
*/
protected static string $entityClass = 'Drupal\simple_oauth\Entities\AccessTokenEntity';
protected static string $entityClass = AccessTokenEntity::class;
/**
* The OAuth2 entity interface name.
*
* @var string
*/
protected static string $entityInterface = 'League\OAuth2\Server\Entities\AccessTokenEntityInterface';
protected static string $entityInterface = AccessTokenEntityInterface::class;
/**
* {@inheritdoc}
......@@ -60,7 +70,7 @@ class AccessTokenRepository implements AccessTokenRepositoryInterface {
* {@inheritdoc}
*/
public function getNewToken(ClientEntityInterface $client_entity, array $scopes, $user_identifier = NULL) {
$access_token = new AccessTokenEntity();
$access_token = $this->classResolver->getInstanceFromDefinition($this::$entityClass);
$access_token->setClient($client_entity);
foreach ($scopes as $scope) {
$access_token->addScope($scope);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment