1. Go to this page and download the library: Download rafalswierczek/jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rafalswierczek / jwt example snippets
// Issuer example:
namespace YourApp\AuthServer\Infrastructure;
use YourApp\AuthServer\Application\JWSIssuer;
use YourApp\AuthServer\Domain\Header;
use YourApp\AuthServer\Domain\Payload;
use YourApp\AuthServer\Domain\User;
use rafalswierczek\JWT\JWS\Enum\Header\AlgorithmType;
use rafalswierczek\JWT\JWS\Enum\Header\TokenType;
use rafalswierczek\JWT\JWS\Issuer\JWSIssuerInterface;
use rafalswierczek\JWT\JWS\Model\JWSHeader;
use rafalswierczek\JWT\JWS\Model\JWSPayload;
use rafalswierczek\Uuid4\Uuid4Factory;
final class RafalswierczekJWSIssuer implements JWSIssuer
{
public function __construct(private JWSIssuerInterface $issuer)
{
}
public function issueToken(User $user): string
{
$header = Header::create();
$payload = Payload::create(
jwtId: Uuid4Factory::create()->toHex(),
userId: $user->id,
);
return $this->issuer->generateCompactJWS(
header: $this->mapHeader($header),
payload: $this->mapPayload($payload, $user),
secret: $_ENV['JWS_PRIVATE_KEY'],
);
}
private function mapHeader(Header $header): JWSHeader
{
return new JWSHeader(
tokenType: TokenType::from($header->typ),
algorithmType: AlgorithmType::from($header->alg),
);
}
private function mapPayload(Payload $payload, User $user): JWSPayload
{
return new JWSPayload(
id: $payload->jti, // globally unique JWT id
issuer: $payload->iss, // domain of auth server (might be the same as audience element)
subject: $payload->sub, // user-uuid
issuedAt: (new \DateTimeImmutable())->setTimestamp($payload->iat),
expirationTime: (new \DateTimeImmutable())->setTimestamp($payload->exp),
audience: $payload->aud, // ['yourdomain1.com', 'yourdomain2.com']
data: ['user' => $user], // user metadata known to all audience applications
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.