PHP code example of iwouldrathercode / php-custom-saml
1. Go to this page and download the library: Download iwouldrathercode/php-custom-saml library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iwouldrathercode / php-custom-saml example snippets
$metadata['http://laravel_url/myidp1/metadata'] = array(
'AssertionConsumerService' => 'http://laravel_url/myidp1/acs',
'SingleLogoutService' => 'http://laravel_url/myidp1/sls',
//the following two affect what the $Saml2user->getUserId() will return'NameIDFormat' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
'simplesaml.nameidattribute' => 'uid'
);
publicfunctionhandle($request, Closure $next){
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'
}
else
{
$saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1'));
return $saml2Auth->login(URL::full());
}
}
return $next($request);
}
protectedfunctionunauthenticated($request, AuthenticationException $exception){
if ($request->expectsJson())
{
return response()->json(['error' => 'Unauthenticated.'], 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'
}
$saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1'));
return $saml2Auth->login('/my/redirect/path');
}
Event::listen('Iwouldrathercode\Saml2\Events\Saml2LoginEvent', function(Saml2LoginEvent $event){
$messageId = $event->getSaml2Auth()->getLastMessageId();
// Add 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);
});