Laravel Passport Code Grant return User when login.

Barta Tamas
2018-12-04T10:03:00Z

AccessTokenController.php

| <?php
|
| namespace App\Http\Controllers;
|
|
| use Psr\Http\Message\ServerRequestInterface;
| use App\Http\Controllers\Traits\UserByAccessToken;
| use League\OAuth2\Server\Exception\OAuthServerException;
| use Illuminate\Database\Eloquent\ModelNotFoundException;
| use \Laravel\Passport\Http\Controllers\AccessTokenController as ATC;
|
|
| class AccessTokenController extends ATC
| {
|
| use UserByAccessToken;
|
| /**
| * @param ServerRequestInterface $request
| * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
| */
| public function issueToken(ServerRequestInterface $request)
| {
| try {
| $tokenResponse = parent::issueToken($request);
|
| $content = $tokenResponse->getContent();
|
| $data = json_decode($content, true);
|
| if (isset($data["error"])) {
| throw new OAuthServerException('The user credentials were incorrect.', 6, 'invalid_credentials', 401);
| }
|
| $userId = $this->updateSSOCache($data['access_token']);
|
| $data['user_id'] = $userId;
|
| return response()->json($data);
|
| } catch (ModelNotFoundException $e) {
| //return error message
| return response(["message" => "User not found"], 500);
| } catch (OAuthServerException $e) {
| //return error message
| return response(["message" => "The user credentials were incorrect.', 6, 'invalid_credentials"], 500);
| } catch (\Exception $e) {
| ////return error message
| return response(["message" => "Internal server error"], 500);
| }
| }
| }

UserByAccessToken.php

| <?php
|
| namespace App\Http\Controllers\Traits;
|
| use App\User;
| use Lcobucci\JWT\Parser;
| use Laravel\Passport\Token;
| use Illuminate\Support\Facades\Cache;
| use Laravel\Passport\TokenRepository;
|
|
| trait UserByAccessToken
| {
|
| /**
| * @param string $accessToken
| * @return \Laravel\Passport\Token
| */
| protected function getTokenByAccessToken(string $accessToken): Token
| {
| $tokenRepository = new TokenRepository();
| $jwt = (new Parser())->parse($accessToken);
|
| return $tokenRepository->find($jwt->getClaim('jti'));
| }
|
| /**
| * @param string $accessToken
| * @return User
| * @throws \Exception
| */
| protected function getTokenUser(string $accessToken): User
| {
| $token = $this->getTokenByAccessToken($accessToken);
|
| if (!$token) throw new \Exception('Access Token not set');
|
| return User::findOrFail($token->user_id);
| }
|
| /**
| * @param string $accessToken
| * @return int
| * @throws \Exception
| */
| protected function updateSSOCache(string $accessToken): int
| {
| $user = $this->getTokenUser($accessToken);
| if (!$user) {
| throw new \Exception('No User found for Access Token');
| }
|
| $this->alterCache($user->user_id, $accessToken);
|
| return $user->user_id;
| }
|
| /**
| * @param int $userId
| * @param string $accessToken
| * @return Void
| */
| protected function alterCache(int $userId, string $accessToken): Void
| {
| $userCacheKey = 'user_tokens_' . $userId;
|
| $defaultPrefix = Cache::getPrefix();
| Cache::setPrefix('sso_auth:');
| $cachedData = Cache::get($userCacheKey);
|
| if ($cachedData) {
| $cachedData[] = $accessToken;
| } else {
| $cachedData = [$accessToken];
| }
|
| Cache::add($userCacheKey, $cachedData, 10);
|
| Cache::setPrefix($defaultPrefix);
| }
| }