silents/yii2-auth-cas

为 Yii2 提供的 CAS 认证

安装: 516

依赖: 0

建议者: 0

安全: 0

星星: 7

关注者: 6

分支: 4

开放问题: 0

类型:yii2-extension

1.0.7 2024-01-24 14:50 UTC

This package is not auto-updated.

Last update: 2024-10-02 18:43:56 UTC


README

使用 phpCAS 库实现的 CAS 认证功能的 Yii2 库。

用法

  1. 使用 composer require silecs/yii2-auth-cas 将其添加到项目中

  2. 配置 Yii2 应用程序,例如在 backend/config/main.php

    return [
        ...
        'modules' => [
            'cas' => [
                'class' => 'silecs\yii2auth\cas\CasModule',
                'config' => [
                    'host' => 'ssoserver.example.com',
                    'port' => '443',
                    'path' => '/idp/profile/cas',
                    // optional parameters
                    'certfile' => '', // empty, or path to a SSL cert, or false to ignore certs
                    'debug' => true, // will add many logs into X/runtime/logs/cas.log
                ],
            ],
    
  3. SiteController 中添加使用此 CAS 模块的操作

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        return $this->redirect(['/cas/auth/login']);
    }
    
    public function actionLogout()
    {
        if (Yii::$app->user->isGuest) {
            return $this->redirect(['/cas/auth/logout']);
        }
        return $this->goHome();
    }
    

注意

实现 yii\web\IdentityInterfaceuser 组件将在查询 CAS 服务器后用于获取本地配置文件。这意味着,如果 User 是应用程序组件,并且 CAS 返回的用户名为 "bibendum",则只有当 User::findIdentity("bibendum") 的结果不为空时,认证才会成功。

操作路径 '/cas/auth/login' 以应用程序配置中定义的模块别名开头,例如在 'modules' => [ 'cas' => [ ... ] ] 中的 'cas'

使用 CAS 容器进行测试

以下是一些部署 Docker CAS 服务器以测试此库的说明。此过程将使用 Shibboleth 实例的 CAS 接口。这在 Debian Stretch 和 Buster(测试版)上进行了测试。

  1. 从 docker.io 的额外仓库安装 docker(我使用官方 Debian 仓库中的旧版 docker 时遇到了错误)。

  2. 从 Debian 或 docker.io 安装 docker-compose

  3. Git 克隆 https://hub.docker.com/r/unicon/shibboleth-idp/ 如果使用旧版 docker-compose,则切换到 3c29f10,因为后续提交需要较新的功能。

  4. 修改 docker-compose.yml 以确保容器不会尝试使用端口 80,因此将 "80:80" 替换为 "8080:80"

  5. 如果您的本地 Yii2 应用程序未使用 HTTPS,请将 idp/shibboleth-idp/conf/cas-protocol.xml 中的 c:regex="https://idptestbed/.*" 替换为 c:regex="https?://idptestbed/.*"

  6. 127.0.0.1 idptestbed 添加到 /etc/hosts,作为 root。

  7. 配置您的 Yii2 应用程序以使用

     'host' => 'idptestbed',
     'port' => '443',
     'path' => '/idp/profile/cas',
     'certfile' => false,
     'debug' => true,
    
  8. 启动容器

     docker-compose build
     docker-compose run
    
  9. 访问您的 Yii2 应用的登录页面。

  10. 在容器终端中按 Ctrl-C 结束它们。

如果您想向 CAS 添加用户,可以修改 ldap/users.ldif。别忘了在此之后重建 Docker 镜像。