nowise/uup-auth

支持多种凭证获取方法和账户验证后端的灵活认证堆栈。

2.3.21 2019-10-14 23:59 UTC

README

uup-auth 包提供了一种库,可以将认证器堆叠起来,以统一的方式支持多种认证方法。

还包括执行访问限制(例如,根据时间或IP地址/主机名)的限制器。堆栈中的所有认证器都可以根据需要设置为强制或充分以执行登录策略(例如,要求从LAN外进行CAS登录,同时支持从LAN内进行Kerberos登录)。

该库是模块化的。认证器是前端(凭证获取器),可能使用验证器作为认证源(例如LDAP)。认证器可以与存储对象结合使用,以支持登录会话。

认证器可以在堆栈中使用或单独使用(单一登录方法)。如果配置堆栈,可以使用访问类之一来轻松访问链和认证器。

+-- UUP/Authentication/
      +-- Authenticator/        : Authenticator frontend classes.
      +-- Restrictor/           : Restrictor classes.
      +-- Stack/                : Support for stacking authenticators/restrictors.
      +-- Storage/              : Persistance support.
      +-- Validator/            : Authentication support.

示例

一个典型的认证/授权堆栈,通过PAM、CAS和LDAP提供登录,并对网络和登录时间进行限制,可能看起来像这样

class Authentication extends AuthenticatorStack
{

    public function __construct()
    {
        $chain = array(
            // 
            // Plugin account authenticator objects in stack:
            // 
            'auth'   => array(
                'pam'  => (new SystemAuthentication())
                    ->visible(true)
                    ->control(Authenticator::SUFFICIENT)
                    ->name('System')
                    ->description('Login using local system account.'),
                'cas'  => (new CasAuthenticator('cas.example.com'))
                    ->visible(true)
                    ->control(Authenticator::SUFFICIENT)
                    ->name('CAS')
                    ->description('CAS server login'),
                'ldap' => (new LdapAuthenticator('ldaps://ldap.example.com'))
                    ->visible(true)
                    ->control(Authenticator::SUFFICIENT)
                    ->name('LDAP')
                    ->description('LDAP authentication')
            ),
            // 
            // Add some login restrictions:
            // 
            'access' => array(
                'addr' => (new AddressRestrictor(array('::1', '127.0.0.1', '192.168.0.0/16')))
                    ->visible(false)
                    ->control(Authenticator::REQUIRED),
                'time' => (new DateTimeRestrictor('08:45', '16:30'))
                    ->visible(false)
                    ->control(Authenticator::REQUIRED)
            )
        );

        parent::__construct($chain);
    }

    public function getName()
    {
        return $this->getAuthenticator()->name;
    }

}

在某个地方(典型的分发器或主模板)添加一些代码来处理登录/注销请求并渲染登录表单

try {
    $authenticator = new Authentication();

    if (filter_has_var(INPUT_GET, 'login')) {
        $authenticator->activate(filter_input(INPUT_GET, 'login'));
        $authenticator->login();
    }
    if (filter_has_var(INPUT_GET, 'logout')) {
        $authenticator->logout();
    }

    if ($authenticator->accepted()) {
        printf("<p>Logged on to %s as %s | <a href=\"?logout\">Logout</a>\n", 
                $authenticator->getName(), 
                $authenticator->getSubject()
        );
    } else {
        printf("<form action=\"\" method=\"GET\">\n");
        printf("<select name=\"login\">\n");
        foreach ($authenticator->authenticators(true) as $key => $obj) {
            printf("<option value=\"%s\" title=\"%s\">%s</option>\n", $key, $obj->description, $obj->name);
        }
        printf("</select>\n");
        printf("<input type=\"submit\" value=\"Login\">\n");
        printf("</form>\n");
    }
} catch (Exception $exception) {
    die(sprintf("Exception: %s", $exception));
}

请参阅示例目录以获取功能完整的代码。访问项目页面获取更多信息。