kpicaza / expressive-jwt

Zend Expressive JWT token utility
3 stars 1 forks source link

Have some docs to use? #1

Closed lpj145 closed 4 years ago

lpj145 commented 5 years ago

When added on my project, i found some difficulties to execute, you have some docs to implement it ?

kpicaza commented 5 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

lpj145 commented 5 years ago

i'm need adapt to my project, so, i inspect the code and found some interface for adapter, i think this needed doc ?

kpicaza commented 5 years ago

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;
    }
}
lpj145 commented 5 years ago

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 ?

lpj145 commented 5 years ago

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...

kpicaza commented 5 years ago

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

lpj145 commented 5 years ago

is good!, if you help in some things tell me.