ingeneo / yii2-saml
将 Yii 2 应用程序连接到 Saml 身份提供者以实现单点登录
Requires
- ingeneo/onelogin-php-saml: ~3.3.3
- yiisoft/yii2: >=2.0.13
Requires (Dev)
- phpunit/phpunit: 5.0.*
Conflicts
- phpunit/php-timer: >=2
README
将 Yii 2 应用程序连接到 Saml 身份提供者以实现单点登录
安装
安装此扩展的首选方式是通过 composer.
运行
php composer.phar require --prefer-dist asasmoyo/yii2-saml "*"
或者在您的 composer.json 文件的 require 部分添加
"asasmoyo/yii2-saml": "*"
。
配置
在 config/web.php 中将 ingeneo\yii2saml\Saml 注册到组件中。
'components' => [ 'saml' => [ 'class' => 'ingeneo\yii2saml\Saml', 'configFileName' => '@app/config/saml.php', // OneLogin_Saml config file (Optional) ] ]
此组件需要一个存储在 php 文件中的 OneLogin_Saml 配置。默认的 configFileName 值是 @app/config/saml.php,因此请确保在之前创建此文件。此文件必须返回 OneLogin_Saml 配置。有关示例配置,请参阅此 链接。
<?php $urlManager = Yii::$app->urlManager; $spBaseUrl = $urlManager->getHostInfo() . $urlManager->getBaseUrl(); return [ 'sp' => [ 'entityId' => $spBaseUrl.'/saml/metadata', 'assertionConsumerService' => [ 'url' => $spBaseUrl.'/saml/acs', ], 'singleLogoutService' => [ 'url' => $spBaseUrl.'/saml/sls', ], 'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified', ], 'idp' => [ 'entityId' => 'identity-provider', 'singleSignOnService' => [ 'url' => 'https://idp.com/sso', ], 'singleLogoutService' => [ 'url' => 'https://idp.com/sls', ], 'x509cert' => '<x509cert string>', ], ];
注意:自版本 1.6.0 以来,您可以直接将配置放入组件中。例如
<?php $urlManager = Yii::$app->urlManager; $spBaseUrl = $urlManager->getHostInfo() . $urlManager->getBaseUrl(); $config = [ // some other configuration here 'components' => [ 'saml' => [ 'class' => 'ingeneo\yii2saml\Saml', 'config' => [ 'sp' => [ 'entityId' => $spBaseUrl.'/saml/metadata', 'assertionConsumerService' => [ 'url' => $spBaseUrl.'/saml/acs', ], 'singleLogoutService' => [ 'url' => $spBaseUrl.'/saml/sls', ], 'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified', ], 'idp' => [ 'entityId' => 'identity-provider', 'singleSignOnService' => [ 'url' => 'https://idp.com/sso', ], 'singleLogoutService' => [ 'url' => 'https://idp.com/sls', ], 'x509cert' => '<x509cert string>', ], ]; ] ], // some other configuration here ]; return $config;
用法
此扩展提供了 4 个动作
-
登录动作
此动作将启动配置文件中指定的身份提供者的登录过程。要使用此动作,只需将此动作注册到您的控制器动作中。
<?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\helpers\Url; class SamlController extends Controller { // Remove CSRF protection public $enableCsrfValidation = false; public function actions() { return [ 'login' => [ 'class' => 'ingeneo\yii2saml\actions\LoginAction' ] ]; } }
现在您可以通过访问
saml/login来登录到您的身份提供者。 -
Acs 动作
此动作将处理身份提供者在成功登录后发送的 saml 响应。您可以为操作注册一个回调,例如读取身份提供者发送的属性并从这些属性中创建新用户。要使用此动作,只需将此动作注册到您控制器的动作中。
<?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\helpers\Url; class SamlController extends Controller { // Remove CSRF protection public $enableCsrfValidation = false; public function actions() { return [ ... 'acs' => [ 'class' => 'ingeneo\yii2saml\actions\AcsAction', 'successCallback' => [$this, 'callback'], 'successUrl' => Url::to('site/welcome'), ] ]; } /** * @param array $attributes attributes sent by Identity Provider. */ public function callback($attributes) { // do something } }
注意:确保在身份提供者中将 acs 动作的 URL 注册到
AssertionConsumerService,并将 sls 动作的 URL 注册到SingleLogoutService(如果支持)。 -
元数据动作
此动作将以 xml 格式显示您应用程序的元数据。要使用此动作,只需将动作注册到控制器的动作中。
<?php public function actions() { return [ ... 'metadata' => [ 'class' => 'ingeneo\yii2saml\actions\MetadataAction' ] ]; }
-
注销动作
此动作将启动到身份提供者的单点注销过程。要使用此动作,只需将此动作注册到控制器的动作中。
<?php public function actions() { return [ ... 'logout' => [ 'class' => 'ingeneo\yii2saml\actions\LogoutAction', 'returnTo' => Url::to('site/bye'), ] ]; }
-
Sls 动作
此动作将处理身份提供者发送的 saml 注销请求/响应。要使用此动作,只需将此动作注册到您控制器的动作中。
<?php public function actions() { ... return [ ... 'sls' => [ 'class' => 'ingeneo\yii2saml\actions\SlsAction', 'successUrl' => Url::to('site/bye'), ] ] }
用法
如果 SAMLResponse 被拒绝,请将以下参数添加到 SAML 设置中
'debug' => true,
并将提示原因。
许可协议
MIT 许可协议