{"id":15874756,"url":"https://github.com/rudifa/php-jwt","last_synced_at":"2025-10-13T14:06:34.399Z","repository":{"id":74523938,"uuid":"591352590","full_name":"rudifa/php-jwt","owner":"rudifa","description":"php files that decode Apple iTunes purchase reports","archived":false,"fork":false,"pushed_at":"2023-01-24T17:55:17.000Z","size":523,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-13T14:04:45.125Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rudifa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-20T14:51:00.000Z","updated_at":"2023-01-20T14:51:30.000Z","dependencies_parsed_at":"2023-02-25T20:00:26.133Z","dependency_job_id":null,"html_url":"https://github.com/rudifa/php-jwt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rudifa/php-jwt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudifa%2Fphp-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudifa%2Fphp-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudifa%2Fphp-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudifa%2Fphp-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rudifa","download_url":"https://codeload.github.com/rudifa/php-jwt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rudifa%2Fphp-jwt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279015699,"owners_count":26085747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-06T01:41:24.691Z","updated_at":"2025-10-13T14:06:34.379Z","avatar_url":"https://github.com/rudifa.png","language":"PHP","readme":"![Build Status](https://github.com/firebase/php-jwt/actions/workflows/tests.yml/badge.svg)\n[![Latest Stable Version](https://poser.pugx.org/firebase/php-jwt/v/stable)](https://packagist.org/packages/firebase/php-jwt)\n[![Total Downloads](https://poser.pugx.org/firebase/php-jwt/downloads)](https://packagist.org/packages/firebase/php-jwt)\n[![License](https://poser.pugx.org/firebase/php-jwt/license)](https://packagist.org/packages/firebase/php-jwt)\n\nPHP-JWT\n=======\nA simple library to encode and decode JSON Web Tokens (JWT) in PHP, conforming to [RFC 7519](https://tools.ietf.org/html/rfc7519).\n\nInstallation\n------------\n\nUse composer to manage your dependencies and download PHP-JWT:\n\n```bash\ncomposer require firebase/php-jwt\n```\n\nOptionally, install the `paragonie/sodium_compat` package from composer if your\nphp is \u003c 7.2 or does not have libsodium installed:\n\n```bash\ncomposer require paragonie/sodium_compat\n```\n\nExample\n-------\n```php\nuse Firebase\\JWT\\JWT;\nuse Firebase\\JWT\\Key;\n\n$key = 'example_key';\n$payload = [\n    'iss' =\u003e 'http://example.org',\n    'aud' =\u003e 'http://example.com',\n    'iat' =\u003e 1356999524,\n    'nbf' =\u003e 1357000000\n];\n\n/**\n * IMPORTANT:\n * You must specify supported algorithms for your application. See\n * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40\n * for a list of spec-compliant algorithms.\n */\n$jwt = JWT::encode($payload, $key, 'HS256');\n$decoded = JWT::decode($jwt, new Key($key, 'HS256'));\n\nprint_r($decoded);\n\n/*\n NOTE: This will now be an object instead of an associative array. To get\n an associative array, you will need to cast it as such:\n*/\n\n$decoded_array = (array) $decoded;\n\n/**\n * You can add a leeway to account for when there is a clock skew times between\n * the signing and verifying servers. It is recommended that this leeway should\n * not be bigger than a few minutes.\n *\n * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef\n */\nJWT::$leeway = 60; // $leeway in seconds\n$decoded = JWT::decode($jwt, new Key($key, 'HS256'));\n```\nExample with RS256 (openssl)\n----------------------------\n```php\nuse Firebase\\JWT\\JWT;\nuse Firebase\\JWT\\Key;\n\n$privateKey = \u003c\u003c\u003cEOD\n-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQC8kGa1pSjbSYZVebtTRBLxBz5H4i2p/llLCrEeQhta5kaQu/Rn\nvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t0tyazyZ8JXw+KgXTxldMPEL9\n5+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4ehde/zUxo6UvS7UrBQIDAQAB\nAoGAb/MXV46XxCFRxNuB8LyAtmLDgi/xRnTAlMHjSACddwkyKem8//8eZtw9fzxz\nbWZ/1/doQOuHBGYZU8aDzzj59FZ78dyzNFoF91hbvZKkg+6wGyd/LrGVEB+Xre0J\nNil0GReM2AHDNZUYRv+HYJPIOrB0CRczLQsgFJ8K6aAD6F0CQQDzbpjYdx10qgK1\ncP59UHiHjPZYC0loEsk7s+hUmT3QHerAQJMZWC11Qrn2N+ybwwNblDKv+s5qgMQ5\n5tNoQ9IfAkEAxkyffU6ythpg/H0Ixe1I2rd0GbF05biIzO/i77Det3n4YsJVlDck\nZkcvY3SK2iRIL4c9yY6hlIhs+K9wXTtGWwJBAO9Dskl48mO7woPR9uD22jDpNSwe\nk90OMepTjzSvlhjbfuPN1IdhqvSJTDychRwn1kIJ7LQZgQ8fVz9OCFZ/6qMCQGOb\nqaGwHmUK6xzpUbbacnYrIM6nLSkXgOAwv7XXCojvY614ILTK3iXiLBOxPu5Eu13k\neUz9sHyD6vkgZzjtxXECQAkp4Xerf5TGfQXGXhxIX52yH+N2LtujCdkQZjXAsGdm\nB2zNzvrlgRmgBrklMTrMYgm1NPcW+bRLGcwgW2PTvNM=\n-----END RSA PRIVATE KEY-----\nEOD;\n\n$publicKey = \u003c\u003c\u003cEOD\n-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8kGa1pSjbSYZVebtTRBLxBz5H\n4i2p/llLCrEeQhta5kaQu/RnvuER4W8oDH3+3iuIYW4VQAzyqFpwuzjkDI+17t5t\n0tyazyZ8JXw+KgXTxldMPEL95+qVhgXvwtihXC1c5oGbRlEDvDF6Sa53rcFVsYJ4\nehde/zUxo6UvS7UrBQIDAQAB\n-----END PUBLIC KEY-----\nEOD;\n\n$payload = [\n    'iss' =\u003e 'example.org',\n    'aud' =\u003e 'example.com',\n    'iat' =\u003e 1356999524,\n    'nbf' =\u003e 1357000000\n];\n\n$jwt = JWT::encode($payload, $privateKey, 'RS256');\necho \"Encode:\\n\" . print_r($jwt, true) . \"\\n\";\n\n$decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));\n\n/*\n NOTE: This will now be an object instead of an associative array. To get\n an associative array, you will need to cast it as such:\n*/\n\n$decoded_array = (array) $decoded;\necho \"Decode:\\n\" . print_r($decoded_array, true) . \"\\n\";\n```\n\nExample with a passphrase\n-------------------------\n\n```php\nuse Firebase\\JWT\\JWT;\nuse Firebase\\JWT\\Key;\n\n// Your passphrase\n$passphrase = '[YOUR_PASSPHRASE]';\n\n// Your private key file with passphrase\n// Can be generated with \"ssh-keygen -t rsa -m pem\"\n$privateKeyFile = '/path/to/key-with-passphrase.pem';\n\n// Create a private key of type \"resource\"\n$privateKey = openssl_pkey_get_private(\n    file_get_contents($privateKeyFile),\n    $passphrase\n);\n\n$payload = [\n    'iss' =\u003e 'example.org',\n    'aud' =\u003e 'example.com',\n    'iat' =\u003e 1356999524,\n    'nbf' =\u003e 1357000000\n];\n\n$jwt = JWT::encode($payload, $privateKey, 'RS256');\necho \"Encode:\\n\" . print_r($jwt, true) . \"\\n\";\n\n// Get public key from the private key, or pull from from a file.\n$publicKey = openssl_pkey_get_details($privateKey)['key'];\n\n$decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));\necho \"Decode:\\n\" . print_r((array) $decoded, true) . \"\\n\";\n```\n\nExample with EdDSA (libsodium and Ed25519 signature)\n----------------------------\n```php\nuse Firebase\\JWT\\JWT;\nuse Firebase\\JWT\\Key;\n\n// Public and private keys are expected to be Base64 encoded. The last\n// non-empty line is used so that keys can be generated with\n// sodium_crypto_sign_keypair(). The secret keys generated by other tools may\n// need to be adjusted to match the input expected by libsodium.\n\n$keyPair = sodium_crypto_sign_keypair();\n\n$privateKey = base64_encode(sodium_crypto_sign_secretkey($keyPair));\n\n$publicKey = base64_encode(sodium_crypto_sign_publickey($keyPair));\n\n$payload = [\n    'iss' =\u003e 'example.org',\n    'aud' =\u003e 'example.com',\n    'iat' =\u003e 1356999524,\n    'nbf' =\u003e 1357000000\n];\n\n$jwt = JWT::encode($payload, $privateKey, 'EdDSA');\necho \"Encode:\\n\" . print_r($jwt, true) . \"\\n\";\n\n$decoded = JWT::decode($jwt, new Key($publicKey, 'EdDSA'));\necho \"Decode:\\n\" . print_r((array) $decoded, true) . \"\\n\";\n````\n\nUsing JWKs\n----------\n\n```php\nuse Firebase\\JWT\\JWK;\nuse Firebase\\JWT\\JWT;\n\n// Set of keys. The \"keys\" key is required. For example, the JSON response to\n// this endpoint: https://www.gstatic.com/iap/verify/public_key-jwk\n$jwks = ['keys' =\u003e []];\n\n// JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\\JWT\\Key\n// objects. Pass this as the second parameter to JWT::decode.\nJWT::decode($payload, JWK::parseKeySet($jwks));\n```\n\nUsing Cached Key Sets\n---------------------\n\nThe `CachedKeySet` class can be used to fetch and cache JWKS (JSON Web Key Sets) from a public URI.\nThis has the following advantages:\n\n1. The results are cached for performance.\n2. If an unrecognized key is requested, the cache is refreshed, to accomodate for key rotation.\n3. If rate limiting is enabled, the JWKS URI will not make more than 10 requests a second.\n\n```php\nuse Firebase\\JWT\\CachedKeySet;\nuse Firebase\\JWT\\JWT;\n\n// The URI for the JWKS you wish to cache the results from\n$jwksUri = 'https://www.gstatic.com/iap/verify/public_key-jwk';\n\n// Create an HTTP client (can be any PSR-7 compatible HTTP client)\n$httpClient = new GuzzleHttp\\Client();\n\n// Create an HTTP request factory (can be any PSR-17 compatible HTTP request factory)\n$httpFactory = new GuzzleHttp\\Psr\\HttpFactory();\n\n// Create a cache item pool (can be any PSR-6 compatible cache item pool)\n$cacheItemPool = Phpfastcache\\CacheManager::getInstance('files');\n\n$keySet = new CachedKeySet(\n    $jwksUri,\n    $httpClient,\n    $httpFactory,\n    $cacheItemPool,\n    null, // $expiresAfter int seconds to set the JWKS to expire\n    true  // $rateLimit    true to enable rate limit of 10 RPS on lookup of invalid keys\n);\n\n$jwt = 'eyJhbGci...'; // Some JWT signed by a key from the $jwkUri above\n$decoded = JWT::decode($jwt, $keySet);\n```\n\nMiscellaneous\n-------------\n\n#### Exception Handling\n\nWhen a call to `JWT::decode` is invalid, it will throw one of the following exceptions:\n\n```php\nuse Firebase\\JWT\\JWT;\nuse Firebase\\JWT\\SignatureInvalidException;\nuse Firebase\\JWT\\BeforeValidException;\nuse Firebase\\JWT\\ExpiredException;\nuse DomainException;\nuse InvalidArgumentException;\nuse UnexpectedValueException;\n\ntry {\n    $decoded = JWT::decode($payload, $keys);\n} catch (InvalidArgumentException $e) {\n    // provided key/key-array is empty or malformed.\n} catch (DomainException $e) {\n    // provided algorithm is unsupported OR\n    // provided key is invalid OR\n    // unknown error thrown in openSSL or libsodium OR\n    // libsodium is required but not available.\n} catch (SignatureInvalidException $e) {\n    // provided JWT signature verification failed.\n} catch (BeforeValidException $e) {\n    // provided JWT is trying to be used before \"nbf\" claim OR\n    // provided JWT is trying to be used before \"iat\" claim.\n} catch (ExpiredException $e) {\n    // provided JWT is trying to be used after \"exp\" claim.\n} catch (UnexpectedValueException $e) {\n    // provided JWT is malformed OR\n    // provided JWT is missing an algorithm / using an unsupported algorithm OR\n    // provided JWT algorithm does not match provided key OR\n    // provided key ID in key/key-array is empty or invalid.\n}\n```\n\nAll exceptions in the `Firebase\\JWT` namespace extend `UnexpectedValueException`, and can be simplified\nlike this:\n\n```php\ntry {\n    $decoded = JWT::decode($payload, $keys);\n} catch (LogicException $e) {\n    // errors having to do with environmental setup or malformed JWT Keys\n} catch (UnexpectedValueException $e) {\n    // errors having to do with JWT signature and claims\n}\n```\n\n#### Casting to array\n\nThe return value of `JWT::decode` is the generic PHP object `stdClass`. If you'd like to handle with arrays\ninstead, you can do the following:\n\n```php\n// return type is stdClass\n$decoded = JWT::decode($payload, $keys);\n\n// cast to array\n$decoded = json_decode(json_encode($decoded), true);\n```\n\nTests\n-----\nRun the tests using phpunit:\n\n```bash\n$ pear install PHPUnit\n$ phpunit --configuration phpunit.xml.dist\nPHPUnit 3.7.10 by Sebastian Bergmann.\n.....\nTime: 0 seconds, Memory: 2.50Mb\nOK (5 tests, 5 assertions)\n```\n\nNew Lines in private keys\n-----\n\nIf your private key contains `\\n` characters, be sure to wrap it in double quotes `\"\"`\nand not single quotes `''` in order to properly interpret the escaped characters.\n\nLicense\n-------\n[3-Clause BSD](http://opensource.org/licenses/BSD-3-Clause).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudifa%2Fphp-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frudifa%2Fphp-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frudifa%2Fphp-jwt/lists"}