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

feat: Allows Key instance to be used for encoding #575

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public static function decode(
* Converts and signs a PHP array into a JWT string.
*
* @param array<mixed> $payload PHP array
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
* @param string $alg Supported algorithms are 'ES384','ES256', 'ES256K', 'HS256',
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate|Key $key The secret key.
* @param ?string $alg Supported algorithms are 'ES384','ES256', 'ES256K', 'HS256',
* 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
* @param string $keyId
* @param array<string, string> $head An array with header elements to attach
Expand All @@ -199,10 +199,16 @@ public static function decode(
public static function encode(
array $payload,
$key,
string $alg,
?string $alg = null,
string $keyId = null,
array $head = null
): string {
if (is_a($key, Key::class)) {
$alg = $key->getAlgorithm();
$key = $key->getKeyMaterial();
} elseif ($alg === null) {
throw new InvalidArgumentException('alg cannot be null unless key is instance of Key');
}
$header = ['typ' => 'JWT'];
if (isset($head) && \is_array($head)) {
$header = \array_merge($header, $head);
Expand All @@ -226,8 +232,8 @@ public static function encode(
* Sign a string with a given key and algorithm.
*
* @param string $msg The message to sign
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
* @param string $alg Supported algorithms are 'EdDSA', 'ES384', 'ES256', 'ES256K', 'HS256',
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate|Key $key The secret key.
* @param ?string $alg Supported algorithms are 'EdDSA', 'ES384', 'ES256', 'ES256K', 'HS256',
* 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
*
* @return string An encrypted message
Expand All @@ -237,8 +243,14 @@ public static function encode(
public static function sign(
string $msg,
$key,
string $alg
?string $alg = null
): string {
if (is_a($key, Key::class)) {
$alg = $key->getAlgorithm();
$key = $key->getKeyMaterial();
} elseif ($alg === null) {
throw new InvalidArgumentException('alg cannot be null unless key is instance of Key');
}
if (empty(static::$supported_algs[$alg])) {
throw new DomainException('Algorithm not supported');
}
Expand Down