how to use SimpleSAMLphp in yii framework?
Asked Answered
H

3

7

I have two project in yii framework and I want to use both project using SimpleSAMLphp with SSO. The condition, I need is if I login from the first project, i want access to the second project. Thank you in advance.

Humoresque answered 3/4, 2014 at 6:2 Comment(3)
It would be nice if you place some code and clarify what exactly the problem is. Please see this yiiframework.com/forum/index.php/topic/…Wollongong
have you checkout in simplesamlphp.org/docs/stable/authorize:authorizeLondoner
@ Rohan Ale , thamk you for your advice but please can you give me the way to implement it in yii framework.Humoresque
S
2

First you load the SAML library by temporarily disabling the Yii autoloader. This is just to let you use the SAML classes and methods:

<?php
class YiiSAML extends CComponent {
    private $_yiiSAML = null;
    static private function pre() {
        require_once (Yii::app()->params['simpleSAML'] . '/lib/_autoload.php');
        // temporary disable Yii autoloader
        spl_autoload_unregister(array(
            'YiiBase',
            'autoload'
        ));
    }
    static private function post() {
        // enable Yii autoloader
        spl_autoload_register(array(
            'YiiBase',
            'autoload'
        ));
    }
    public function __construct() {
        self::pre();
        //We select our authentication source:
        $this->_yiiSAML = new SimpleSAML_Auth_Simple(Yii::app()->params['authSource']);
        self::post();
    }
    static public function loggedOut($param, $stage) {
        self::pre();
        $state = SimpleSAML_Auth_State::loadState($param, $stage);
        self::post();
        if (isset($state['saml:sp:LogoutStatus'])) {
            $ls = $state['saml:sp:LogoutStatus']; /* Only for SAML SP */
        } else return true;
        return $ls['Code'] === 'urn:oasis:names:tc:SAML:2.0:status:Success' && !isset($ls['SubCode']);
    }
    public function __call($method, $args) {
        $params = (is_array($args) and !empty($args)) ? $args[0] : $args;
        if (method_exists($this->_yiiSAML, $method)) return $this->_yiiSAML->$method($params);
        else throw new YiiSAMLException(Yii::t('app', 'The method {method} does not exist in the SAML class', array(
        '{method}' => $method
    )));
    }
}
class YiiSAMLException extends CException {
}

Then you define a filter extending the CFilter Yii class:

<?php
Yii::import('lib.YiiSAML');
class SAMLControl extends CFilter {
    protected function preFilter($filterChain) {
        $msg = Yii::t('yii', 'You are not authorized to perform this action.');
        $saml = new YiiSAML();
        if (Yii::app()->user->isGuest) {
            Yii::app()->user->loginRequired();
            return false;
        } else {
            $saml_attributes = $saml->getAttributes();
            if (!$saml->isAuthenticated() or Yii::app()->user->id != $saml_attributes['User.id'][0]) {
                Yii::app()->user->logout();
                Yii::app()->user->loginRequired();
                return false;
            }
            return true;
        }
    }
}

And finally, in the controllers you are interested to restrict, you override the filters() method:

public function filters() {
    return array(
        array(
            'lib.SAMLControl'
        ) , // perform access control for CRUD operations
        ...
    );
}

Hope it helps.

Sinusoid answered 9/10, 2014 at 16:10 Comment(0)
K
0

It can be done simply using "vendors" directory.

  1. Download PHP Library from https://simplesamlphp.org/
  2. Implement it in Yii Framework as a vendor library. (http://www.yiiframework.com/doc/guide/1.1/en/extension.integration)

Good Luck :)

Kevel answered 30/9, 2014 at 4:59 Comment(0)
A
0

I came across an Yii Extension for SimpleSAMLphp in github

https://github.com/asasmoyo/yii-simplesamlphp

You can load the simplesamlphp as a vendor library and then specify the autoload file in the extension.

Apart from the extension you can copy all the necessary configs and metadatas into the application and configure SimpleSAML Configuration to load the configurations from your directory, so you can keep the vendor package untouched for future updates.

Abyss answered 19/5, 2015 at 19:50 Comment(1)
good, find but the question was asked 5 years ago :) which means yii1 package i am still looking for yii1 packageRamillies

© 2022 - 2024 — McMap. All rights reserved.