diff --git a/config/_default.php b/config/_default.php index 7334df2..f06122e 100644 --- a/config/_default.php +++ b/config/_default.php @@ -7,7 +7,9 @@ $config = [ 'dsn' => null, 'username' => 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; \ No newline at end of file diff --git a/src/Models/User.php b/src/Models/User.php index f95bb29..3316da0 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -8,6 +8,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Gedmo\Mapping\Annotation as Gedmo; use Symfony\Component\Security\Core\User\UserInterface; +use Silex\Component\Security\Core\Encoder\JWTEncoder; +use EmotionHero\Application; /** * Users @@ -139,4 +141,14 @@ class User implements UserInterface { 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()]); + } } diff --git a/www/index.php b/www/index.php index 9e8d3d1..1309ead 100644 --- a/www/index.php +++ b/www/index.php @@ -18,11 +18,11 @@ $app = new Silex\Application([ define('USER_ID_FIELD', 'id'); -$get_secret_key = function($payload){return 'AqeZFu4MWMZ=P2H_SMgS%%7'.$payload['uid'].'y9aas52%$^eQSQ%HQbYqU(fDo';}; + $app['security.jwt'] = [ - 'secret_key' => 'Very_secret_key', - 'life_time' => 86400 * 365, + 'secret_key' => $eh->getConfig()['secret_key'], + 'life_time' => $eh->getConfig()['jwt_lifetime'], 'options' => [ '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