1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 15:04:25 +02:00
Files
php-flarum/src/Api/Controller/CreateTokenController.php
Matt Kilgore d7a5a6ad14 Change Zend namespace to Laminas (#1963)
Also ensure backwards compatibility for extensions that use the Zend framework but don't explicitly require it.
2020-01-06 22:29:34 +01:00

78 lines
1.9 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Http\AccessToken;
use Flarum\User\Exception\NotAuthenticatedException;
use Flarum\User\UserRepository;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class CreateTokenController implements RequestHandlerInterface
{
/**
* @var \Flarum\User\UserRepository
*/
protected $users;
/**
* @var BusDispatcher
*/
protected $bus;
/**
* @var EventDispatcher
*/
protected $events;
/**
* @param UserRepository $users
* @param BusDispatcher $bus
* @param EventDispatcher $events
*/
public function __construct(UserRepository $users, BusDispatcher $bus, EventDispatcher $events)
{
$this->users = $users;
$this->bus = $bus;
$this->events = $events;
}
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$body = $request->getParsedBody();
$identification = Arr::get($body, 'identification');
$password = Arr::get($body, 'password');
$lifetime = Arr::get($body, 'lifetime', 3600);
$user = $this->users->findByIdentification($identification);
if (! $user || ! $user->checkPassword($password)) {
throw new NotAuthenticatedException;
}
$token = AccessToken::generate($user->id, $lifetime);
$token->save();
return new JsonResponse([
'token' => $token->token,
'userId' => $user->id
]);
}
}