ramyhakam / symfony-authentication-bundle
jwt authenticator bundle for symfony 4/5
Installs: 32
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^7.2.5
- firebase/php-jwt: ^5.1
- symfony/config: ^5.0
- symfony/dependency-injection: ^5.0
- symfony/http-kernel: ^5.0
- symfony/security-bundle: ^5.0.0
Requires (Dev)
- symfony/phpunit-bridge: ^5.0
This package is auto-updated.
Last update: 2025-01-10 23:45:43 UTC
README
Symfony Authentication bundle provides JWT API token authentication for your symfony project with these features:
- Generate and validate RSA encrypted JWT token
- Authenticate Login User with the token validation using symfony security
- Refresh tokens before expiration (later)
Installation
This bundle requires symfony v4+ to run.
Install using Composer
$ composer require ramyhakam/symfony-authentication-bundle
Using the Bundle
-
First, make sure you've followed the main Security Guide to create your User class. Then, to keep things simple, add an
apiToken
property directly to your User class -
Now you have a new entity for your Authentication that implements
Symfony\Component\Security\Core\User\UserInterface;
-
! This is automatically Done By Symfony Flex: Enable the bundle in your
bundle.php
file by adding this linereturn [ //... Hakam\AuthenticationBundle\HakamAuthenticationBundle::class => ['all' => true], ];
-
Then You need to use the Authenticator as your auth in your firewall by adding these lines in your
packages/security.yml
#example firewalls login: pattern: /api/login stateless: true scurity: false api: pattern: ^/api stateless: true guard: authenticators: - token-authenticator #using jwt authenticator here
-
Now you can inject
JWTTokenAuthenticatorService
and generate user tokens based on the user data\\ Controller\AccountController.php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Hakam\AuthenticationBundle\Services\JWTTokenAuthenticatorService; public class AccountController extends AbstractController /** * @var JWTTokenAuthenticatorService */ private $authenticatorService; public function __construct(JWTTokenAuthenticatorService $authenticatorService) { $this->authenticatorService = $authenticatorService; } //.. public funcion generateToken() { $authToken = $this->authenticatorService->generateUserToken($user->getApiToken()); }
-
Now use can inject
UserIntetface
in your controller which will be replaced by the login User object after token authentication is verified.\\ Controller\AccountController.php namespace App\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; public class AccountController extends AbstractController /** * @param UserInterface $user * @Security("is_granted('ROLE_User') */ public function needsAuthUser( UserInterface $user) { $user // is the logged in user object with ROLE_USER //.. }
Configuration
- Generate your private and public keys:
$ openssl genrsa -out config/jwt/private.pem 2048 $ openssl rsa -in config/jwt/private.pem -outform PEM -pubout -out config/jwt/public.pem
- After generating your public and private keys you should create the bundle configuration file
lives in
config/packages/hakam_authentication_bundle.yaml
with these configuration:
hakam_authentication: public_path_key: '%kernel.project_dir%/config/jwt/public.pem' private_path_key: '%kernel.project_dir%/config/jwt/private.pem'
Contribution
Want to contribute? Great!
- Fork your copy from the repository
- Add your new Awesome features
- Write MORE Tests
- Create a new Pull request
License
MIT
Free Software, Hell Yeah!