https://github.com/marvin255/jwt-symfony
Symfony bundle for marvin255/jwt lib.
https://github.com/marvin255/jwt-symfony
jwt php symfony symfony-bundle
Last synced: 3 months ago
JSON representation
Symfony bundle for marvin255/jwt lib.
- Host: GitHub
- URL: https://github.com/marvin255/jwt-symfony
- Owner: marvin255
- License: mit
- Created: 2021-05-04T18:35:44.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-05-11T14:08:55.000Z (about 1 year ago)
- Last Synced: 2026-02-27T09:23:58.636Z (4 months ago)
- Topics: jwt, php, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JWT
[](https://github.com/marvin255/jwt-symfony/actions?query=workflow%3A%22jwt_symfony%22)
Symfony bundle for the [marvin255/jwt](https://github.com/marvin255/jwt) library.
## Installation
Install the bundle via Composer
```shell
composer req marvin255/jwt-symfony
```
## Configuration
Set up one or more profiles in your configuration
```yaml
# config/packages/marvin255_jwt_symfony.yaml
marvin255_jwt_symfony:
profiles:
basic:
signer: RS256 # signer algorithm
signer_public: 'file:///path/to/public.key' # path to public key
signer_private: 'file:///path/to/private.key' # path to private key
signer_private_password: 'password' # password for private key
use_signature_constraint: true # allow signature validation
use_not_before_constraint: true # allow not before validation
not_before_leeway: 2 # leeway to check nbf header
use_expiration_constraint: true # allow expiration validation
expiration_leeway: 2 # leeway to check exp header
use_audience_constraint: true # allow audience validation
audience: 'test' # audience to check
```
## Usage
```php
use Marvin255\Jwt\Symfony\Profile\JwtProfileManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use RuntimeException;
final class SiteController extends AbstractController
{
public function __construct(private JwtProfileManager $jwtProfileManager)
{
}
public function read(Request $request): void
{
// Select profile
$jwtProfile = $this->jwtProfileManager->profile('basic');
$tokenHeader = $request->headers->get('Authorization');
// Decode the token from the header string
$token = $jwtProfile->getDecoder()->decodeString($tokenHeader);
// Validate the token
$validationResult = $jwtProfile->getValidator()->validate($token);
if (!$validationResult->isValid()) {
$message = implode('. ', $validationResult->getErrors());
throw new RuntimeException($message);
}
}
public function build(): void
{
// Select profile
$jwtProfile = $this->jwtProfileManager->profile('basic');
// Build a token
$token = $jwtProfile
->getBuilder()
->setJoseParam('test', 'test') // Any custom JOSE parameter
->setIss('test') // Registered claims have their own setters
->setClaim('test', 'test') // Any custom claim
->build();
}
}
```