当前位置: 首页>>代码示例>>PHP>>正文


PHP AuthenticationService::addListener方法代码示例

本文整理汇总了PHP中Zend\Authentication\AuthenticationService::addListener方法的典型用法代码示例。如果您正苦于以下问题:PHP AuthenticationService::addListener方法的具体用法?PHP AuthenticationService::addListener怎么用?PHP AuthenticationService::addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Zend\Authentication\AuthenticationService的用法示例。


在下文中一共展示了AuthenticationService::addListener方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: __construct

use Zend\Authentication\AuthenticationService;
use Zend\EventManager\EventManager;
class AuditLog
{
    private $log;
    /**
     * AuditLog constructor.
     * @TODO add dependency for a psr logger
     * @param $log
     */
    public function __construct($log)
    {
        $this->log = $log;
    }
    public function onAuthenticationFailed(\Zend\Authentication\Event\Authenticate $event)
    {
        $this->log->warn(sprintf('Authenication Failure for (%s) from (%s)', $event->getParam('identity'), $event->getParam('ip')));
    }
}
$auditLog = new AuditLog(new stdClass());
$callback = function ($identity, $credential) {
    if ($identity === $credential) {
        return new \Zend\Stdlib\ArrayObject(['identity' => $identity, 'credential' => $credential]);
    }
    throw new \Exception('Authentication failed');
};
$adapter = new \Zend\Authentication\Adapter\Callback($callback);
$authService = new AuthenticationService(null, $adapter);
$authService->addListener('AuthenticationFailed', [$auditLog, 'onAuthenticationFailed'], -1);
$authService->authenticate(['ip' => '127.0.0.1', 'identity' => 'test', 'credential' => 'failed']);
开发者ID:carnage,项目名称:zend-authentication,代码行数:30,代码来源:AuditLog.php

示例2: onAuthenticationFailed

            }
        }
    }
    public function onAuthenticationFailed(\Zend\Authentication\Event\Authenticate $event)
    {
        $identity = $event->getParam('identity');
        if ($identity !== null) {
            $this->authFails[$identity]++;
        }
        $ip = $event->getParam('ip');
        if ($ip !== null) {
            $this->authFails[$ip]++;
        }
    }
}
$firewall = new Firewall();
$callback = function ($identity, $credential) {
    if ($identity === $credential) {
        return new \Zend\Stdlib\ArrayObject(['identity' => $identity, 'credential' => $credential]);
    }
    throw new \Exception('Authentication failed');
};
$adapter = new \Zend\Authentication\Adapter\Callback($callback);
$authService = new AuthenticationService(null, $adapter, 10);
$authService->addListener('Authenticate', [$firewall, 'onAuthenticate'], -1);
$authService->addListener('AuthenticationFailed', [$firewall, 'onAuthenticationFailed'], -1);
$authService->authenticate(['ip' => '127.0.0.1', 'identity' => 'test', 'credential' => 'failed']);
$authService->authenticate(['ip' => '127.0.0.1', 'identity' => 'test', 'credential' => 'failed']);
$authService->authenticate(['ip' => '127.0.0.1', 'identity' => 'test2', 'credential' => 'failed']);
// result = failure to many attempts
$authService->authenticate(['ip' => '127.0.0.1', 'identity' => 'test2', 'credential' => 'failed']);
开发者ID:carnage,项目名称:zend-authentication,代码行数:31,代码来源:BruteForceProtection.php

示例3: TwoFAListener

                if (isset($twoFactorResponse)) {
                    if ($prevResult !== null && isset($prevResult->twoFactorToken) && $twoFactorResponse === $prevResult->twoFactorToken) {
                        $result = new \Zend\Authentication\Result(\Zend\Authentication\Result::SUCCESS, $identity);
                        $event->setResult($result);
                        return $result;
                    }
                }
                $result = new \Zend\Authentication\Result(-4, $identity, 'Requires 2 factor Auth');
                $result->twoFactorToken = 'efg456';
                //generate randomly
                $event->setResult($result);
                $event->stopPropagation();
                return $result;
            }
        }
        return $result;
    }
}
$twoFaListener = new TwoFAListener();
$callback = function ($identity, $credential) {
    if ($identity === $credential) {
        return new \Zend\Stdlib\ArrayObject(['identity' => $identity, 'credential' => $credential, 'do2fa' => true]);
    }
    throw new \Exception('Authentication failed');
};
$adapter = new \Zend\Authentication\Adapter\Callback($callback);
$authService = new AuthenticationService(null, $adapter);
$authService->addListener('Authenticate', [$TwoFAlistener, 'onAuthenticate'], 20);
$authService->authenticate(['identity' => 'test', 'credential' => 'test']);
//result success with test identity
$authService->authenticate(['twoFactorResponse' => 'efg456']);
开发者ID:carnage,项目名称:zend-authentication,代码行数:31,代码来源:2fa.php


注:本文中的Zend\Authentication\AuthenticationService::addListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。