2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
2017-03-01 17:37:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
namespace App\Services;
|
|
|
|
|
2017-06-22 15:41:23 +01:00
|
|
|
use App\Jobs\AddClientToDatabase;
|
2020-11-28 18:21:29 +00:00
|
|
|
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
|
|
|
|
{
|
2020-11-28 18:21:29 +00:00
|
|
|
$config = resolve(Configuration::class);
|
|
|
|
|
|
|
|
$token = $config->builder()
|
2024-10-25 20:40:52 +01:00
|
|
|
->issuedAt(new DateTimeImmutable)
|
2020-11-28 18:21:29 +00:00
|
|
|
->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());
|
|
|
|
|
2017-06-22 15:41:23 +01:00
|
|
|
dispatch(new AddClientToDatabase($data['client_id']));
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2020-11-28 18:21:29 +00:00
|
|
|
return $token->toString();
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
}
|