|
|
|
@ -3,17 +3,31 @@ |
|
|
|
|
|
|
|
|
|
namespace Chamilo\ApiBundle\GraphQL; |
|
|
|
|
|
|
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
|
|
|
use Chamilo\UserBundle\Entity\User; |
|
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
use Firebase\JWT\JWT; |
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Class Auth. |
|
|
|
|
* |
|
|
|
|
* Trait ApiGraphQLTrait |
|
|
|
|
* @package Chamilo\ApiBundle\GraphQL |
|
|
|
|
*/ |
|
|
|
|
class Auth |
|
|
|
|
trait ApiGraphQLTrait |
|
|
|
|
{ |
|
|
|
|
use ContainerAwareTrait; |
|
|
|
|
|
|
|
|
|
private $em; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* ApiGraphQLTrait constructor. |
|
|
|
|
* |
|
|
|
|
* @param EntityManager $entityManager |
|
|
|
|
*/ |
|
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
{ |
|
|
|
|
$this->em = $entityManager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param string $username |
|
|
|
|
* @param string $password |
|
|
|
@ -22,16 +36,16 @@ class Auth |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
public function getUserToken($username, $password): string |
|
|
|
|
private function getUserToken($username, $password): string |
|
|
|
|
{ |
|
|
|
|
/** @var User $user */ |
|
|
|
|
$user = Container::getUserManager()->findUserBy(['username' => $username]); |
|
|
|
|
$user = $this->em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $username]); |
|
|
|
|
|
|
|
|
|
if (!$user) { |
|
|
|
|
throw new \Exception(get_lang('NoUser')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$encoder = Container::$container->get('chamilo_user.security.encoder'); |
|
|
|
|
$encoder = $this->container->get('chamilo_user.security.encoder'); |
|
|
|
|
$isValid = $encoder->isPasswordValid( |
|
|
|
|
$user->getPassword(), |
|
|
|
|
$password, |
|
|
|
@ -42,7 +56,28 @@ class Auth |
|
|
|
|
throw new \Exception(get_lang('InvalidId')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return self::generateToken($user->getId()); |
|
|
|
|
return self::encodeToken($user); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param User $user |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function encodeToken(User $user): string |
|
|
|
|
{ |
|
|
|
|
$secret = $this->container->getParameter('secret'); |
|
|
|
|
$time = time(); |
|
|
|
|
|
|
|
|
|
$payload = [ |
|
|
|
|
'iat' => $time, |
|
|
|
|
'exp' => $time + (60 * 60 * 24), |
|
|
|
|
'data' => [ |
|
|
|
|
'user' => $user->getId(), |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
return JWT::encode($payload, $secret, 'HS384'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -50,19 +85,20 @@ class Auth |
|
|
|
|
* |
|
|
|
|
* @throws \Exception |
|
|
|
|
*/ |
|
|
|
|
public static function checkAuthorization(\ArrayObject $context): void |
|
|
|
|
public function checkAuthorization(\ArrayObject $context): void |
|
|
|
|
{ |
|
|
|
|
$header = Container::getRequest()->headers->get('Authorization'); |
|
|
|
|
$request = $this->container->get('request_stack')->getCurrentRequest(); |
|
|
|
|
$header = $request->headers->get('Authorization'); |
|
|
|
|
$token = str_replace(['Bearer ', 'bearer '], '', $header); |
|
|
|
|
|
|
|
|
|
if (empty($token)) { |
|
|
|
|
throw new \Exception(get_lang('NotAllowed')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$tokenData = Auth::getTokenData($token); |
|
|
|
|
$tokenData = $this->decodeToken($token); |
|
|
|
|
|
|
|
|
|
/** @var User $user */ |
|
|
|
|
$user = Container::getUserManager()->find($tokenData['user']); |
|
|
|
|
$user = $this->em->find('ChamiloUserBundle:User', $tokenData['user']); |
|
|
|
|
|
|
|
|
|
if (!$user) { |
|
|
|
|
throw new \Exception(get_lang('NotAllowed')); |
|
|
|
@ -71,35 +107,14 @@ class Auth |
|
|
|
|
$context->offsetSet('user', $user); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param int $userId |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private static function generateToken($userId): string |
|
|
|
|
{ |
|
|
|
|
$secret = Container::$container->getParameter('secret'); |
|
|
|
|
$time = time(); |
|
|
|
|
|
|
|
|
|
$payload = [ |
|
|
|
|
'iat' => $time, |
|
|
|
|
'exp' => $time + (60 * 60 * 24), |
|
|
|
|
'data' => [ |
|
|
|
|
'user' => $userId, |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
return JWT::encode($payload, $secret, 'HS384'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param string $token |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
private static function getTokenData($token): array |
|
|
|
|
private function decodeToken($token): array |
|
|
|
|
{ |
|
|
|
|
$secret = Container::$container->getParameter('secret'); |
|
|
|
|
$secret = $this->container->getParameter('secret'); |
|
|
|
|
$jwt = JWT::decode($token, $secret, ['HS384']); |
|
|
|
|
|
|
|
|
|
$data = (array) $jwt->data; |