mirror of https://github.com/pixelfed/pixelfed
Fix oauth/token
parent
c857a3d6e3
commit
ef803ae9b6
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\OAuth;
|
||||||
|
|
||||||
|
use Laravel\Passport\Http\Controllers\AccessTokenController;
|
||||||
|
use Laravel\Passport\Token;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class ApiTokenController extends AccessTokenController
|
||||||
|
{
|
||||||
|
public function issueToken(ServerRequestInterface $psrRequest, ResponseInterface $psrResponse): Response
|
||||||
|
{
|
||||||
|
$response = parent::issueToken($psrRequest, $psrResponse);
|
||||||
|
|
||||||
|
$data = json_decode($response->getContent(), true);
|
||||||
|
|
||||||
|
if (isset($data['access_token'])) {
|
||||||
|
$tokenId = $this->getTokenIdFromJwt($data['access_token']);
|
||||||
|
|
||||||
|
if ($tokenId) {
|
||||||
|
$token = Token::find($tokenId);
|
||||||
|
|
||||||
|
if ($token) {
|
||||||
|
$data['created_at'] = $token->created_at->toIso8601String();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($data, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTokenIdFromJwt(string $jwt): ?string
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$parts = explode('.', $jwt);
|
||||||
|
if (count($parts) !== 3) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true);
|
||||||
|
|
||||||
|
return $payload['jti'] ?? null;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Laravel\Passport\Token as PassportToken;
|
||||||
|
|
||||||
|
class OAuthToken extends PassportToken
|
||||||
|
{
|
||||||
|
protected $visible = [
|
||||||
|
'id',
|
||||||
|
'user_id',
|
||||||
|
'client_id',
|
||||||
|
'name',
|
||||||
|
'scopes',
|
||||||
|
'revoked',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
'expires_at',
|
||||||
|
];
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue