2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\models\authentication;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
use DateTimeImmutable;
|
|
|
|
use Lcobucci\JWT\UnencryptedToken;
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final readonly class AuthenticationResult {
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
private UnencryptedToken $token,
|
|
|
|
private ?string $refreshToken = null,
|
|
|
|
) {
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getToken(): UnencryptedToken {
|
2019-08-01 14:47:12 +05:30
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRefreshToken(): ?string {
|
|
|
|
return $this->refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function formatAsOAuth2Response(): array {
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var DateTimeImmutable $expiresAt */
|
|
|
|
$expiresAt = $this->token->claims()->get('exp');
|
2019-08-01 14:47:12 +05:30
|
|
|
$response = [
|
2024-12-02 15:40:55 +05:30
|
|
|
'access_token' => $this->token->toString(),
|
|
|
|
'expires_in' => $expiresAt->getTimestamp() - (new DateTimeImmutable())->getTimestamp(),
|
2019-08-01 14:47:12 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
$refreshToken = $this->refreshToken;
|
|
|
|
if ($refreshToken !== null) {
|
|
|
|
$response['refresh_token'] = $refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|