marcelbonnet/slim-auth
Composer 安装命令:
composer require marcelbonnet/slim-auth
包简介
Authorization and authentication for the Slim Framework using ZF2 Authentication and Acl components
README 文档
README
This project uses parts of Zend Framework, like Zend Auth classes, Zend ACL and Zend config.
My goal was to build a reusable Middleware to authenticate through LDAP and/or RDBMS.
Install
$ composer require marcelbonnet/slim-auth
Bundled Mechanism
- Authentication through LDAP (see sample config for LDAP and AD)
- Authentication through RDBMS
- Authorization based on user roles kept in a RDBMS' table.
RDBMS Mechanism
Agnostic table design. The only thing slim-auth needs to know is where users and roles are stored , using an instance of Doctrine ORM's EntityManager.
Requirements
- Slim Framework v. 3.x
- PHP >= 5.6
- Doctrine ORM >= 2.5
To see all dependencies: https://packagist.org/packages/marcelbonnet/slim-auth
How To (slim-auth min version 2.0.0)
1.x not compatible.
Dao
This package suppose you have a User 0..+ Role(s). Here an example design (use whatever attribute names you want):
class User { protected $username; protected $passwordHash; /** @OneToMany(targetEntity="Role", ...) */ protected $roles; } class Role { /** The role name @var string */ protected $role; /** @ManyToOne(targetEntity="User") */ protected $user; }
index.php
use marcelbonnet\Slim\Auth\ServiceProvider\SlimAuthProvider; use Zend\Authentication\Storage\Session as SessionStorage; use marcelbonnet\Slim\Auth\Middleware\Authorization; use marcelbonnet\Slim\Auth\Handlers\RedirectHandler; use marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter; use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; use \Slim\Http\Request as SlimHttpRequest; use \Slim\Http\Response as SlimHttpResponse; require_once 'vendor/autoload.php'; /* **************************************************************************** * Slim App and Config * **************************************************************************** */ $config = require '../conf/config.php'; $app = new \Slim\App($config); // Fetch DI Container $container = $app->getContainer(); /* **************************************************************************** * Authentication/Authorization * **************************************************************************** */ $acl = new Acl(); //ACLed Slim Route $container['router'] = new \marcelbonnet\Slim\Auth\Route\AuthorizableRouter(null, $acl); $container['acl'] = $acl; $adapterOptions = []; //if you want auth to be valid if some column exists with an expected value: // $adapterOptions = [ // 'checkUserIsActivated' => 'my_column_in_user_table', // 'userIsActivatedFlag' => true // ]; $adapter = new marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter( '/some/file.conf', //LDAP config or NULL if not using LDAP $myEntityManager, //an Doctrine's Entity Manager instance "\Your\Project\Dao\Role", //Role class "role", //Role's class role attribute "user", //Role's class user attribute (the @ManyToOne attrib) "\Your\Project\Dao\User", //User class "username", //User name attribute "passwordHash", //password (as a hash) attribute marcelbonnet\Slim\Auth\Adapter\LdapRdbmsAdapter::AUTHENTICATE_RDBMS, //auth method: LdapRdbmsAdapter::AUTHENTICATE_RDBMS | LdapRdbmsAdapter::AUTHENTICATE_LDAP 10, //a hash factor PASSWORD_DEFAULT, //hash algorithm $adapterOptions //if needed ); $container["authAdapter"] = $adapter; $slimAuthProvider = new SlimAuthProvider(); $slimAuthProvider->register($container); $app->add(new Authorization( $container["auth"], $acl, new RedirectHandler("auth/notAuthenticated", "auth/notAuthorized") )); # checks: #$username=(is_array(@$c["auth"]->getStorage()->read()))? @$c["auth"]->getStorage()->read()["username"] : @$c["auth"]->getStorage()->read(); #$userRoles=(is_array(@$c["auth"]->getStorage()->read()))? @$c["auth"]->getStorage()->read()["role"] : array(); /** Example Routes: you must set allowed Roles (as one string or as an array or string roles) for each route. */ $app->get('/', 'My\Controller:home' )->setName("home")->allow(Acl::MEMBER); $app->get('/home', function (SlimHttpRequest $request, SlimHttpResponse $response, $args) use($container) { $container->get('router')->getNamedRoute('home')->run($request, $response); })->allow(Acl::MEMBER); $app->get('/hello[/{name}]', 'My\Controller:sayHello')->setName('hello')->allow([Acl::GUEST, Acl::MEMBER]); $app->get('/protected', 'My\Controller:callProtectedResource')->setName('protected')->allow(Acl::ADMIN); $app->run();
Now your ACL class should look like:
class Acl extends SlimAuthAcl { const GUEST = "guest"; const ADMIN = "admin"; const MEMBER = "member"; public function __construct() { // APPLICATION ROLES $this->addRole(self::GUEST); $this->addRole(self::MEMBER, self::GUEST); /* ************************************** * WARNING: ALLOW ALL: * ************************************** */ $this->addRole(self::ADMIN); $this->allow(self::ADMIN); } }
How it looks in MySQL
mysql> SELECT * FROM core__users; +----+--------------+--------------------------------------------------------------+ | id | username | passwordHash | +----+--------------+--------------------------------------------------------------+ | 2 | marcelbonnet | $2y$15$9b9Vb5K/Rcg.s6Gjn0cpnu4iAhRdbWA0lIxqzf5mLl81WW.qYtXzK | +----+--------------+--------------------------------------------------------------+ mysql> SELECT * FROM core__user_roles; +----+------------+--------+ | id | fk_user_id | role | +----+------------+--------+ | 5 | 2 | admin | | 3 | 2 | member | +----+------------+--------+
History
This project started as a modified version of a development branch from jeremykendall/slim-auth.
marcelbonnet/slim-auth 适用场景与选型建议
marcelbonnet/slim-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 187 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「Authentication」 「zend」 「zf2」 「Zend Framework」 「auth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marcelbonnet/slim-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marcelbonnet/slim-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marcelbonnet/slim-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Polyfill for mb_ereg(), mb_eregi(), mb_ereg_match(), and mb_ereg_replace*() functions; primary use case is for mbstring on Windows 7.4+
统计信息
- 总下载量: 187
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-24