{"id":16666851,"url":"https://github.com/mishal/jwt","last_synced_at":"2026-03-09T10:04:00.805Z","repository":{"id":57017564,"uuid":"46346900","full_name":"mishal/Jwt","owner":"mishal","description":"JWT (JSON Web Tokens) for PHP","archived":false,"fork":false,"pushed_at":"2018-08-28T13:14:25.000Z","size":32,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T19:16:50.245Z","etag":null,"topics":["json","jwt","php","security","webtoken"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mishal.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}},"created_at":"2015-11-17T12:52:07.000Z","updated_at":"2022-06-07T00:51:38.000Z","dependencies_parsed_at":"2022-08-22T11:30:52.169Z","dependency_job_id":null,"html_url":"https://github.com/mishal/Jwt","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishal%2FJwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishal%2FJwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishal%2FJwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishal%2FJwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mishal","download_url":"https://codeload.github.com/mishal/Jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094988,"owners_count":21046770,"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":["json","jwt","php","security","webtoken"],"created_at":"2024-10-12T11:12:12.214Z","updated_at":"2026-03-09T10:03:55.750Z","avatar_url":"https://github.com/mishal.png","language":"PHP","readme":"# JWT – (JSON Web Token) for PHP\n\n[![Build Status](https://travis-ci.org/mishal/jwt.svg)](https://travis-ci.org/mishal/jwt)\n[![Downloads](https://img.shields.io/packagist/dm/mishal/jwt.svg)](https://packagist.org/packages/mishal/jwt)\n[![Latest release](https://img.shields.io/packagist/v/mishal/jwt.svg)](https://github.com/mishal/jwt/releases) \n\nA Php implementation of [JSON Web Token](https://tools.ietf.org/html/rfc7519).\n\n## Installing\n\n### Install with composer\n\n```\n$ composer require mishal/jwt\n```\n\n## Algorithms and Usage\n\nThe JWT spec supports `NONE`, `HMAC`, `RSASSA`, `ECDSA` and `RSASSA-PSS` algorithms for cryptographic signing.\n\nSupported algorithms:\n\n * None\n * HMAC 256\n * RSA 256\n\n### NONE - unsigned token\n\n    \u003c?php\n        \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\NoneAlgorithm;\n        \n    $token = Jwt::encode('string', new NoneAlgorithm());\n      \n    echo $token; // eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoic3RyaW5nIn0.\n\n### HMAC\n\n * HS256 - HMAC using SHA-256 hash algorithm\n\n#### HS256\n\n    \u003c?php\n    \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\HS256Algorithm;\n    \n    $token = Jwt::encode('string', $alg = new HS256Algorithm('secret'));\n    \n    echo $token; // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoic3RyaW5nIn0.RncJbCyf4zd0pu1N02u_rKwEezkmd94r3i5sWLk1ceU\n    \n    // decode, you must passed allowed algorithm(s) to prevent attackers to control the choice of algorithm\n    $decoded = Jwt::decode($token, ['algorithm' =\u003e $alg]);\n    \n    echo $decoded['data']; // 'string'\n\n### RSA\n\n * RS256 - RSA using SHA-256 hash algorithm\n\n#### RS256 - RSA using SHA-256 hash algorithm\n\n    \u003c?php\n    \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\RS256Algorithm;\n    \n    $privateKey = __DIR__ . '/key.pem';\n    $publicKey = __DIR__ . '/key.pub';\n    \n    $token = Jwt::encode('string', $alg = new RS256Algorithm($privateKey, $publicKey));\n    \n    echo $token; // eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoic3RyaW5nIn0.RncJbCyf4zd0pu1N02u_rKwEezkmd94r3i5sWLk1ceU\n    \n    // decode, you must passed allowed algorithm(s) to prevent attackers to control the choice of algorithm\n    $decoded = Jwt::decode($token, ['algorithm' =\u003e $alg]);\n    \n    echo $decoded['data']; // 'string'\n\n\n## Support for reserved claim names\n\nJSON Web Token defines some reserved claim names and defines how they should be\nused. 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\nThe `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    \u003c?php\n    \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\HS256Algorithm;\n    use Jwt\\Exception\\ExpiredException;\n    \n    $payload = [\n        Jwt::CLAIM_EXPIRATION =\u003e strtotime('1 day'),\n        'data' =\u003e 'my data'\n    ];\n    \n    $token = Jwt::encode($payload, $alg = new HS256Algorithm('secret'));\n    \n    try {\n        $token = Jwt::decode($token, ['algorithm' =\u003e $alg]);\n    } catch (ExpiredException $e) {\n        // Handle expired token, e.g. logout user or deny access\n    }\n\n#### Adding Leeway\n\n    \u003c?php\n    \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\HS256Algorithm;\n    use Jwt\\Exception\\ExpiredException;\n    \n    $leeway = 30; // 30 sec\n    \n    $payload = [\n        Jwt::CLAIM_EXPIRATION =\u003e strtotime('1 day'),\n        'data' =\u003e 'my data'\n    ];\n    \n    $token = Jwt::encode($payload, $alg = new HS256Algorithm('secret'));\n    \n    try {\n        $token = Jwt::decode($token, ['algorithm' =\u003e $alg, 'leeway' =\u003e $leeway]);\n    } catch (ExpiredException $e) {\n        // Handle expired token, e.g. logout user or deny access\n    }\n\n### Not Before Time Claim\n\nThe `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    \u003c?php\n\n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\HS256Algorithm;\n    use Jwt\\Exception\\BeforeValidException;\n    \n    $payload = [\n        Jwt::CLAIM_NOT_BEFORE =\u003e strtotime('1 day'),\n        'data' =\u003e 'my data'\n    ];\n    \n    $token = Jwt::encode($payload, $alg = new HS256Algorithm('secret'));\n    \n    try {\n        $token = Jwt::decode($token, ['algorithm' =\u003e $alg]);\n    } catch (BeforeValidException $e) {\n        // Handle invalid token, e.g. logout user or deny access\n    }\n\n### Issuer Claim\n\nThe `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#### Handle Issuer Claim\n\nSee the payload verification section.\n\n### Audience Claim\n\nThe `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#### Handle Audience Claim\n\nSee the payload verification section.\n\n### JWT ID Claim\n\nThe `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#### Handle JWT ID claim\n\nSee the payload verification section.\n\n### Issued At Claim\n\nThe `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#### Handle Issued At Claim\n\nSee the payload verification section.\n\n### Subject Claim\n\nThe `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#### Handle Subject Claim\n\nSee the payload verification section.\n\n## Payload verification\n\nPayload data can be verified when decoding the token using the `verify` option. \n`Expiration` and `Not Before` claims are **ALWAYS** checked. \n\n    \u003c?php\n    \n    use Jwt\\Jwt;\n    use Jwt\\Algorithm\\HS256Algorithm;\n    use Jwt\\Exception\\VerificationException;\n    \n    $payload = [\n        // expiration claim\n        Jwt::CLAIM_EXPIRATION =\u003e strtotime('1 day'),\n        // issuer claim\n        Jwt::CLAIM_ISSUER =\u003e 'my-web-app',\n        // custom claims\n        'user' =\u003e 'administrator'\n    ];\n    \n    $token = Jwt::encode($payload, $alg = new HS256Algorithm('secret'));\n    \n    // Decode with verification of the payload\n    // Expiration, and Not before claims are verified automatically\n    \n    // we will verify the token when decoding\n    $verify = [\n        Jwt::CLAIM_ISSUER =\u003e 'my-web-app',\n        // we can use closures for value verification\n        'user' =\u003e function ($value) {\n            if ($value === 'administrator') {\n                return true;\n            }\n            return false;\n        }\n    ];\n    \n    try {\n        $decoded = Jwt::decode($token, ['algorithm' =\u003e $alg, 'verify' =\u003e $verify]);\n    } catch (VerificationException $e) {\n        // something is wrong with the token\n        // do something!\n        switch ($e-\u003egetCode()) {\n            case VerificationException::CLAIM_IS_MISSING:\n                // claim is missing\n                break;\n            case VerificationException::CLAIM_VALUE_IS_INVALID:\n                // invalid claim value\n                break;\n        }\n    }\n    \n## Tests\n\nRun the tests using phpunit:\n\n    $ phpunit\n    \n## Credits\n\nJwt was inspired by jwt for ruby and firebase/jwt for php.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmishal%2Fjwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmishal%2Fjwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmishal%2Fjwt/lists"}