54 return self::SUPPORTED_ALGORITHMS;
64 return !empty($this->jwtString);
85 $sections = explode(
'.', $jwtString);
86 $ok = count($sections) === 3;
90 $ok = !is_null($headers) && !is_null(
$payload);
94 $this->jwtHeaders = $headers;
97 $this->jwtString = null;
98 $this->jwtHeaders = null;
99 $this->jwtPayload = null;
122 return !empty($this->jwtHeaders) && isset($this->jwtHeaders[$name]);
134 $value = $this->jwtHeaders[
$name];
136 $value = $defaultValue;
159 return self::$lastHeaders;
169 return !empty($this->jwtPayload) && isset($this->jwtPayload[$name]);
181 $value = $this->jwtPayload[
$name];
183 $value = $defaultValue;
206 return self::$lastPayload;
215 public function verify(
string $publicKey,
string $jku = null): bool
218 $hasPublicKey = !empty($publicKey);
220 if (is_string($publicKey)) {
221 $json = json_decode($publicKey,
true);
222 if (!is_null($json)) {
224 $jwks = array(
'keys' => array($json));
225 $publicKey = static::parseKeySet($jwks);
229 $publicKey =
new Key($publicKey, $this->
getHeader(
'alg'));
232 } elseif (!empty(
$jku)) {
246 } elseif ($hasPublicKey && !empty(
$jku)) {
254 }
while (!$ok && $retry);
277 string $publicKey = null
280 $errorMessage =
'Encrypted tokens not supported by the Firebase JWT client';
282 throw new \Exception($errorMessage);
284 $jwtString =
JWT::encode($payload, $privateKey, $signatureMethod,
$kid);
285 $sections = explode(
'.', $jwtString);
312 "private_key_bits" => $size,
313 "private_key_type" => OPENSSL_KEYTYPE_RSA
332 $res = openssl_pkey_get_private($privateKey);
333 if (
$res !==
false) {
350 $keys[
'keys'] = array();
351 $res = openssl_pkey_get_private($pemKey);
352 if (
$res ===
false) {
353 $res = openssl_pkey_get_public($pemKey);
355 if (
$res !==
false) {
384 $publicKey = array();
388 $publicKey = static::parseKeySet(
$keys);
406 if (!isset($jwks[
'keys'])) {
407 throw new \UnexpectedValueException(
'"keys" member must exist in the JWK Set');
409 if (empty($jwks[
'keys'])) {
410 throw new \InvalidArgumentException(
'JWK Set did not contain any keys');
413 foreach ($jwks[
'keys'] as $k => $v) {
414 if (!empty($v[
'alg'])) {
415 $kid = isset($v[
'kid']) ? $v[
'kid'] : $k;
424 throw new \UnexpectedValueException(
'No supported algorithms found in JWK Set');
static parseKey(array $jwk, string $defaultAlg=null)
Parse a JWK key.
static urlsafeB64Encode(string $input)
Encode a string with URL-safe Base64.
if(count($parts) !=3) $payload
static array static decode(string $jwt, $keyOrKeyArray, stdClass &$headers=null)
Decodes a JWT string into a PHP object.
static encode(array $payload, $key, string $alg, string $keyId=null, array $head=null)
Converts and signs a PHP array into a JWT string.
static urlsafeB64Decode(string $input)
Decode a string with URL-safe Base64.