Closed lpj145 closed 4 years ago
Not currently(except in the README), but I can help you if you want, explain to me your problems and I will do my best to help you. ;-D
i'm need adapt to my project, so, i inspect the code and found some interface for adapter, i think this needed doc ?
You are right, it needs some example or implementation of something like CreateTokenMiddleware
and TokenValidationMiddleware
, I added it to my backlog, thanks for your feedback
While there is more documentation available, these examples may be useful for you
<?php
declare(strict_types=1);
namespace App\Application\Http\Middleware;
use Auth\Model\Token;
use Auth\Service\ValidateToken;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReallySimpleJWT\Exception\TokenValidatorException;
use Zend\Diactoros\Response\JsonResponse;
class JwtTokenAuthMiddleware implements MiddlewareInterface
{
private $validateToken;
public function __construct(ValidateToken $validateToken)
{
$this->validateToken = $validateToken;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$authHeader = $request->getHeader('Authorization')[0] ?? false;
if (false === $authHeader) {
return new JsonResponse([], 401);
}
$validateToken = $this->validateToken;
try {
$token = Token::fromString(\substr($authHeader, 7));
$payload = $validateToken($token);
} catch (TokenValidatorException $exception) {
return new JsonResponse([], 401);
}
$userId = $payload->jsonSerialize()['user_id'] ?? null;
if (false === $userId) {
return new JsonResponse([], 403);
}
return $handler->handle($request->withAttribute('user_id', $userId));
}
}
<?php
declare(strict_types=1);
namespace App\Application\Http;
use App\Domain\Command\CreateTokenCommand;
use App\Domain\Exception\UserDoesNotExist;
use App\Domain\Model\Aggregate\User;
use App\Domain\UserRepository;
use Auth\Model\Identifier;
use Auth\Model\Token;
use Auth\Service\CreateToken;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
class CreateTokenHandlerExample implements RequestHandlerInterface
{
/** @var UserRepository */
private $repository;
/** @var CreateToken */
private $createToken;
public function __construct(UserRepository $repository, CreateTokenService $createToken)
{
$this->repository = $repository;
$this->createToken = $createToken;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
try {
$user = $this->getUser($request->getParsedBody()['email']);
$user->signIn($request->getParsedBody()['password']);
$createToken = $this->createToken;
/** @var Token $token */
$token = $createToken(Identifier::fromString((string)$user->id()));
} catch (UserDoesNotExist $exception) {
$token = null;
}
if (null === $token) {
return new JsonResponse([], 401);
}
return new JsonResponse([
'resource' => 'token',
'token' => (string)$token,
], 201);
}
private function getUser(string $email): User
{
$user = $this->repository->byEmail($email);
if (null === $user) {
throw UserDoesNotExist::withEmail($email);
}
return $user;
}
}
thanks to some improve, if you like me, i think the best micro framework is expressive, and think create some cli tools for improve it.... what do you think ?
i don't thin in skeleton, but on some lib or tools... so always require some/namespace it's added to service provider by the way Config on zf-extras composer...
I'm currently working in my free time in something you may like https://antidotfw.io/ is currently on development and some repos are not available, but it will be available when I have a minimum functional version ready
is good!, if you help in some things tell me.
When added on my project, i found some difficulties to execute, you have some docs to implement it ?