devster / uauth
微型PHP HTTP身份验证库
1.1.0
2016-02-13 14:32 UTC
Requires
- php: >=5.3
Requires (Dev)
- kriswallsmith/buzz: dev-master
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2024-08-29 01:51:17 UTC
README
微型PHP HTTP身份验证库
安装
composer require devster/uauth "1.*"
或者需要单个文件
require_once 'src/Basic.php'; use Uauth\Basic;
用法
HTTP基础认证
简单用例
require_once 'vendor/autoload.php'; // Here is the most simple usecase $basic = new \Uauth\Basic("Secured Area", ['john' => 'd0e!', 'jon' => '5n0w']); $basic->auth(); // All code below is secured by HTTP basic, and you can access the user echo "Welcome ", $basic->getUser();
更复杂的用例
$basic = new \Uauth\Basic("Secured Area"); $basic // Implement your own user verification system. // The callable must return true if user is allowed, false if not ->verify(function ($username, $password) use ($db) { $user = $db->findUser($username); return $user->password == $password; }) // this code is executed if the login modal is cancelled // or if the user is not verified ->deny(function () { echo "This text appears because you hit the cancel button"; }) ; $basic->auth(); echo "Welcome ", $basic->getUser(), ", you password is ", $basic->getPassword();
Silex集成
$app = new Silex\Application(); $app['allowedUsers'] = ['jon' => '5n0w']; // Create your silex middleware $httpBasicAuth = function () use ($app) { $basic = new \Uauth\Basic("Secured Silex Area", $app['allowedUsers']); $basic->auth(); // we save the current user $app['user'] = $basic->getUser(); }; // And now you can use your middleware where you see fit // Protect the entire app $app->before($httpBasicAuth); // Or protect a collection of routes $blog = $app['controllers_factory']; $blog->before($httpBasicAuth); $blog->get('/', function () { return 'Blog home page'; }); $app->mount('/private_blog', $blog); // Or protect a single route $app->get('/hello', function () use ($app) { return 'Hello '.$app->escape($app['user']); })->before($httpBasicAuth); $app->run();
测试
如果你想使用内置的PHP Web服务器运行测试,你需要php >= 5.4。
php -S localhost:8080 -t tests/server
vendor/bin/phpunit
许可证
本项目采用MIT许可证授权