https://github.com/corviz/jwt
Jwt library for PHP
https://github.com/corviz/jwt
authentication jwt php
Last synced: 4 months ago
JSON representation
Jwt library for PHP
- Host: GitHub
- URL: https://github.com/corviz/jwt
- Owner: Corviz
- License: mit
- Created: 2022-03-01T16:38:14.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-22T17:27:07.000Z (over 4 years ago)
- Last Synced: 2025-06-05T10:05:23.747Z (about 1 year ago)
- Topics: authentication, jwt, php
- Language: PHP
- Homepage: https://corviz.github.io/jwt/
- Size: 81.1 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JWT

## How to install
```
composer require corviz/jwt
```
## Provided signers
Algorithm
Version
HS256
1.0
HS384
1.0
HS512
1.0
## Provided claim validators
Claim
Version
exp
1.0
nbf
1.0
## Basic Usage
### Generating token
```php
with('exp', strtotime('+ 1 hour')) //Expires in one hour
->withSigner(SignerFactory::build('HS256')) //HS256 signer is provided by default. This could be omitted
->sign($mySecret)
->toString();
```
### Validating and reading values from a token
```php
validate($mySecret);
if ($isValid) {
$payload = $token->getPayload();
$headers = $token->getHeaders();
}
```
### Validating your private claims
First you have to create your validator
```php
use \Corviz\Jwt\Validator\Validator;
class MyClaimValidator extends Validator {
/**
* @return string
*/
public function validates() : string
{
return 'my-claim'; //this will validate value inside 'my-claim', when set
}
/**
* @param mixed $value
* @return bool
*/
public function validate(mixed $value) : bool
{
// this claim must contain value 'a', 'b' or 'c'
$valid = in_array($value, ['a', 'b', 'c']);
return $valid;
}
}
```
Then all you have to do is assign your validator before running *validate()* method
```php
assignValidator(new MyClaimValidator());
$isValid = $token->validate($mySecret);
if ($isValid) {
$myClaim = $token->getPayload('my-claim');
}
```