自定义令牌响应Laravel Passport [英] Customising token response Laravel Passport

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

问题描述

我目前正在使用API​​,但遇到了麻烦.我正在使用具有密码"授予类型的Passport.

I am working on an API at the moment and have hit a brick wall. I am using Passport with the 'Password' grant type.

我想返回带有访问令牌的用户信息,但是,我不确定该怎么做.

I want to return the user information with the access tokens, however, I am not sure how to.

我可以实现,编辑或扩展哪个类?

Which class could I implement, edit or extend to get this?.

我希望将其退回:

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "lalalalalal",
    "refresh_token": "lalalallala",
    "user": {
        "username": "a username",
        "user_type": "admin"
    }
}

谢谢.

推荐答案

The instructions on how to do this are hinted in the BearerTokenResponse class (part of the league/oauth2-server package).

在Laravel 5.7上进行了测试.

Tested on Laravel 5.7.

1.扩展BearerTokenResponse类,在响应中添加所需的额外参数.

1. Extend the BearerTokenResponse class, add the extra params you need in the response.

namespace App\Auth;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;

class BearerTokenResponse extends \League\OAuth2\Server\ResponseTypes\BearerTokenResponse
{
    /**
     * Add custom fields to your Bearer Token response here, then override
     * AuthorizationServer::getResponseType() to pull in your version of
     * this class rather than the default.
     *
     * @param AccessTokenEntityInterface $accessToken
     *
     * @return array
     */
    protected function getExtraParams(AccessTokenEntityInterface $accessToken): array
    {
        return [
            'user_id' => $this->accessToken->getUserIdentifier(),
        ];
    }
}

2.创建您自己的PassportServiceProvider类并覆盖makeAuthorizationServer()方法,以便传递您自己的BearerTokenResponse类.

2. Create your own PassportServiceProvider class and override the makeAuthorizationServer() method in order to pass in your own BearerTokenResponse class.

namespace App\Providers;

use App\Auth\BearerTokenResponse;
use Laravel\Passport\Bridge;
use League\OAuth2\Server\AuthorizationServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider
{
    /**
     * Make the authorization service instance.
     *
     * @return \League\OAuth2\Server\AuthorizationServer
     */
    public function makeAuthorizationServer()
    {
        return new AuthorizationServer(
            $this->app->make(Bridge\ClientRepository::class),
            $this->app->make(Bridge\AccessTokenRepository::class),
            $this->app->make(Bridge\ScopeRepository::class),
            $this->makeCryptKey('private'),
            app('encrypter')->getKey(),
            new BearerTokenResponse() // <-- The class you created above
        );
    }
}

3.将您的提供者添加到config/app.php

3. Add your provider to the providers array in config/app.php

    /*
     * Application Service Providers...
     */
    App\Providers\PassportServiceProvider::class,

4.从composer.json

4. Exclude the passport package from laravel auto-discovery in composer.json

这将阻止加载默认的PassportServiceProvider类.

This stops the default PassportServiceProvider class from being loaded.

    "extra": {
        "laravel": {
            "dont-discover": [
                "laravel/passport"
            ]
        }
    },

然后运行composer install.

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

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