All Projects → asasmoyo → yii2-saml

asasmoyo / yii2-saml

Licence: other
Connect Yii 2 application to a Saml Identity Provider for Single Sign on

Programming Languages

PHP
23972 projects - #3 most used programming language

Labels

Projects that are alternatives of or similar to yii2-saml

yii2-rollbar
Rollbar for Yii2
Stars: ✭ 36 (+5.88%)
Mutual labels:  yii2
luya-bootstrap4
Bootstrap4 Assets and Helper classes like ActiveForm for LUYA and Yii2.
Stars: ✭ 18 (-47.06%)
Mutual labels:  yii2
background-translation-i18n
Based on the YII2 module to translate JSON formatted translation files on the web
Stars: ✭ 11 (-67.65%)
Mutual labels:  yii2
SpBundle
SAML2 SP Symfony Bundle based on LightSAML
Stars: ✭ 62 (+82.35%)
Mutual labels:  saml2
yii2-array-query
Yii2 component that allows for searching/filtering the elements of an array.
Stars: ✭ 34 (+0%)
Mutual labels:  yii2
yii2-imagick
Class for working with Imagick
Stars: ✭ 17 (-50%)
Mutual labels:  yii2
yii2-linkable-behavior
Yii2 behavior to help creating urls easier
Stars: ✭ 12 (-64.71%)
Mutual labels:  yii2
yii2-command-bus
Command Bus for Yii2
Stars: ✭ 56 (+64.71%)
Mutual labels:  yii2
yii2-league-oauth2-server
Yii 2.0 implementation of PHP league OAuth2 server interfaces
Stars: ✭ 29 (-14.71%)
Mutual labels:  yii2
yii2-mariadb
MariaDB Driver for Yii2
Stars: ✭ 24 (-29.41%)
Mutual labels:  yii2
yii2-vote
Provides voting for any model 👍 👎
Stars: ✭ 70 (+105.88%)
Mutual labels:  yii2
service-skeleton
Microservice skeleton based on yii2 framework.
Stars: ✭ 14 (-58.82%)
Mutual labels:  yii2
yii2-datetime-widgets
Datetime widgets for Yii2
Stars: ✭ 22 (-35.29%)
Mutual labels:  yii2
php-framework-benchmark
php framework benchmark (include laravel、symfony、silex、lumen、slim、yii2、tastphp etc)
Stars: ✭ 17 (-50%)
Mutual labels:  yii2
yii2-elasticsearch
Elasticsearch client based on official Elasticsearch PHP library
Stars: ✭ 14 (-58.82%)
Mutual labels:  yii2
yii2-content-tools
ContentTools editor implementation for Yii 2
Stars: ✭ 79 (+132.35%)
Mutual labels:  yii2
yii2-toastr
Yii2 - Javascript Toast Notifications
Stars: ✭ 25 (-26.47%)
Mutual labels:  yii2
yii2-emoji
😄 this is a emoji extension of yii2.
Stars: ✭ 17 (-50%)
Mutual labels:  yii2
yii2-ion-slider
Easily customizable range slider with skins support.
Stars: ✭ 21 (-38.24%)
Mutual labels:  yii2
yii2-sweet-submit
sweet sumit using sweetalert
Stars: ✭ 26 (-23.53%)
Mutual labels:  yii2

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

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

or add

"asasmoyo/yii2-saml": "*"

to the require section of your composer.json file.

Configuration

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

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

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.

<?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>',
    ],
];

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

<?php

$urlManager = Yii::$app->urlManager;
$spBaseUrl = $urlManager->getHostInfo() . $urlManager->getBaseUrl();

$config = [
    // some other configuration here

    'components' => [
        'saml' => [
            'class' => 'asasmoyo\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;

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
    
    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' => 'asasmoyo\yii2saml\actions\LoginAction',
                    'returnTo' => Yii::app()->user->returnUrl
                ]
            ];
        }
    
    }

    The login method can receive seven optional parameters:

    • $returnTo - The target URL the user should be returned to after login..
    • $parameters - An array of parameters that will be added to the GET in the HTTP-Redirect.
    • $forceAuthn - When true the AuthNRequest will set the ForceAuthn='true'
    • $isPassive - When true the AuthNRequest will set the Ispassive='true'
    • $strict - True if we want to stay (returns the url string) False to redirect
    • $setNameIdPolicy - When true the AuthNRequest will set a nameIdPolicy element.
    • $nameIdValueReq - Indicates to the IdP the subject that should be authenticated.

    Now you can login to your Identity Provider by visiting saml/login.

  2. 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
    
    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' => 'asasmoyo\yii2saml\actions\AcsAction',
                    'successCallback' => [$this, 'callback'],
                    'successUrl' => Url::to('site/welcome'),
                ]
            ];
        }
    
        /**
         * @param array $param has 'attributes', 'nameId' , 'sessionIndex', 'nameIdNameQualifier' and 'nameIdSPNameQualifier' from response
         */
        public function callback($param) {
            // do something
            //
            // if (isset($_POST['RelayState'])) {
            // $_POST['RelayState'] - should be returnUrl from login action
            // }
        }
    }

    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.

  3. MetadataAction

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

    <?php
    
        public function actions() {
            return [
                ...
                'metadata' => [
                    'class' => 'asasmoyo\yii2saml\actions\MetadataAction'
                ]
            ];
        }
  4. LogoutAction

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

    <?php
        $session = Yii::$app->session;
        public function actions() {
            return [
                ...
                'logout' => [
                    'class' => 'asasmoyo\yii2saml\actions\LogoutAction',
                    'returnTo' => Url::to('site/bye'),
                    'parameters' => [],
                    'nameId' => $session->get('nameId'),
                    'sessionIndex' => $session->get('sessionIndex'),
                    'stay' => false,
                    'nameIdFormat' => null,
                    'nameIdNameQualifier' => $session->get('nameIdNameQualifier'),
                    'nameIdSPNameQualifier' => $session->get('nameIdSPNameQualifier'),
                    'logoutIdP' => false, // if you don't want to logout on idp
                ]
            ];
        }
  5. 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.

    <?php
    
        public function actions() {
            ...
    
            return [
                ...
                'sls' => [
                    'class' => 'asasmoyo\yii2saml\actions\SlsAction',
                    'successUrl' => Url::to('site/bye'),
                    'logoutIdP' => false, // if you don't want to logout on idp
                ]
            ]
        }

Usage

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

'debug' => true,

and the reason will be prompted.

LICENCE

MIT Licence

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].