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

replacing codercats jwk converter with web-token to support EC tokens #15

Merged
merged 1 commit into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"league/oauth2-server": "^8.1"
"league/oauth2-server": "^8.1",
"web-token/jwt-core": "^2.2"
},
"require-dev": {
"ext-xdebug": "*",
Expand Down
24 changes: 17 additions & 7 deletions src/Utils/DPop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\ValidationData;
use CoderCat\JWKToPEM\JWKConverter;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\ECKey;
use Jose\Component\Core\Util\RSAKey;

class DPop {
public function getWebId($request) {
Expand Down Expand Up @@ -111,17 +113,25 @@ private function validateDpop($dpop, $request) {
if ($alg == "none") {
throw new \Exception("alg is none");
}
if ($alg != "RS256") {
throw new \Exception("alg is not supported");
}

//error_log("5");
// 5. that the JWT is signed using the public key contained in the
// "jwk" header of the JWT,
$jwk = $dpop->getHeader("jwk");
$jwkConverter = new JWKConverter();
$pem = $jwkConverter->toPEM(json_decode(json_encode($jwk), true));
$signer = new \Lcobucci\JWT\Signer\Rsa\Sha256();
$webTokenJwk = \Jose\Component\Core\JWK::createFromJson(json_encode($jwk));
switch ($alg) {
case "RS256":
$pem = \Jose\Component\Core\Util\RSAKey::createFromJWK($webTokenJwk)->toPEM();
$signer = new \Lcobucci\JWT\Signer\Rsa\Sha256();
break;
case "ES256":
$pem = \Jose\Component\Core\Util\ECKey::convertToPEM($webTokenJwk);
$signer = new \Lcobucci\JWT\Signer\Ecdsa\Sha256();
break;
default:
throw new \Exception("unsupported algorithm");
break;
}
$key = new \Lcobucci\JWT\Signer\Key($pem);
if (!$dpop->verify($signer, $key)) {
throw new \Exception("invalid signature");
Expand Down