Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

oauth 2.0 - Customising token response Laravel 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?.

I would like this to be returned:

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Tested on Laravel 5.7.

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

namespace AppAuth;

use LeagueOAuth2ServerEntitiesAccessTokenEntityInterface;

class BearerTokenResponse extends LeagueOAuth2ServerResponseTypesBearerTokenResponse
{
    /**
     * 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. Create your own PassportServiceProvider class and override the makeAuthorizationServer() method in order to pass in your own BearerTokenResponse class.

namespace AppProviders;

use AppAuthBearerTokenResponse;
use LaravelPassportBridge;
use LeagueOAuth2ServerAuthorizationServer;

class PassportServiceProvider extends LaravelPassportPassportServiceProvider
{
    /**
     * Make the authorization service instance.
     *
     * @return LeagueOAuth2ServerAuthorizationServer
     */
    public function makeAuthorizationServer()
    {
        return new AuthorizationServer(
            $this->app->make(BridgeClientRepository::class),
            $this->app->make(BridgeAccessTokenRepository::class),
            $this->app->make(BridgeScopeRepository::class),
            $this->makeCryptKey('private'),
            app('encrypter')->getKey(),
            new BearerTokenResponse() // <-- The class you created above
        );
    }
}

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

    /*
     * Application Service Providers...
     */
    AppProvidersPassportServiceProvider::class,

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

This stops the default PassportServiceProvider class from being loaded.

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

Then run composer install.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...