uhi67/yii2-eduid

为 Yii2 应用程序的 EduID SP 登录

安装: 464

依赖: 0

建议者: 0

安全: 0

类型:yii2-extension

2.0-rc 2023-03-10 11:30 UTC

This package is auto-updated.

Last update: 2024-09-10 14:36:15 UTC


README

使用已预装的 SimpleSAMLphp SP 实例作为 Yii2 应用的 EduID 登录助手

版本 1.4 发布于 2022-03-25

先决条件

  • yii2 >= 2.0.13
  • php >= 5.6
  • SimpleSamlPHP >= 1.14

安装

安装此扩展的首选方式是通过 composer

要安装,请运行

    composer require uhi67/yii2-eduid "1.*" 

或添加

"uhi67/yii2-eduid" : "1.*"

或从 github 克隆

    git clone https://github.com/uhi67/yii2-eduid

用法

配置文件(web.php)中的设置

'components' => [
    'saml' => [
        'class' => 'uhi67\eduidsp\Saml',
        'simpleSamlPath' => '/usr/share/simplesamlphp/',
        'authSource' => 'default-sp'
        'idAttribute' => 'eduPersonTargetedID',    ]
]

'simpleSamlPath' 属性仅在 SimpleSAMLphp 独立安装且未由 composer 自身包含到当前项目中时需要。

代码中的用法

/**
 * Login action.
 *
 * @return Response
 */
public function actionLogin() {
	/** @var Saml $saml */
    $saml = Yii::$app->saml;
    if($saml->isAuthenticated() && !Yii::$app->user->isGuest) return $this->goHome();
    if (!$saml->isAuthenticated()) {
	    if(!$saml->requireAuth()) { // optional params array may be passed
	    	throw new ForbiddenHttpException('Authentication failure');
	    };
    }

    /** @var SamlIdentityInterface $identityClass */
    $identityClass = Yii::$app->user->identityClass;
    /** @var SamlIdentityInterface|ActiveRecord $appUser -- Locate user record in identity table */
    $appUser = $identityClass::findIdentityByUid($saml->id);
    if(!$appUser) {
        // Optional attribute check if user can be created goes here
        // Create new user record
	    $appUser = new $identityClass();
    }
    $appUser->setSamlAttributes($saml->attributes);
    $appUser->save();
    Yii::$app->user->login($appUser);
    return $this->goHome();
}

/**
 * Logout action.
 *
 * @return Response
 */
public function actionLogout() {
    /** @var Saml $saml */
    $saml = Yii::$app->saml;
    Yii::$app->user->logout();
    if($saml->isAuthenticated()) $saml->logout(); // This call never returns
    return $this->goHome();
}

视图中的用法

对于普通登录按钮,使用 eduid-login 类,对于轻量版使用 eduid-login eduid-light 类。

<?php
use uhi67\eduidsp\SamlAsset;

/* @var yii\web\View $this */
SamlAsset::register($this);
?>
<?= Html::a(Yii::t('app', 'Login'), ['saml'], ['class' => 'btn btn-primary eduid-login eduid-light']) ?>

在 Yii2/bootstrap 导航栏中的用法

<?php
NavBar::begin([
    'brandLabel' => Yii::$app->name,
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
]);
echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => [
        ...
        Yii::$app->saml->isAuthenticated() ?
            ['label' => Yii::$app->saml->attributes['displayName'][0], 'url' => ['/site/logout']] :
            ['label' => Yii::t('app', 'Login'), 'url' => ['/site/login'], 'options'=>['class'=>'eduid-login']],
    ],
]);
NavBar::end();
?>

API 文档

\uhi67\eduidsp\Saml

- isAuthenticated() -- determines if the user is currently authenticated in SAMl/EduID
- requireAuth() -- Initializes login on configured authentication source
- attributes -- all attributes of the logged in user got from IdP
- logout() -- Forces logout from SAML/EduiID
- formatAttribute() -- Html formats attribute value set
- translateAttributeName() -- Translates attribute name to given language
- scope -- the best scope value determined from csoped attribute values. Waring: the scope is not clearly mapped to the organization 

\uhi67\eduidsp\SamlIdentityInterface

- setSamlAttributes($attributes)

变更

1.5 (2022-04-12)

  • 退出参数
  • 登录参数文档

1.4 (2022-03-25)

  • 添加了 scope 属性

1.3 (2020-10-20)

  • requireAuth() 现在可以接受可选的 params 数组

1.2.3

  • 如果存在,使用新命名空间

1.2.2

  • 自动加载命名空间错误

1.2.1

  • Composer 构建的 SimpleSAMLphp 兼容性

1.2

  • 添加了 Saml::formatAttribute()
  • 添加了 Saml::translateAttributeName()

1.1.x

  • 错误修复

1.1

  • 首次发布