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

Error handling in event listener #177

Open neomaximus2k opened 5 years ago

neomaximus2k commented 5 years ago

In the documentation you have the following line //if it does not exist create it and go on or show an error message

I need to "show an error message" in my system as SSO is using ADFS email to then re-match the user, this doesn't work if their username and email don't match and I need to show an error message asking them to contact the IT support team.

I've attempted to return a view (which I know you shouldn't do) in the event along with a few other options but to no avail. How can I achieve this?

` $Auth = $event->getSaml2Auth(); $User = $event->getSaml2User(); $messageId = $Auth->getLastMessageId();

    if (!$Auth->isAuthenticated()){
        Log::error("SSO User unable to login.");
        Log::error($messageId . " - " . $Auth->getLastErrorReason());
        // something went wrong, return back the error view
        return view('errors.ssoerror')->with(['MessageID' => $messageId, 'AuthOBJ' => $Auth]);
    } else {
                // continue the logon process and update the users groups etc

`

imacrayon commented 5 years ago

I was able to display a view by throwing an Exception inside the login event.

// app/Exceptions/SsoAuthenticationException.php

namespace App\Exceptions;

use Illuminate\Auth\AuthenticationException;

class SsoAuthenticationException extends AuthenticationException
{
}
// app/Exceptions/Handler.php

...

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($exception instanceof SsoAuthenticationException) {
        return redirect()->route('sso-error');
    }

    return parent::unauthenticated($request, $exception);
}

...

Throwing an exception in the event will now redirect to a route named sso-error.

throw new SsoAuthenticationException('User not found for ID: '.$user->getUserId())

This approach is kind of awkward, but it works.

jamesratcliffe commented 3 years ago

@imacrayon Thanks. I've seen awkwarder ;).

It would be good to have more information about this in the Readme.