项目作者: asasmoyo

项目描述 :
Connect Yii 2 application to a Saml Identity Provider for Single Sign on
高级语言: PHP
项目地址: git://github.com/asasmoyo/yii2-saml.git
创建时间: 2015-12-31T03:31:13Z
项目社区:https://github.com/asasmoyo/yii2-saml

开源协议:

下载


Yii 2 Saml

Build Status

Connect Yii 2 application to a Saml Identity Provider for Single Sign On

Installation

The preferred way to install this extension is through composer.

Either run

  1. php composer.phar require --prefer-dist asasmoyo/yii2-saml "*"

or add

  1. "asasmoyo/yii2-saml": "*"

to the require section of your composer.json file.

Configuration

Register asasmoyo\yii2saml\Saml to your components in config/web.php.

  1. 'components' => [
  2. 'saml' => [
  3. 'class' => 'asasmoyo\yii2saml\Saml',
  4. 'configFileName' => '@app/config/saml.php', // OneLogin_Saml config file (Optional)
  5. ]
  6. ]

This component requires a OneLogin_Saml configuration stored in a php file. The default value for configFileName is @app/config/saml.php so make sure to create this file before. This file must returns the OneLogin_Saml configuration. See this link for example configuration.

  1. <?php
  2. $urlManager = Yii::$app->urlManager;
  3. $spBaseUrl = $urlManager->getHostInfo() . $urlManager->getBaseUrl();
  4. return [
  5. 'sp' => [
  6. 'entityId' => $spBaseUrl.'/saml/metadata',
  7. 'assertionConsumerService' => [
  8. 'url' => $spBaseUrl.'/saml/acs',
  9. ],
  10. 'singleLogoutService' => [
  11. 'url' => $spBaseUrl.'/saml/sls',
  12. ],
  13. 'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified',
  14. ],
  15. 'idp' => [
  16. 'entityId' => 'identity-provider',
  17. 'singleSignOnService' => [
  18. 'url' => 'https://idp.com/sso',
  19. ],
  20. 'singleLogoutService' => [
  21. 'url' => 'https://idp.com/sls',
  22. ],
  23. 'x509cert' => '<x509cert string>',
  24. ],
  25. ];

NOTE : As of version 1.6.0 you can directly put your configuration into your component. For example:

  1. <?php
  2. $urlManager = Yii::$app->urlManager;
  3. $spBaseUrl = $urlManager->getHostInfo() . $urlManager->getBaseUrl();
  4. $config = [
  5. // some other configuration here
  6. 'components' => [
  7. 'saml' => [
  8. 'class' => 'asasmoyo\yii2saml\Saml',
  9. 'config' => [
  10. 'sp' => [
  11. 'entityId' => $spBaseUrl.'/saml/metadata',
  12. 'assertionConsumerService' => [
  13. 'url' => $spBaseUrl.'/saml/acs',
  14. ],
  15. 'singleLogoutService' => [
  16. 'url' => $spBaseUrl.'/saml/sls',
  17. ],
  18. 'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified',
  19. ],
  20. 'idp' => [
  21. 'entityId' => 'identity-provider',
  22. 'singleSignOnService' => [
  23. 'url' => 'https://idp.com/sso',
  24. ],
  25. 'singleLogoutService' => [
  26. 'url' => 'https://idp.com/sls',
  27. ],
  28. 'x509cert' => '<x509cert string>',
  29. ],
  30. ],
  31. ]
  32. ],
  33. // some other configuration here
  34. ];
  35. return $config;

Usage

