fgsl/jwt

Fgsl JWT Handler

Installs: 118

Dependents: 3

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 0

Type:component

1.2.1 2024-10-22 10:42 UTC

This package is auto-updated.

Last update: 2024-10-22 10:43:39 UTC


README

For creating a JWT token, you must before create an instance of Fgsl\Jwt\Jwt. After, you call method getBearerToken. This method will store the username in the token as the sub attribute. Additional attributes can be sent in an array as the third argument.

For recovering the payload from a given token, call the static method getPayload.

You can see a use example below. You can run this test from JwtTest class using PHPUnit.

    public function testBearerToken()
    {
        $jwt = new Jwt(['RS256','sha256'], 'JWT', 'newbraveworld.com', 'PT2H');
        
        $bearerToken = $jwt->getBearerToken('foouser','baapassword',['role' => 'admin']);
        
        $this->assertTrue(is_string($bearerToken));
        $this->assertNotEmpty($bearerToken);
        
        $payload = Jwt::getPayload($bearerToken);
   
        $this->assertTrue(is_object($payload));
        
        $this->assertEquals('foouser', $payload->sub);
        $this->assertEquals('newbraveworld.com', $payload->iss);
        $this->assertEquals('admin',$payload->role);
        
        $this->assertFalse(Jwt::expired($bearerToken));
    }