SAML Service Provider(SP) for Yii2 using simplesamlphp (yii2 extension). This is useful when you want to act as a Service Provider(SP), where a partner company is acting as a SAML Identity Provider (IDP). User data and their credentials are with the IDP and you are using SAML based login and use those users information in your yii2 application.
This is a very basic implementation where we are not persisting user data coming from the IDP at the Service Provider side for reporting on usage etc. However, those are fairly straightforward additions one can do.
This is available to be installed via composer. In your yii2 application folder, run the following command.
composer require lucidprogrammer/yii2-simplesamlphp
This will install the package as a yii2 extension and update the extensions and installs the simplesamlphp project too in the vendor folder.
In a standard web server configuration like Apache, the DocumentRoot will be pointing to the yii2 application's web folder. Create an alias for /saml pointing to pathto_my-yii2-app/vendor/simplesamlphp/simplesamlphp/www.
In the case of Apache, you may do something like (replace pathto_my-yii2-app with your real path.)
echo "Alias /saml pathto_my-yii2-app/vendor/simplesamlphp/simplesamlphp/www" >> /etc/apache2/apache2.conf
echo "<Directory \"pathto_my-yii2-app/simplesamlphp/simplesamlphp/www/\"> \n Options Indexes FollowSymLinks \n AllowOverride all \n Require all granted \n </Directory>" >> /etc/apache2/apache2.conf
To explain it visually, following could help.
my-yii2-app/
/web -> DocumentRoot
..
..
/vendor
...
...
/lucidprogrammer (newly created folder in vendor)
/simplesamlphp (newly created folder in vendor)
/simplesamlphp/wwww -> [Create an alias /saml pointing to pathto_my-yii2-app/vendor/simplesamlphp/simplesamlphp/www]
Yii configuration straightforward, just add the following in your config/web.php
'user' => [
'class' => 'lucidprogrammer\simplesamlphp\SamlUser',
],
Let's say you have a rule like the following in your yii2 setting,
'access' => [
'class' => AccessControl::className(),
'only' => ['index','logout','about'],
'rules' => [
[
'allow' => true,
'actions' => ['index'],
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
After the component is installed, the moment you hit the site/about page, it should redirect you to the configured saml idp login page.
So, if you want to do SAML provided attributes and want to implement a fine grained access control, yii2 makes it easy.
If your application has links to login, for example, 'site/login', you need to change to _saml/login.
However, it is best if you use Yii::$app->user->loginUrl[0], so it will take whatever is the correct loginUrl, so it will work with or without this plugin.