This extension provides 4 actions:

  1. LoginAction

    This actions will initiate login process to Identity Provider specified in config file. To use this action, just register this action to your actions in your controller.

    ```php
    <?php

    namespace app\controllers;

    use Yii;
    use yii\web\Controller;
    use yii\helpers\Url;

  1. class SamlController extends Controller {
  2. // Remove CSRF protection
  3. public $enableCsrfValidation = false;
  4. public function actions() {
  5. return [
  6. 'login' => [
  7. 'class' => 'asasmoyo\yii2saml\actions\LoginAction',
  8. 'returnTo' => Yii::app()->user->returnUrl
  9. ]
  10. ];
  11. }
  12. }
  13. ```
  14. The login method can receive seven optional parameters:
  15. * `$returnTo` - The target URL the user should be returned to after login..
  16. * `$parameters` - An array of parameters that will be added to the `GET` in the HTTP-Redirect.
  17. * `$forceAuthn` - When true the `AuthNRequest` will set the `ForceAuthn='true'`
  18. * `$isPassive` - When true the `AuthNRequest` will set the `Ispassive='true'`
  19. * `$strict` - True if we want to stay (returns the url string) False to redirect
  20. * `$setNameIdPolicy` - When true the AuthNRequest will set a nameIdPolicy element.
  21. * `$nameIdValueReq` - Indicates to the IdP the subject that should be authenticated.
  22. Now you can login to your Identity Provider by visiting ``saml/login``.
  1. AcsAction

    This action will process saml response sent by Identity Provider after succesfull login. You can register a callback to do some operation like read the attributes sent by Identity Provider and create a new user from that attributes. To use this action just register this action to you controllers’s actions.

    ```php
    <?php

    namespace app\controllers;

    use Yii;
    use yii\web\Controller;
    use yii\helpers\Url;

  1. class SamlController extends Controller {
  2. // Remove CSRF protection
  3. public $enableCsrfValidation = false;
  4. public function actions() {
  5. return [
  6. ...
  7. 'acs' => [
  8. 'class' => 'asasmoyo\yii2saml\actions\AcsAction',
  9. 'successCallback' => [$this, 'callback'],
  10. 'successUrl' => Url::to('site/welcome'),
  11. ]
  12. ];
  13. }
  14. /**
  15. * @param array $param has 'attributes', 'nameId' , 'sessionIndex', 'nameIdNameQualifier' and 'nameIdSPNameQualifier' from response
  16. */
  17. public function callback($param) {
  18. // do something
  19. //
  20. // if (isset($_POST['RelayState'])) {
  21. // $_POST['RelayState'] - should be returnUrl from login action
  22. // }
  23. }
  24. }
  25. ```
  26. **NOTE: Make sure to register the acs action's url to ``AssertionConsumerService`` and the sls actions's url to ``SingleLogoutService`` (if supported) in the Identity Provider.**
  1. MetadataAction

    This action will show metadata of you application in xml. To use this action, just register the action to your controller’s action.

    1. <?php
    2. public function actions() {
    3. return [
    4. ...
    5. 'metadata' => [
    6. 'class' => 'asasmoyo\yii2saml\actions\MetadataAction'
    7. ]
    8. ];
    9. }
  2. LogoutAction

    This action will initiate SingleLogout process to Identity Provider. To use this action, just register this action to your controller’s actions.

    1. <?php
    2. $session = Yii::$app->session;
    3. public function actions() {
    4. return [
    5. ...
    6. 'logout' => [
    7. 'class' => 'asasmoyo\yii2saml\actions\LogoutAction',
    8. 'returnTo' => Url::to('site/bye'),
    9. 'parameters' => [],
    10. 'nameId' => $session->get('nameId'),
    11. 'sessionIndex' => $session->get('sessionIndex'),
    12. 'stay' => false,
    13. 'nameIdFormat' => null,
    14. 'nameIdNameQualifier' => $session->get('nameIdNameQualifier'),
    15. 'nameIdSPNameQualifier' => $session->get('nameIdSPNameQualifier'),
    16. 'logoutIdP' => false, // if you don't want to logout on idp
    17. ]
    18. ];
    19. }
  3. SlsAction

    This action will process saml logout request/response sent by Identity Provider. To use this action just register this action to you controllers’s actions.

    1. <?php
    2. public function actions() {
    3. ...
    4. return [
    5. ...
    6. 'sls' => [
    7. 'class' => 'asasmoyo\yii2saml\actions\SlsAction',
    8. 'successUrl' => Url::to('site/bye'),
    9. 'logoutIdP' => false, // if you don't want to logout on idp
    10. ]
    11. ]
    12. }

Usage

If the SAMLResponse is rejected, add to the SAML settings the parameter

  1. 'debug' => true,

and the reason will be prompted.

LICENCE

MIT Licence