{"id":21744571,"url":"https://github.com/cdoco/php-jwt","last_synced_at":"2025-04-03T02:09:36.644Z","repository":{"id":54451395,"uuid":"132859805","full_name":"cdoco/php-jwt","owner":"cdoco","description":":cyclone: A PHP extension for JSON Web Token (JWT)","archived":false,"fork":false,"pushed_at":"2021-02-17T05:05:07.000Z","size":102,"stargazers_count":230,"open_issues_count":10,"forks_count":49,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-24T07:58:31.052Z","etag":null,"topics":["c","jwt","php","php-jwt","php7-extension"],"latest_commit_sha":null,"homepage":"https://jwt.io","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdoco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-10T06:43:25.000Z","updated_at":"2025-03-14T08:56:43.000Z","dependencies_parsed_at":"2022-08-13T16:10:09.959Z","dependency_job_id":null,"html_url":"https://github.com/cdoco/php-jwt","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Fphp-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Fphp-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Fphp-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Fphp-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdoco","download_url":"https://codeload.github.com/cdoco/php-jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922247,"owners_count":20855345,"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","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":["c","jwt","php","php-jwt","php7-extension"],"created_at":"2024-11-26T07:11:57.247Z","updated_at":"2025-04-03T02:09:36.617Z","avatar_url":"https://github.com/cdoco.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status][travis-image]][travis-url]\n![PHP](https://img.shields.io/badge/PHP-%3E%3D7.0.0-orange.svg)\n![OpenSSL](https://img.shields.io/badge/OpenSSL-%3E%3D1.0.1f-orange.svg)\n![branch](https://img.shields.io/badge/branch-master-brightgreen.svg)\n![license](https://img.shields.io/badge/License-PHP/3.01-blue.svg)\n\n\u003e A PHP extension for [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519)\n\n## Requirement\n\n- PHP 7 +\n- PHP json extension, need to json extension before loading JWT extension.\n- OpenSSL (Version \u003e= 1.1.0j) Might work with older version as well, but I did not check that.\n\n## Install\n\n```shell\n$ git clone https://github.com/cdoco/php-jwt.git\n$ cd php-jwt\n$ phpize \u0026\u0026 ./configure --with-openssl=/path/to/openssl\n$ make \u0026\u0026 make install\n```\n\n## Quick [Example](https://github.com/cdoco/php-jwt/tree/master/example)\n\n```php\n$key = \"example-hmac-key\";\n$payload = [\n    \"data\" =\u003e [\n        \"name\" =\u003e \"ZiHang Gao\",\n        \"admin\" =\u003e true\n    ],\n    \"iss\" =\u003e \"http://example.org\",\n    \"sub\" =\u003e \"1234567890\",\n];\n\n// default HS256 algorithm\n$token = jwt_encode($payload, $key);\n\n// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7Im5hbWUiOiJaaUhhbmcgR2FvIiwiYWRtaW4iOnRydWV9LCJpc3MiOiJodHRwOlwvXC9leGFtcGxlLm9yZyIsInN1YiI6IjEyMzQ1Njc4OTAifQ.UcrCt9o9rz38kKMTa-nCrm7JNQRNAId5Xg9C7EIl2Zc\necho $token;\n\n$decoded_token = jwt_decode($token, $key);\n\n// Array\n// (\n//    [data] =\u003e Array\n//        (\n//            [name] =\u003e ZiHang Gao\n//            [admin] =\u003e 1\n//        )\n//\n//    [iss] =\u003e http://example.org\n//    [sub] =\u003e 1234567890\n// )\nprint_r($decoded_token);\n\n// or would you prefer to use a static method call\n$token = \\Cdoco\\JWT::encode($payload, $key);\n$decoded_token = \\Cdoco\\JWT::decode($token, $key);\n```\n\n## Algorithms and Usage\n\nThe JWT supports NONE, HMAC, RSASSA and ECDSA algorithms for cryptographic signing.\n\n#### NONE\n\n- none - unsigned token\n\n```php\n$payload = ['data' =\u003e 'test'];\n\n// IMPORTANT: set null as key parameter\n$token = jwt_encode($payload, null, 'none');\n\n// eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.\necho $token;\n\n// Set key to nil and options to false otherwise this won't work\n$decoded_token = jwt_decode($token, null, false);\n\n// Array\n// (\n//    [data] =\u003e test\n// )\nprint_r($decoded_token);\n```\n\n#### HMAC (default: HS256)\n\n- HS256 - HMAC using SHA-256 hash algorithm (default)\n- HS384 - HMAC using SHA-384 hash algorithm\n- HS512 - HMAC using SHA-512 hash algorithm\n\n```php\n$hmackey = \"example-hmac-key\";\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\n// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.C8kzOqBbcaPRhRdLWdNVSvYkIPIBPu7f_8-avoG-JiU\necho $token;\n\n$decoded_token = jwt_decode($token, $hmackey, ['algorithm' =\u003e 'HS256']);\n\n// Array\n// (\n//    [data] =\u003e test\n// )\nprint_r($decoded_token);\n```\n\n#### RSA\n\n- RS256 - RSA using SHA-256 hash algorithm\n- RS384 - RSA using SHA-384 hash algorithm\n- RS512 - RSA using SHA-512 hash algorithm\n\n```php\n$privateKey = file_get_contents('key/rsa_private_key.pem');\n$publicKey = file_get_contents('key/rsa_public_key.pem');\n\n$token = jwt_encode($payload, $privateKey, 'RS256');\n\n// eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pkpKlkzQWSkme42WcOxwkLeUttiLeNORzthSJeIt140iNEtRK_f8IotoinfIKI7Y6x8pfQ4n1DHJ_5IUDe6elds8gnhLwfq5XRY48BGc8Dc_QowVQd75m5fXI6nFySW8z8CAsbwn2Efg-p7SLdfhWpNQ9AISfwa_1l-OB3BgKFw\necho $token;\n\n$decoded_token = jwt_decode($token, $publicKey, ['algorithm' =\u003e 'RS256']);\n\n// Array\n// (\n//    [data] =\u003e test\n// )\nprint_r($decoded_token);\n```\n\n#### ECDSA\n\n- ES256 - ECDSA using P-256 and SHA-256\n- ES384 - ECDSA using P-384 and SHA-384\n- ES512 - ECDSA using P-521 and SHA-512\n\n```php\n$privateKey = file_get_contents('key/ec_private_key.pem');\n$publicKey = file_get_contents('key/ec_public_key.pem');\n\n$token = jwt_encode($payload, $privateKey, 'ES256');\n\n// eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.etzxzSvJi1QS5nUtKDuLX2sScZ5W50CJL6PivKys45nc77QLxnLsF5QQApEAis8SI28rqwP9VITqPPlwJBNdH3N5n0I58z3jevGJYOfRtBnCa6omUNE03nxoEYMqRBuP\necho $token;\n\n$decoded_token = jwt_decode($token, $publicKey, ['algorithm' =\u003e 'ES256']);\n\n// Array\n// (\n//    [data] =\u003e test\n// )\nprint_r($decoded_token);\n```\n\n## Support for reserved claim names\n\nJSON Web Token defines some reserved claim names and defines how they should be used. JWT supports these reserved claim names:\n\n- 'exp' (Expiration Time) Claim\n- 'nbf' (Not Before Time) Claim\n- 'iss' (Issuer) Claim\n- 'aud' (Audience) Claim\n- 'jti' (JWT ID) Claim\n- 'iat' (Issued At) Claim\n- 'sub' (Subject) Claim\n\n### Expiration Time Claim\n\n\u003e The `exp` (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the `exp` claim requires that the current date/time MUST be before the expiration date/time listed in the `exp` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a **NumericDate** value. Use of this claim is OPTIONAL.\n\n#### Handle Expiration Claim\n\n```php\n$payload = ['data' =\u003e 'data', 'exp' =\u003e time() + 4 * 3600];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['algorithm' =\u003e 'HS256']);\n} catch (ExpiredSignatureException $e) {\n    // Expired token\n}\n```\n\n#### Adding Leeway\n\n```php\n$payload = ['data' =\u003e 'data', 'exp' =\u003e time() - 10];\n\n// build expired token\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['leeway' =\u003e 30, 'algorithm' =\u003e 'HS256']);\n} catch (ExpiredSignatureException $e) {\n    // Expired token\n}\n```\n\n### Not Before Time Claim\n\n\u003e The `nbf` (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. The processing of the `nbf` claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the `nbf` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a **NumericDate** value. Use of this claim is OPTIONAL.\n\n#### Handle Not Before Claim\n\n```php\n$payload = ['data' =\u003e 'data', 'nbf' =\u003e time() - 3600];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['algorithm' =\u003e 'HS256']);\n} catch (BeforeValidException $e) {\n    // Handle invalid token\n}\n```\n\n#### Adding Leeway\n\n```php\n$payload = ['data' =\u003e 'data', 'nbf' =\u003e time() + 10];\n\n// build expired token\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['leeway' =\u003e 30, 'algorithm' =\u003e 'HS256']);\n} catch (BeforeValidException $e) {\n    // Handle invalid token\n}\n```\n\n### Issuer Claim\n\n\u003e The `iss` (issuer) claim identifies the principal that issued the JWT. The processing of this claim is generally application specific. The `iss` value is a case-sensitive string containing a **StringOrURI** value. Use of this claim is OPTIONAL.\n\n```php\n$payload = ['data' =\u003e 'data', 'iss' =\u003e 'http://example.org'];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['iss' =\u003e 'http://example.org', 'algorithm' =\u003e 'HS256']);\n} catch (InvalidIssuerException $e) {\n     // Handle invalid token\n}\n```\n\n### Audience Claim\n\n\u003e The `aud` (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the `aud` claim when this claim is present, then the JWT MUST be rejected. In the general case, the `aud` value is an array of case-sensitive strings, each containing a **StringOrURI** value. In the special case when the JWT has one audience, the `aud` value MAY be a single case-sensitive string containing a **StringOrURI** value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.\n\n```php\n$payload = ['data' =\u003e 'data', 'aud' =\u003e ['Young', 'Old']];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['aud' =\u003e ['Young', 'Old'], 'algorithm' =\u003e 'HS256']);\n} catch (InvalidAudException $e) {\n     // Handle invalid token\n}\n```\n\n### JWT ID Claim\n\n\u003e The `jti` (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The `jti` claim can be used to prevent the JWT from being replayed. The `jti` value is a **case-sensitive string**. Use of this claim is OPTIONAL.\n\n```php\n$payload = ['data' =\u003e 'data', 'jti' =\u003e md5('id')];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['jti' =\u003e md5('id'), 'algorithm' =\u003e 'HS256']);\n} catch (InvalidJtiException $e) {\n     // Handle invalid token\n}\n```\n\n### Issued At Claim\n\n\u003e The `iat` (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a **NumericDate** value. Use of this claim is OPTIONAL.\n\n```php\n$payload = ['data' =\u003e 'data', 'iat' =\u003e time()];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['algorithm' =\u003e 'HS256']);\n} catch (InvalidIatException $e) {\n     // Handle invalid token\n}\n```\n\n### Subject Claim\n\n\u003e The `sub` (subject) claim identifies the principal that is the subject of the JWT. The Claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific. The sub value is a case-sensitive string containing a **StringOrURI** value. Use of this claim is OPTIONAL.\n\n```php\n$payload = ['data' =\u003e 'data', 'sub' =\u003e 'Subject'];\n\n$token = jwt_encode($payload, $hmackey, 'HS256');\n\ntry {\n    $decoded_token = jwt_decode($token, $hmackey, ['sub' =\u003e 'Subject', 'algorithm' =\u003e 'HS256']);\n} catch (InvalidSubException $e) {\n     // Handle invalid token\n}\n```\n\n## Benchmarks\n\n![Benchmarks](https://cdoco.com/images/jwt-benchmarks.png \"Benchmarks\")\n\n## Functions\n\n```php\n// encode\nstring jwt_encode(array $payload, string $key [, string $algorithm = 'HS256'])\n\n// decode\narray jwt_decode(string $token, string $key [, array $options = ['algorithm' =\u003e 'HS256']])\n```\n\n## IDE Helper\n\n```php\n\u003c?php\n/**\n * Put this file into root of your project for IDE helper\n * Note: This file don't need to be require/include\n */\nnamespace {\n    function jwt_encode(array $payload, string $key, string $algorithm = 'HS256'): string\n    {\n        return '';\n    }\n\n    function jwt_decode(string $token, string $key, string $algorithm = 'HS256'): array\n    {\n        return [];\n    }\n}\n\nnamespace Cdoco {\n    // @codingStandardsIgnoreStart\n    abstract class JWT\n    // @codingStandardsIgnoreEnd\n    {\n        abstract public static function encode(array $payload, string $key, string $algorithm = 'HS256'): string;\n\n        abstract public static function decode(string $token, string $key, string $algorithm = 'HS256'): array;\n    }\n}\n```\n\n## The algorithm of support\n\nalgorithm|-|-|-\n-|-|-|-\nHMAC|HS256|HS384|HS512\nRSA|RS256|RS384|RS512\nECDSA|ES256|ES384|ES512\n\n## Inspired By\n\n- \u003chttps://github.com/benmcollins/libjwt\u003e\n- \u003chttps://github.com/firebase/php-jwt\u003e\n- \u003chttps://github.com/kohkimakimoto/php-jwt\u003e\n- \u003chttps://github.com/jwt/ruby-jwt\u003e\n\n## License\n\nPHP License 3.01. See the [LICENSE](LICENSE) file.\n\n[travis-url]: https://travis-ci.org/cdoco/php-jwt\n[travis-image]: https://travis-ci.org/cdoco/php-jwt.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdoco%2Fphp-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdoco%2Fphp-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdoco%2Fphp-jwt/lists"}