Skip to content

Commit

Permalink
update to work with symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Jafar jabr committed Feb 28, 2022
1 parent aa25a86 commit 255669b
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 427 deletions.
2 changes: 1 addition & 1 deletion Api/ApiResponse/ApiResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ApiResponseFactory
*
* @return JsonResponse
*/
public function createResponse(ApiProblem $apiProblem)
public function createResponse(ApiProblem $apiProblem): JsonResponse
{
$data = $apiProblem->toArray();
if ('about:blank' != $data['type']) {
Expand Down
4 changes: 2 additions & 2 deletions Api/JWSEncoder/JWSEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(JWSProviderInterface $jwsProvider)
/**
* {@inheritdoc}
*/
public function encode(array $payload, string $type = 'Main')
public function encode(array $payload, string $type = 'Main'): string
{
try {
$jws = $this->jwsProvider->create($payload, $type);
Expand All @@ -56,7 +56,7 @@ public function encode(array $payload, string $type = 'Main')
/**
* {@inheritdoc}
*/
public function decode(string $token)
public function decode(string $token): array
{
try {
$jws = $this->jwsProvider->load($token);
Expand Down
4 changes: 2 additions & 2 deletions Api/JWSEncoder/JWSEncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface JWSEncoderInterface
* @throws ApiException If an error occurred while trying to create
* the token (invalid cryptic key, invalid payload...)
*/
public function encode(array $data, string $type);
public function encode(array $data, string $type): string;

/**
* @param string $token
Expand All @@ -38,5 +38,5 @@ public function encode(array $data, string $type);
* @throws ApiException If an error occurred while trying to load the token
* (invalid signature, invalid crept key, expired token...)
*/
public function decode(string $token);
public function decode(string $token): array;
}
17 changes: 9 additions & 8 deletions Api/JWTSigner/JWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Base64\Base64UrlSafeEncoder;
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Base64\EncoderInterface;
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\SignerInterface;
use OpenSSLAsymmetricKey;

/**
* Class JWS.
Expand All @@ -23,34 +24,34 @@
*/
class JWS extends JWT
{
protected $signature;
protected ?string $signature;

protected $isSigned = false;
protected bool $isSigned = false;

protected $originalToken;

protected $encodedSignature;
protected string $encodedSignature;

protected $encryptionEngine = 'OpenSSL';
protected string $encryptionEngine = 'OpenSSL';

/**
* Constructor.
*
* @param array $header An associative array of headers. The value can be any type accepted by json_encode or a JSON serializable object
*/
public function __construct($header = [])
public function __construct(array $header = [])
{
parent::__construct([], $header);
}

/**
* Signs the JWS signininput.
*
* @param resource|string $key
* @param string $key
*
* @return string
* @return null|string
*/
public function sign($key)
public function sign(string|OpenSSLAsymmetricKey $key): ?string
{
$this->signature = $this->getSigner()->sign($this->generateSigninInput(), $key);
$this->isSigned = true;
Expand Down
9 changes: 4 additions & 5 deletions Api/JWTSigner/Signer/OpenSSL/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use InvalidArgumentException;
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWTSigner\Signer\SignerInterface;
use OpenSSLAsymmetricKey;
use RuntimeException;

/**
Expand Down Expand Up @@ -63,19 +64,17 @@ public function verify($key, $signature, $input)
* @param string|resource $key
* @param string $password
*
* @return resource OpenSSL key resource
* @return OpenSSLAsymmetricKey|resource OpenSSL key resource
*/
protected function getKeyResource($key, $password = null)
{
if (is_resource($key)) {
if (is_resource($key) || $key instanceof OpenSSLAsymmetricKey) {
return $key;
}

$resource = openssl_pkey_get_public($key) ?: openssl_pkey_get_private($key, $password);
$resource = $password ? openssl_pkey_get_private($key, $password): openssl_pkey_get_public($key);
if (false === $resource) {
throw new RuntimeException('Could not read key resource: '.openssl_error_string());
}

return $resource;
}

Expand Down
1 change: 0 additions & 1 deletion Api/KeyLoader/AbstractKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected function getKeyPath($type)
throw new RuntimeException(sprintf('%s key "%s" does not exist or is not readable.
Please run jafar:generate-keys again to regenerate the kys!', ucfirst($type), $path));
}

return $path;
}
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
For a diff between two versions https://github.com/jafaronly/guarded-authentication-bundle/compare/v1.0.0...v2.0.0


## [v4.0.0](https://github.com/jafaronly/guarded-authentication-bundle/tree/v4.0.0) (2022-02-28)

## [v2.0.6](https://github.com/jafaronly/guarded-authentication-bundle/tree/v2.0.6) (2018-04-01)

## [v2.0.5](https://github.com/jafaronly/guarded-authentication-bundle/tree/v2.0.5) (2018-01-14)
Expand Down Expand Up @@ -38,3 +40,9 @@ For a diff between two versions https://github.com/jafaronly/guarded-authenticat

## [v1.0.0](https://github.com/jafaronly/guarded-authentication-bundle/tree/v1.0.0) (2017-12-24)

#######################
Jafar jabr
@2021/03/08_14:26:07
ON
* master
composer.json
14 changes: 7 additions & 7 deletions Command/PasswordEncryptorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

/**
* Class PasswordEncryptorCommand.
Expand All @@ -20,10 +20,10 @@ class PasswordEncryptorCommand extends Command
{
protected static $defaultName = 'jafar:encrypt-password';

/** @var UserPasswordEncoderInterface */
private $encoder;
private UserPasswordHasherInterface $encoder;

public function __construct(UserPasswordEncoderInterface $encoder)

public function __construct(UserPasswordHasherInterface $encoder)
{
$this->encoder = $encoder;
parent::__construct();
Expand All @@ -36,8 +36,8 @@ protected function configure()
{
$this
->setName('jafar:encrypt-password')
->setDescription('encrypt a password for first use before to have the regisration')
->setHelp('encrypt a password for first use before to have the regisration');
->setDescription('encrypt a password for first use before to have the registration')
->setHelp('encrypt a password for first use before to have the registration');
}

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
throw new Exception('No User Entity found, searched in \'App\Entity\User\' and \'App\Entity\Users\'');
}
$enc = $this->encoder->encodePassword(
$enc = $this->encoder->hashPassword(
$user,
$plainPassword
);
Expand Down
5 changes: 1 addition & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('jafar_guarded_authentication');
$rootNode = $treeBuilder->getRootNode();
Expand All @@ -32,9 +32,6 @@ public function getConfigTreeBuilder()
->scalarNode('pass_phrase')->end()
->scalarNode('token_ttl')->end()
->scalarNode('login_route')->end()
->scalarNode('home_page_route')->end()
->scalarNode('api_login_route')->end()
->scalarNode('api_home_page_route')->end()
->scalarNode('refresh_ttl')->end()
->end();

Expand Down
6 changes: 0 additions & 6 deletions DependencyInjection/JafarGuardedAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('jafar_guarded_authentication.pass_phrase', $config['pass_phrase'] ?? '');
$container->setParameter('jafar_guarded_authentication.token_ttl', $config['token_ttl'] ?? 3600);
$container->setParameter('jafar_guarded_authentication.login_route', $config['login_route'] ?? '');
$container->setParameter('jafar_guarded_authentication.home_page_route', $config['home_page_route'] ?? '');
$container->setParameter('jafar_guarded_authentication.api_login_route', $config['api_login_route'] ?? '');
$container->setParameter(
'jafar_guarded_authentication.api_home_page_route',
$config['api_home_page_route'] ?? ''
);
$container->setParameter('jafar_guarded_authentication.refresh_ttl', $config['refresh_ttl'] ?? 604800);
}
}
2 changes: 1 addition & 1 deletion Exception/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct($reason, $message, Exception $previous = null)
/**
* @return string
*/
public function getReason()
public function getReason(): string
{
return $this->reason;
}
Expand Down
Loading

0 comments on commit 255669b

Please sign in to comment.