PHP code example of lotashinski / apikey-security-classes

1. Go to this page and download the library: Download lotashinski/apikey-security-classes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lotashinski / apikey-security-classes example snippets


    


namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;


class UserController extends AbstractController
{

    #[Route('/api/int/users/me', name: 'api_user_info', methods:['GET'])]
    public function index(NormalizerInterface $normalizer): Response
    {
        $user = $this->getUser();
        return $this->json(
            $normalizer->normalize([
                'class' => get_class($user),
                'object' => $user
            ])
        );
    }
}

yaml
### ./config/packages/security.yaml

security:

  # ...
  providers:
    # ...
    api_key_user_provider:
      id: Grsu\ApiKeySecurity\ApiKeyUserProvider

  # ...
  firewalls:
    # ...
    api_key:
      pattern: ^/api/int  
      lazy: true
      provider: api_key_user_provider
      custom_authenticator: Grsu\ApiKeySecurity\ApiKeyAuthentication
  
  # ...
  access_control:
    - { path: ^/api/int, roles: IS_AUTHENTICATED_FULLY }