-
-
Notifications
You must be signed in to change notification settings - Fork 603
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
Tighten string
to non-empty-string
where possible
#936
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ interface Parser | |
/** | ||
* Parses the JWT and returns a token | ||
* | ||
* @param non-empty-string $jwt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a parser should accept an empty string here, with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty string JWT is always an error, just like a boolean would be, that's why there's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to think about it, but I see a I agree that a In the following (valid) code: function foo(string $token) {
Assert::notEmpty($token);
$parsed = $this->parser->parse($token);
// ... If a I'm just trying to figure out the ergonomics of the library here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I understand better your doubts. I can only say the sooner the type is safe, the better:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one good thing coming from this new type restriction: the existence of the token must be verified by the consumer. Anyway, I'm just presenting the options: I think that the parser (being a parser) is responsible for upcasting what comes in (or throwing), so it's our decision to decide how wide the API is supposed to be. The parser makes the type safe anyway, so I have doubts on the value of an early assertion, in this specific case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, we can experiment with this, and then later-on relax the type constraint, if we consider it excessive. |
||
* | ||
* @throws CannotDecodeContent When something goes wrong while decoding. | ||
* @throws InvalidTokenStructure When token string structure is invalid. | ||
* @throws UnsupportedHeaderFound When parsed token has an unsupported header. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter; | ||
use Lcobucci\JWT\Signer\Ecdsa\SignatureConverter; | ||
|
||
use function assert; | ||
|
||
use const OPENSSL_KEYTYPE_EC; | ||
|
||
abstract class Ecdsa extends OpenSSL | ||
|
@@ -19,6 +21,7 @@ public static function create(): Ecdsa | |
return new static(new MultibyteStringConverter()); // @phpstan-ignore-line | ||
} | ||
|
||
/** @return non-empty-string */ | ||
final public function sign(string $payload, Key $key): string | ||
{ | ||
return $this->converter->fromAsn1( | ||
|
@@ -29,6 +32,8 @@ final public function sign(string $payload, Key $key): string | |
|
||
final public function verify(string $expected, string $payload, Key $key): bool | ||
{ | ||
assert($expected !== ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, is the signature not supposed to be non-empty upfront? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not until we allow
|
||
|
||
return $this->verifySignature( | ||
$this->converter->toAsn1($expected, $this->pointLength()), | ||
$payload, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I would expect psalm and phpstan to have better type inference here 🤔