jonnybarnes.uk/app/Services/TokenService.php

33 lines
787 B
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
declare(strict_types=1);
2016-05-19 15:01:28 +01:00
namespace App\Services;
use App\Jobs\AddClientToDatabase;
use DateTimeImmutable;
2022-07-09 10:08:26 +01:00
use Lcobucci\JWT\Configuration;
2016-05-19 15:01:28 +01:00
class TokenService
{
/**
* Generate a JWT token.
*/
public function getNewToken(array $data): string
{
$config = resolve(Configuration::class);
$token = $config->builder()
->issuedAt(new DateTimeImmutable)
->withClaim('client_id', $data['client_id'])
->withClaim('me', $data['me'])
->withClaim('scope', $data['scope'])
->withClaim('nonce', bin2hex(random_bytes(8)))
->getToken($config->signer(), $config->signingKey());
dispatch(new AddClientToDatabase($data['client_id']));
2016-05-19 15:01:28 +01:00
return $token->toString();
2016-05-19 15:01:28 +01:00
}
}