feat: Allows Key instance to be used for encoding#575
Open
Oinkling wants to merge 6 commits intofirebase:mainfrom
Open
feat: Allows Key instance to be used for encoding#575Oinkling wants to merge 6 commits intofirebase:mainfrom
Oinkling wants to merge 6 commits intofirebase:mainfrom
Conversation
Allows an instance of Key to be passed as the second argument to JWT::encode and JWT::sign, making $alg optional.
bshaffer
reviewed
Apr 9, 2025
Collaborator
bshaffer
left a comment
There was a problem hiding this comment.
this looks good, and I can see the logic in it... but I am not thrilled about having two ways of doing the same thing. It would be less confusing, looking at the phpdoc, to just have the one way (as it is today).
But I don't have a real issue with it. Could you add some unit tests as well?
src/JWT.php
Outdated
| string $keyId = null, | ||
| array $head = null | ||
| ): string { | ||
| if (is_a($key, Key::class)) { |
Collaborator
There was a problem hiding this comment.
nit: this library prefers the use of instanceof
Suggested change
| if (is_a($key, Key::class)) { | |
| if ($key instanceof Key::class) { |
| ): string { | ||
| if (is_a($key, Key::class)) { | ||
| $alg = $key->getAlgorithm(); | ||
| $key = $key->getKeyMaterial(); |
Collaborator
There was a problem hiding this comment.
We would also want to throw an exception if $alg isn't null. Otherwise this would be possible and potentially cause confusion:
JWT::encode($payload, $key, 'FooAlg');
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allows an instance of Key to be passed as the second argument to
JWT::encodeandJWT::sign, making $alg optional.This is more in line with how decoding works and as such it makes sense to streamline it.
This also generally makes the process easier in the use case where you get an instance of Key injected so you can just encode using
JWT::encode($payload, $key)rather thanJWT::encode($payload, $key->getKeyMaterial(), $key->getAlgorithm())