PHP code example of rootinc / laravel-saml2-middleware

1. Go to this page and download the library: Download rootinc/laravel-saml2-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rootinc / laravel-saml2-middleware example snippets


Route::get('/login/saml2', '\RootInc\LaravelSaml2Middleware\Saml2@saml2');
Route::post('/login/saml2callback', '\RootInc\LaravelSaml2Middleware\Saml2@saml2callback');



namespace App\Http\Middleware;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

use Auth;

use App\User;

class AppSaml2 extends Saml2
{
    protected function success($request, $token, $profile)
    {
        $email = mb_strtolower($profile['Email'][0]);

        $user = User::updateOrCreate(['email' => $email], [
            'firstName' => $profile['FirstName'][0],
            'lastName' => $profile['LastName'][0],
        ]);

        Auth::login($user, true);

        return parent::success($request, $token, $profile);
    }
}

Route::get('/login/saml2', '\App\Http\Middleware\AppSaml2@saml2');
Route::post('/login/saml2callback', '\App\Http\Middleware\AppSaml2@saml2callback');
Route::get('/logout/saml2', '\App\Http\Middleware\AppSaml2@saml2logout');
Route::post('/logout/logoutcallback', '\App\Http\Middleware\AppSaml2@logoutcallback');



namespace App\Http\Middleware;

use Closure;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

use Auth;
use Carbon\Carbon;

use App\User;

class AppSaml2 extends Saml2
{
    protected function handlecallback($request, Closure $next, $token)
    {
        $user = Auth::user();

        $user->updated_at = Carbon::now();

        $user->save();

        return parent::handlecallback($request, $next, $token);
    }
}



namespace App\Http\Middleware;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

use Auth;

class AppSaml2 extends Saml2
{
    protected function redirect($request)
    {
        if (Auth::user() !== null)
        {
            return $this->saml2($request);
        }
        else
        {
            return parent::redirect($request);
        }
    }
}



namespace App\Http\Middleware;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

class AppSaml2 extends Saml2
{
    protected $login_route = "/";
}



namespace App\Http\Middleware;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

use Auth;

class AppSaml2 extends Saml2
{
    //we could overload this if we wanted too.
    public function getSaml2Url($email = null)
    {
        return $this->getAuth()->login(null, [], false, false, true, true, $email);
    }

    public function saml2(Request $request)
    {
        $user = Auth::user();

        $away = $this->getSaml2Url($user ? $user->email : null);

        return redirect()->away($away);
    }
}



namespace App\Http\Middleware;

use RootInc\LaravelSaml2Middleware\Saml2 as Saml2;

use Auth;

class AppSaml2 extends Saml2
{
    //this is the default behavior
    //overwrite to meet your needs
    protected function handleTesting(Request $request, Closure $next)
    {
        $user = Auth::user();

        if (!isset($user))
        {
            return $this->redirect($request, $next);
        }

        return $this->handlecallback($request, $next, null);
    }
}