超载laravel护照授予密码访问令牌

时间:2018-05-14 15:26:47

标签: laravel token laravel-passport

是否可以重载laravel护照授予密码令牌有效负载?

我的意图是将一些其他数据嵌入到生成的令牌哈希中,如用户名,电子邮件等

2 个答案:

答案 0 :(得分:0)

您可以像在这里一样从您的控制器内部自己生成令牌:

<?php

namespace MyApp\Http\Controllers\API;

use Illuminate\Http\Request;
use Laravel\Passport\Http\Controllers\ConvertsPsrResponses;
use League\OAuth2\Server\AuthorizationServer;
use MyApp\Http\Controllers\APIController;
use Illuminate\Auth\AuthenticationException;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Response as Psr7Response;

class LoginController extends APIController
{

    use ConvertsPsrResponses;

    /**
     *
     * @param Request $request
     * @param AuthorizationServer $authServer
     * @return \Illuminate\Http\JsonResponse
     * @throws AuthenticationException
     * @throws \League\OAuth2\Server\Exception\OAuthServerException
     */
    public function login(Request $request, AuthorizationServer $authServer)
    {
        $token = $this->getPasswordToken($request, $authServer);
        $data = [
            "token_details" => $token,
        ];

        return $this->successResponse(
            'Successful Login',
            200,
            $data
        );

    }


    /**
     * @param $request
     * @param AuthorizationServer $authServer
     * @return mixed
     * @throws \League\OAuth2\Server\Exception\OAuthServerException
     */
    private function getPasswordToken($request, AuthorizationServer $authServer)
    {
        $parsedBody = [
            'grant_type' => 'password',
            'client_id' => config('app.client_id'),
            'client_secret' => config('app.client_secret'),
            'username' => $request->username,
            'password' => $request->password,
            'scope' => '',
        ];

        $serverRequest = new ServerRequest(
            $request->server(),
            [],
            null,
            $request->method(),
            'php://input',
            $request->header(),
            [],
            [],
            $parsedBody
        );

        $response = $this->convertResponse(
            $authServer->respondToAccessTokenRequest($serverRequest, new Psr7Response)
        );
        return json_decode($response->getContent());
    }

}

答案 1 :(得分:-1)

是的,这是可能的。请看一下这个包

https://github.com/benbjurstrom/passport-custom-jwt-claims