Give JWT on register

This commit is contained in:
Ruben 2016-09-01 14:08:43 +01:00
parent 2516cfce14
commit 81bc082145
3 changed files with 18 additions and 4 deletions

View File

@ -7,7 +7,9 @@ $config = [
'dsn' => null, 'dsn' => null,
'username' => null, 'username' => null,
'password' => null 'password' => null
] ],
'secret_key' => 'AqeZFu4MWMZ=P2H_SMgS%%7y9aas52%$^eQSQ%HQbYqU(fDo',
'jwt_lifetime' => 86400 * 365 * 5, // five years should be enough for now...
]; ];
return $config; return $config;

View File

@ -8,6 +8,8 @@ use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo; use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Silex\Component\Security\Core\Encoder\JWTEncoder;
use EmotionHero\Application;
/** /**
* Users * Users
@ -139,4 +141,14 @@ class User implements UserInterface
{ {
return $this->id; return $this->id;
} }
/**
* @JMS\VirtualProperty
* @return String
*/
public function getJwt() {
$c =Application::getInstance()->getConfig();
$encoder = new JWTEncoder($c['secret_key'], $c['jwt_lifetime'], null);
return $encoder->encode(['id' => $this->getUsername()]);
}
} }

View File

@ -18,11 +18,11 @@ $app = new Silex\Application([
define('USER_ID_FIELD', 'id'); define('USER_ID_FIELD', 'id');
$get_secret_key = function($payload){return 'AqeZFu4MWMZ=P2H_SMgS%%7'.$payload['uid'].'y9aas52%$^eQSQ%HQbYqU(fDo';};
$app['security.jwt'] = [ $app['security.jwt'] = [
'secret_key' => 'Very_secret_key', 'secret_key' => $eh->getConfig()['secret_key'],
'life_time' => 86400 * 365, 'life_time' => $eh->getConfig()['jwt_lifetime'],
'options' => [ 'options' => [
'username_claim' => USER_ID_FIELD, // default name, option specifying claim containing username 'username_claim' => USER_ID_FIELD, // default name, option specifying claim containing username
'header_name' => 'X-Access-Token', // default null, option for usage normal oauth2 header 'header_name' => 'X-Access-Token', // default null, option for usage normal oauth2 header