forked from luciferous/jwt
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
feat!: Require Key object when decoding JWT objects #364
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31a7c16
Proposed Fix for #351
paragonie-security e08160a
Add unit tests for current behavior
paragonie-security 28eb0e3
add key object support in v5
bshaffer 65920fd
fix array syntax for PHP 5.3
bshaffer c6964eb
feat: require "Key" object when decoding JWTs
bshaffer 8c188aa
cleanup
bshaffer e58f86b
Merge branch 'master' into add-key-object
bshaffer 7ec98ac
Merge branch 'master' into add-key-object
bshaffer 4062eed
fix tests from merge
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ apt-get install -y --no-install-recommends \ | |
git \ | ||
zip \ | ||
curl \ | ||
ca-certificates \ | ||
unzip \ | ||
wget | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Firebase\JWT; | ||
|
||
use InvalidArgumentException; | ||
use OpenSSLAsymmetricKey; | ||
|
||
class Key | ||
{ | ||
/** @var string $algorithm */ | ||
private $algorithm; | ||
|
||
/** @var string $keyMaterial */ | ||
private $keyMaterial; | ||
|
||
/** | ||
* @param string|resource $keyMaterial | ||
* @param string $algorithm | ||
*/ | ||
public function __construct($keyMaterial, $algorithm) | ||
{ | ||
if ( | ||
!is_string($keyMaterial) | ||
&& !is_resource($keyMaterial) | ||
&& !$keyMaterial instanceof OpenSSLAsymmetricKey | ||
) { | ||
throw new InvalidArgumentException('Type error: $keyMaterial must be a string, resource, or OpenSSLAsymmetricKey'); | ||
} | ||
|
||
if (empty($keyMaterial)) { | ||
throw new InvalidArgumentException('Type error: $keyMaterial must not be empty'); | ||
} | ||
|
||
if (!is_string($algorithm)|| empty($keyMaterial)) { | ||
throw new InvalidArgumentException('Type error: $algorithm must be a string'); | ||
} | ||
$this->keyMaterial = $keyMaterial; | ||
$this->algorithm = $algorithm; | ||
} | ||
|
||
/** | ||
* Return the algorithm valid for this key | ||
* | ||
* @return string | ||
*/ | ||
public function getAlgorithm() | ||
{ | ||
return $this->algorithm; | ||
} | ||
|
||
/** | ||
* @return string|resource|OpenSSLAsymmetricKey | ||
*/ | ||
public function getKeyMaterial() | ||
{ | ||
return $this->keyMaterial; | ||
} | ||
} |
Oops, something went wrong.
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.
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.
@paragonie-security if the "alg" parameter is missing from a JWK, is there a reliable way to detect it? Or is it best to just throw the exception and require the users to specify it manually?
We can use the "kty" field to find out of the key is of type RSA or EC. I imagine there is a way to detect more algorithms from there as well, but I don't want to add magic unless I know it's reliable.
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.
It's possible, but it's going to require a bit of additional complexity.
First, you have to know if you're dealing with a DER- and PEM-encoded key.
This table below maps some hex-encoded ASN.1 prefixes to the respective algorithms, based on this encoder and the OIDs specified in IETF RFCs.
06 09 2A 86 48 86 F7 0D 01 01 01
06 07 2A 86 48 CE 3D 02 01
30 2E 02 01 00 30 05 06 03 2B 65 70 04 22 04 20
So that's what you would need to do.
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.
(This is on top of the
-----BEGIN PUBLIC KEY-----
and-----END PUBLIC KEY-----
, of course.)