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
437 views
in Technique[技术] by (71.8m points)

php - Extending Laravel 5.2 SessionGuard

I want to extend the Laravel stock authentication to use an OAuth server for user retrieval and authentication while taking advantage of the existing functionality. I already managed to extend the EloquentUserProvider to partially overwrite/extend the implementations of the IlluminateContractsAuthUserProvider contract. The current implementation looks like this:

class EloquentOauthServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return HcOAuthProvider;
     */
    public function boot()
    {
        Auth::provider('oauth',function($app){
            $model = $app['config']['auth.providers.oauth.model'];
            $repository = new OauthUserRepository();
            return new EloquentOauthUserProvider($app['hash'], $model, $repository);
        });
    }

}

In the auth.php config I changed the descriptors like this:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'oauth',
    ],
]

'providers' => [
    'oauth' => [
        'driver' => 'oauth',
        'model' => AppModelsUser::class,
    ],
]

This is working so far, I can overwrite methods to add my logic. But I realized that I also need to overwrite some methods of the SessionGuard class (login and logout to be specific) since I want to save and retrieve OAuth specific tokens using the Laravel session implementation. There are a couple of suggestions around but they are either not working (Maybe they worked before Laravel 5.2) or would require to overwrite the Authmanager which looks like overkill.

So my question is: What I have to do in Laravel 5.2 to overwrite the SessionGuard?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Recently I've had the same problem, so maybe the solution is something like this.

<?php
namespace AppCoreExtensions;
use IlluminateAuthSessionGuard;
use IlluminateContractsAuthAuthenticatable;
class SessionGuardExtended extends SessionGuard
{
    /**
     * Log a user into the application.
     *
     * @param  IlluminateContractsAuthAuthenticatable  $user
     * @param  bool  $remember
     * @return void
     */
    public function login(Authenticatable $user, $remember = false)
    {
        $this->updateSession($user->getAuthIdentifier());
        if ($remember) {
            $this->refreshRememberToken($user);
            $this->queueRecallerCookie($user);
        }
        $this->fireLoginEvent($user, $remember);
        $this->setUser($user);
    }
}

In AppServiceProvider.php

public function boot()
{
    Auth::extend(
        'sessionExtended',
        function ($app) {
            $provider = new EloquentUserProvider($app['hash'], config('auth.providers.users.model'));
            return new SessionGuardExtended('sessionExtended', $provider, app()->make('session.store'), request());
        }
    );
}

In Config/auth.php driver should be renamed

'web' => [
    'driver' => 'sessionExtended',
    'provider' => 'users',
],

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

...