2017 © Pedro Peláez
 

library silex-jwt-security

JSON web token security provider for Silex 2.

image

hallboav/silex-jwt-security

JSON web token security provider for Silex 2.

  • Tuesday, July 25, 2017
  • by hallboav
  • Repository
  • 2 Watchers
  • 3 Stars
  • 773 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 35 % Grown

The README.md

Silex JWT Security Service Provider

Install

composer require hallboav/silex-jwt-security

Usage

Set up JSON web token application (which extends Application class from Silex)

This is required if you want to use the JsonWebTokenTrait trait., (*1)

use Hallboav\JsonWebTokenApplication;

$app = new JsonWebTokenApplication();

Register the service provider

use Hallboav\Provider\JsonWebTokenSecurityServiceProvider;

$app->register(new JsonWebTokenSecurityServiceProvider());

Set up your Symfony's firewalls

use Silex\Provider\SecurityServiceProvider;

$providerKey = 'jwt0';
$app['security.user_provider.' . $providerKey] = function ($app) {
    return $app['security.jwt.user_provider'];
};

$app->register(new SecurityServiceProvider(), [
    'security.firewalls' => [
        $providerKey => [
            'pattern' => '^/admin', // any url that matches this pattern
            'stateless' => true,
            'guard' => [
                'authenticators' => [
                    'security.jwt.guard_authenticator'
                ]
            ]
        ]
    ]
]);

Examples of how to generate and retrieve your json web token (thanks to Luís Cobucci)

use Symfony\Component\Security\Core\User\User;

$app->get('/get-token', function () use ($app) {
    $user = new User('hall', 'KIPP', ['ROLE_ADMIN']);

    $token = $app['security.jwt.builder']
        ->setExpiration(strtotime('+15 minutes'))
        ->set('username', $user->getUsername())
        ->set('roles', $user->getRoles())
        ->sign($app['security.jwt.default_signer'], $app['security.jwt.secret'])
        ->getToken();

    return $app->json(['token' => (string) $token]);
});

$app->get('/admin', function () use ($app) {
    $user = $app['user'];

    return $app->json([
        'user' => [
            'username' => $user->getUsername(),
            'roles' => $user->getRoles(),
            'token' => (string) $app->getToken()
        ]
    ]);
});

That's it!

$app->run();

The Versions

25/07 2017

dev-master

9999999-dev

JSON web token security provider for Silex 2.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Hallison Boaventura

25/07 2017

v1.1

1.1.0.0

JSON web token security provider for Silex 2.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Hallison Boaventura

18/07 2017

v1.0

1.0.0.0

JSON web token security provider for Silex 2.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Hallison Boaventura