Last active
May 3, 2020 11:57
-
-
Save einnar82/e365e6cf4331b0f71233097bf09ec3bb to your computer and use it in GitHub Desktop.
Revoke access token manually in Laravel Passport (Laravel 6.x)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Support\Facades\Request as RequestFacade; | |
use Illuminate\Http\Request; | |
use Laravel\Passport\Client; | |
use Laravel\Passport\Token; | |
use Lcobucci\JWT\Parser; | |
class OAuthClient extends Client | |
{ | |
public static function revokeManually(?Request $request = null) | |
{ | |
$bearerToken = $request !== null ? $request->bearerToken() : RequestFacade::bearerToken(); | |
$parsedJwt = (new Parser())->parse($bearerToken); | |
if ($parsedJwt->hasHeader('jti')) { | |
$tokenId = $parsedJwt->getHeader('jti'); | |
} elseif ($parsedJwt->hasClaim('jti')) { | |
$tokenId = $parsedJwt->getClaim('jti'); | |
} else { | |
Log::error('Invalid JWT token, Unable to find JTI header'); | |
return null; | |
} | |
if ($token = Token::findOrFail($tokenId)) { | |
$token->revoke(); | |
} | |
# Then call it via, OAuthClient::revokeManually(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment