Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SupunSam's avatar

Connect Laravel with SAML IDP

Hi All,

I have created an App using the default laravel Auth. However now I need to replace that with an SSO service.

How can I do this? I've already looked into https://github.com/aacotroneo/laravel-saml2.

But as per my understanding, it will help you turn your app into a service provider. I just need to replace my default authentication system with this SAML IDP.

Can anyone help me with this?

0 likes
3 replies
skauk's avatar

@supunsam You can use this package to connect with SAML IDP as well. Although, I've used previous version (1.0.0) which didn't support multiple providers. Basically, you need to add your IDP data in config/saml2_config.php under idp key (entity ID, sign-in URL, certificate). Then you need to create a listener for Aacotroneo\Saml2\Events\Saml2LoginEvent event which gonna receive data from your provider. Then you need to use this data to retrieve a user (for instance, an e-mail or user ID) and login that user using. Here's an example from the older version's Readme:

Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {
            $messageId = $event->getSaml2Auth()->getLastMessageId();
            // your own code preventing reuse of a $messageId to stop replay attacks
            $user = $event->getSaml2User();
            $userData = [
                'id' => $user->getUserId(),
                'attributes' => $user->getAttributes(),
                'assertion' => $user->getRawSamlAssertion()
            ];
             $laravelUser = //find user by ID or attribute
             //if it does not exist create it and go on  or show an error message
             Auth::login($laravelUser);
        });

Hope it helps!

1 like
SupunSam's avatar

Hi @skauk. Thank you very much for this information. I was clueless even how to approach this. But I will definitely look into this and keep you posted if it works for me.

skauk's avatar

@supunsam If you find this helpful, feel free to mark as the best reply! ;) My use case was connecting with G Suite and while seemed confusing at first it ended up to be not that difficult.

Please or to participate in this conversation.