Skip to content

Instantly share code, notes, and snippets.

@Ryomasao
Last active July 6, 2020 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryomasao/f35a39216ad871331e85d8932478f3be to your computer and use it in GitHub Desktop.
Save Ryomasao/f35a39216ad871331e85d8932478f3be to your computer and use it in GitHub Desktop.
jwt

jwt

https://jwt.io/introduction/

基本

  • Header
  • Payload
  • Signature
<?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'));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment