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

Include Client ID (kid) parameter in the JWT header #932

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require-dev": {
"phpunit/phpunit": "^4.0",
"aws/aws-sdk-php": "~2.8",
"firebase/php-jwt": "~2.2",
"firebase/php-jwt": "~3.0",
"predis/predis": "dev-master",
"thobbs/phpcassa": "dev-master",
"mongodb/mongodb": "^1.1"
Expand All @@ -30,7 +30,7 @@
"predis/predis": "Required to use Redis storage",
"thobbs/phpcassa": "Required to use Cassandra storage",
"aws/aws-sdk-php": "~2.8 is required to use DynamoDB storage",
"firebase/php-jwt": "~2.2 is required to use JWT features",
"firebase/php-jwt": "~3.0 is required to use JWT features",
"mongodb/mongodb": "^1.1 is required to use MongoDB storage"
}
}
2 changes: 1 addition & 1 deletion src/OAuth2/Encryption/EncryptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface EncryptionInterface
* @param null $algorithm
* @return mixed
*/
public function encode($payload, $key, $algorithm = null);
public function encode($payload, $key, $algorithm = null, $keyId = null);

/**
* @param $payload
Expand Down
10 changes: 5 additions & 5 deletions src/OAuth2/Encryption/FirebaseJwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class FirebaseJwt implements EncryptionInterface
{
public function __construct()
{
if (!class_exists('\JWT')) {
if (!class_exists('\Firebase\JWT\JWT')) {
throw new \ErrorException('firebase/php-jwt must be installed to use this feature. You can do this by running "composer require firebase/php-jwt"');
}
}

public function encode($payload, $key, $alg = 'HS256', $keyId = null)
{
return \JWT::encode($payload, $key, $alg, $keyId);
return \Firebase\JWT\JWT::encode($payload, $key, $alg, $keyId);
}

public function decode($jwt, $key = null, $allowedAlgorithms = null)
Expand All @@ -29,19 +29,19 @@ public function decode($jwt, $key = null, $allowedAlgorithms = null)
$key = null;
}

return (array)\JWT::decode($jwt, $key, $allowedAlgorithms);
return (array)\Firebase\JWT\JWT::decode($jwt, $key, $allowedAlgorithms);
} catch (\Exception $e) {
return false;
}
}

public function urlSafeB64Encode($data)
{
return \JWT::urlsafeB64Encode($data);
return \Firebase\JWT\JWT::urlsafeB64Encode($data);
}

public function urlSafeB64Decode($b64)
{
return \JWT::urlsafeB64Decode($b64);
return \Firebase\JWT\JWT::urlsafeB64Decode($b64);
}
}
12 changes: 8 additions & 4 deletions src/OAuth2/Encryption/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Jwt implements EncryptionInterface
* @param string $algo
* @return string
*/
public function encode($payload, $key, $algo = 'HS256')
public function encode($payload, $key, $algo = 'HS256', $keyId = null)
{
$header = $this->generateJwtHeader($payload, $algo);
$header = $this->generateJwtHeader($payload, $algo, $keyId);

$segments = array(
$this->urlSafeB64Encode(json_encode($header)),
Expand Down Expand Up @@ -195,12 +195,16 @@ public function urlSafeB64Decode($b64)
/**
* Override to create a custom header
*/
protected function generateJwtHeader($payload, $algorithm)
protected function generateJwtHeader($payload, $algorithm, $keyId = null)
{
return array(
$header = array(
'typ' => 'JWT',
'alg' => $algorithm,
);
if (!is_null($keyId)) {
$header['kid'] = $keyId;
}
return $header;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/OpenID/ResponseType/IdToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function encodeToken(array $token, $client_id = null)
$private_key = $this->publicKeyStorage->getPrivateKey($client_id);
$algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);

return $this->encryptionUtil->encode($token, $private_key, $algorithm);
return $this->encryptionUtil->encode($token, $private_key, $algorithm, $client_id);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/ResponseType/JwtAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function encodeToken(array $token, $client_id = null)
$private_key = $this->publicKeyStorage->getPrivateKey($client_id);
$algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id);

return $this->encryptionUtil->encode($token, $private_key, $algorithm);
return $this->encryptionUtil->encode($token, $private_key, $algorithm, $client_id);
}

/**
Expand Down