hiqdev/yii2-mfa

为 Yii2 项目提供多因素认证

安装次数: 9,542

依赖项: 2

建议者: 0

安全: 0

星星: 10

关注者: 8

分支: 5

公开问题: 2

类型:yii2-extension

0.1.0 2017-10-03 19:25 UTC

This package is auto-updated.

Last update: 2024-09-12 16:13:26 UTC


README

为 Yii2 项目提供多因素认证

Latest Stable Version Total Downloads Build Status Scrutinizer Code Coverage Scrutinizer Code Quality Dependency Status

本软件包提供以下功能

  • TOTP - 用于双因素认证的时间基础一次性密码算法
  • 检查用户允许的 IP
  • 生成和检查恢复代码(计划中)

使用

可以集成到任何现有的 Yii2 项目中。查看如何在 hiqdev/hiam 中使用。

安装

安装此 yii2-extension 的首选方法是使用 composer

运行

php composer.phar require "hiqdev/yii2-mfa"

或将其添加到 composer.json 的 require 部分。

"hiqdev/yii2-mfa": "*"

配置

此扩展提供可插拔配置,可用于与 composer-config-plugin 一起使用。

您也可以通过复制粘贴配置来使用它。有关配置示例,请参阅 src/config/web.php

可用的配置参数

  • organization.name

有关更多详细信息,请参阅 src/config/params.php

使用方法

此插件提供了在 beforeLogin 事件上附加到用户组件的行为和配置。然后,行为在每次登录时验证 IP 和 TOTP。

要使用此插件,您必须从 hiqdev\yii2\mfa\base\MfaIdentityInterface 实例化您的 \Yii->app->user->identity 类,并实现所有将返回或设置 MFA 属性的方法。例如

use hiqdev\yii2\mfa\base\MfaIdentityInterface;

class Identity implements MfaIdentityInterface
{
    ...

    /**
     * @inheritDoc
     */
    public function getUsername(): string
    {
        return $this->username;
    }

    /**
     * @inheritDoc
     */
    public function getTotpSecret(): string
    {
        return $this->totp_secret ?? '';
    }

    ...

IP 和 TOTP 函数是独立的,您只能提供其中一个属性以仅具有相应的功能。

与 OAuth2 一起使用

还有配置可以提供 OAuth2 的 MFA。

  • 需要建议的 "bshaffer/oauth2-server-php": '~1.7'

  • 使用 hiqdev\yii2\mfa\GrantType\UserCredentials 配置通过 totp 代码的 /oauth/token 命令。例如

    'modules' => [ 'oauth2' => [ 'grantTypes' => [ 'user_credentials' => [ 'class' => \hiqdev\yii2\mfa\GrantType\UserCredentials::class, ], ], ], ]

  • ApiMfaIdentityInterface 扩展您的 Identity 类。

  • 使用操作

    POST /mfa/totp/api-temporary-secret - 提供临时密钥以生成 QR 码 POST /mfa/totp/api-enable - 启用 totp POST /mfa/totp/api-disable - 禁用 totp

回退重定向

对于任何 MFA 路由,您可以添加一个 GET 参数 ?back=https://some.site.com。操作成功后,将用户重定向到所需的站点。为了避免公开重定向漏洞,您需要验证 back 参数。

应使用 \hiqdev\yii2\mfa\validator\BackUrlValidatorInterface 来完成此操作,它有一个默认实现。您必须创建自己的并重新使用容器定义初始化它

config/web.php

'container' => [
   'singletons' => [
       \hiqdev\yii2\mfa\validator\BackUrlValidatorInterface::class => \your\own\validator::class,
    ],
],

许可证

本项目根据 BSD-3-Clause 许可协议发布。有关更多信息,请参阅 LICENSE。更多信息请参阅 此处

版权所有 © 2016-2018,HiQDev(http://hiqdev.com/