自定义Laravel护照BearerTokenResponse [英] Custom Laravel Passport BearerTokenResponse

查看:220
本文介绍了自定义Laravel护照BearerTokenResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用Laravel Passport(将league/oauth2-server用于服务器实现)安装了Laravel.授予oauth2令牌后,我想返回用户ID,以便可以在我的EmberJS应用中使用它来标识经过身份验证的用户.

Currently I have a Laravel installation using Laravel Passport (which uses league/oauth2-server for the server implementation). I would like to return the user id when a oauth2 token is granted, so I can use it to identify the authenticated user in my EmberJS app.

建议的方法是:

创建我自己的课程:

use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;

class UserIdBearerTokenResponse extends BearerTokenResponse
{
    protected function getExtraParams(AccessTokenEntityInterface $accessToken)
    {
        return [
            'user_id' => $this->accessToken->getUserIdentifier()
        ];
    }
}

vendor/league/oauth2-server/src

Modifying AuthorizationServer.getResponseType() in vendor/league/oauth2-server/src

protected function getResponseType()
{
    if ($this->responseType instanceof ResponseTypeInterface === false) {
        // Return my own class instead of provided one
        $this->responseType = new UserIdBearerTokenResponse();
    }

    $this->responseType->setPrivateKey($this->privateKey);

    return $this->responseType;
}

但是这种方法需要我将vendor/league/oauth2-server/src/AuthorizationServer.php文件添加到我的git repo中.

But this approach requires me to add the vendor/league/oauth2-server/src/AuthorizationServer.php file to my git repo.

对我来说,这似乎很混乱而且不可靠.有没有更好/更清洁的方法来实现这一目标?

This seems very messy and unreliable to me. Is there a better/cleaner way to achieve this?

推荐答案

要使用您的自定义响应,您可以添加一个自定义授权服务器,如下所示:

To use your custom response, you can add a custom authorization server like this:

<?php

namespace App;

use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;

class TokenServer extends AuthorizationServer
{
    /**
     * Get the token type that grants will return in the HTTP response.
     *
     * @return ResponseTypeInterface
     */
    protected function getResponseType()
    {
        if ($this->responseType instanceof ResponseTypeInterface === false) {
            $this->responseType = new UserIdBearerTokenResponse();
        }

        $this->responseType->setPrivateKey($this->privateKey);

        return $this->responseType;
    }
}

和这样的自定义PassportServiceProvider:

And a custom PassportServiceProvider like this:

<?php

namespace App\Providers;

use App\TokenServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider
{

    /**
     * Make the authorization service instance.
     *
     * @return AuthorizationServer
     */
    public function makeAuthorizationServer()
    {
        return new TokenServer(
            $this->app->make(\Laravel\Passport\Bridge\ClientRepository::class),
            $this->app->make(\Laravel\Passport\Bridge\AccessTokenRepository::class),
            $this->app->make(\Laravel\Passport\Bridge\ScopeRepository::class),
            'file://'.storage_path('oauth-private.key'),
            'file://'.storage_path('oauth-public.key')
        );
    }

}

然后在config/app.php文件中进行以下更改:

And then make the following change in your config/app.php file:

/*
 * Package Service Providers...
 * We extend the packaged PassportServiceProvider with our own customization
 */

// Laravel\Passport\PassportServiceProvider::class,
App\Providers\PassportServiceProvider::class,

这篇关于自定义Laravel护照BearerTokenResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