aacotroneo / laravel-saml2

A Laravel 5 package for Saml2 integration as a SP (service provider) based on the simple OneLogin toolkit
MIT License
567 stars 238 forks source link

How to conditionally redirect users on login #190

Closed r0ulito closed 4 years ago

r0ulito commented 5 years ago

Hello,

I have a specific case in my laravel app. Let me explain : An other app on an other domain can trigger a new browser tab to a specific page called "duplicate". In that case the tab is open with getParams. Like "myapp.com/duplicate?param=value&marap=eulav"

The point is: When user is not logged in yet, he's redirected to the authentication portal (on an other domain too), and then comes back with "/duplicate" without any params.

I don't know how to handle it

albertStaalburger commented 4 years ago

Hi @r0ulito, if I understand you correctly you want the user to be redirected to a specific route once they have authenticated with your parameters in tact? If so, there are 2 things that you will need to do: 1) Extend the Aacotroneo\Saml2\Http\Controllers\Saml2Controller so that you can overwrite the default login function 2) You need to make use of Laravel Redirect::intended() in the new login function in order to ensure that you get redirected to the url+params once you have been authenticated.

Here is a basic example of how to do it:

public function login ()
{
    $loginRoute = session()->pull('url.intended', config('saml2_settings.loginRoute'));
    // If user isn't authenticated, auth them then redirect to where they wanted to go, else just redirect
    return Auth::guest() ? $this->saml2Auth->login($loginRoute) : Redirect::intended();
}
r0ulito commented 4 years ago

Seems like I found a solution which is working well.

In handler.php I simply added this to the un authenticated method

$redirectUrl = $request->getRequestUri(); Return Saml2Auth::login($redirectUrl);