<?php
namespace Tests\Unit;
use Tests\TestCase;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Hmac\Sha256;
class Jwt2Test extends TestCase
{
public function test_jwt()
{
$data = [
'name' => 'tarou',
'age' => 12,
'detail' => [
'address' => '1111111'
]
];
$signer = new Sha256();
$token = (new Builder())
//->issuedBy('http://example.com') // Configures the issuer (iss claim)
//->permittedFor('http://example.org') // Configures the audience (aud claim)
//->identifiedBy('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
//->issuedAt($time) // Configures the time that the token was issue (iat claim)
//->canOnlyBeUsedAfter($time + 60) // Configures the time that the token can be used (nbf claim)
//->expiresAt($time + 3600) // Configures the expiration time of the token (exp claim)
->withClaim('info', $data) // Configures a new claim, called "uid"
->getToken($signer, new Key('testing')); // Retrieves the generated token
//->getToken(); // Retrieves the generated token
echo($token . "\n");
$ts = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbmZvIjp7Im5hbWUiOiJ0YXJvdSIsImFnZSI6MTIsImRldGFpbCI6eyJhZGRyZXNzIjoiMTExMTExMSJ9fX1.-UJiuqas_GNAsGZY1C1Yfor9cgzVtKTvSkJahH11GC0';
$token = (new Parser())->parse((string) $ts); // Parses from a string
$header = $token->getHeaders(); // Retrieves the token header
$claim = $token->getClaims(); // Retrieves the token claims
$this->assertTrue($token->verify($signer, 'testing'));
}
}