Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How get Claims after encode token #203

Closed
lpj145 opened this issue Oct 13, 2017 · 6 comments · Fixed by #616
Closed

How get Claims after encode token #203

lpj145 opened this issue Oct 13, 2017 · 6 comments · Fixed by #616
Assignees
Labels

Comments

@lpj145
Copy link

lpj145 commented Oct 13, 2017

I'm try to get claim on middleware.
Using Version 4.0@dev
image
But parse function return Plain, this is a error ?
image

@lcobucci
Copy link
Owner

@lpj145 that's a forward compatibility layer. As stated on #6 we plan to support encrypted tokens (which will implement that interface).

Since parse() deals with untrusted data, users are supposed to verify if they have the expected type of token, like:

$token = $this->parser->parse($jwt);

if (! $token instanceof Plain) {
    // You should have your own exception for that, sure =)
    throw new \RuntimeException('The provided token is not valid');
}

@lcobucci
Copy link
Owner

btw don't forget to validate the token properly using the validator 😉

@lcobucci lcobucci self-assigned this Oct 13, 2017
@lpj145
Copy link
Author

lpj145 commented Oct 13, 2017

yep i'm using:

/**
     * @param Plain $token
     * @return bool
     */
    private function tokenIsValid(Plain $token)
    {
        if ($token->isExpired((new SystemClock())->now())) {
            return false;
        }

        try {
            $this->validator->assert($token, new SignedWith($this->signer, $this->key));
        }catch (ConstraintViolationException $exception) {
            return false;
        }

        return true;
    }

@lcobucci
Copy link
Owner

lcobucci commented Oct 13, 2017

@lpj145 if you're not using the exception, I'd suggest to use this instead (I really need to work on the documentation 😭 ):

return $this->validator->validate(
    $token,
    new ValidAt(new SystemClock()),
    new SignedWith($this->signer, $this->key)
);

The benefits with that approach are: the validator will stop its process when the first error comes and the constraints will be processed in the order you called the method - so you can leave the "expensive" operations to the end of your validation chain.

I'd also suggest to have the list of constraints injected, so it makes it easier for you (we're planning to add it to the Configuration object):

return $this->validator->validate($token, ...$this->constraints);

@lcobucci
Copy link
Owner

@lpj145 let me know if your question has been answered and we can close this issue 👍

@lpj145
Copy link
Author

lpj145 commented Oct 13, 2017

I think this is a good solution, but need tests. in hour i answer to you.

public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        if (
            $this->middlewareConfig['secure'] &&
            $request->getUri()->getScheme() !== 'https' ||
            !$request->hasHeader('Authorization')
        ) {
            return new Unauthorized();
        }

        $jwt = $this->getTokenWithoutBearer($request->getHeaderLine('Authorization'));

        /** @var Plain $token */
        $token = $this->parser->parse($jwt);
        if (
            !$token instanceof Plain ||
            !$this->tokenIsValid($token)
        ) {
            return new Unauthorized();
        }

        return $delegate->process($request);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants