xinix-technology / bono-auth
Bono Auth
2.0.2
2016-05-20 11:18 UTC
Requires
- xinix-technology/bono: ~2.2.0
Requires (Dev)
README
使用Bono框架实现授权和认证的解决方案。
设置
要使用bono-auth,您需要在config.php中设置bono,使用bono-auth中间件。
'bono.middlewares' => array( '\\Xinix\\BonoAuth\\Middleware\\AuthMiddleware' => array( ... ), ),
bono-auth有两个驱动程序可以使用,每个驱动程序都有不同的配置
Xinix\BonoAuth\Driver\NormAuth
'bono.middlewares' => array( '\\Xinix\\BonoAuth\\Middleware\\AuthMiddleware' => array( 'driver' => '\\Xinix\\BonoAuth\\Driver\\NormAuth' ), ),
Xinix\BonoAuth\Driver\OAuth
'bono.middlewares' => array( '\\Xinix\\BonoAuth\\Middleware\\AuthMiddleware' => array( 'driver' => '\\Xinix\\BonoAuth\\Driver\\OAuth', 'debug' => true, // enable or disable debug 'baseUrl' => 'http://to.your.oauth.provider', 'authUrl' => '/oauth/auth', // URI to access auth 'tokenUrl' => '/oauth/token', // URI to get token 'revokeUrl' => '/oauth/revoke', // URI to revoke auth 'clientId' => '*the client id*', 'clientSecret' => '*the client secret*', 'redirectUri' => \Bono\Helper\URL::site('/login'), // application redirect url 'scope' => 'user', ), ),
上述配置将启用默认的bono-auth。默认值将阻止访客访问所有页面。用户登录后,他将能够访问所有可用的页面。
授权
要授权/取消授权特定页面,您需要编写代码来注册“auth.authorize”过滤器,该过滤器在系统调用的中间件之前运行。通常您可以将这些代码放在提供者文件中。代码将具有以下形式
$app->filter('auth.authorize', function ($options) { // something to do return $allowed; });
返回值
如果返回值满足以下条件之一
- True (bool),URL已授权
- False (bool),URL未授权
- (原始参数),bono-auth将为您决定授权,默认情况下,登录用户有权访问,访客用户无权访问
参数
该过滤器接受单个参数 $options。由于Bono Filter的本质,$options将是相同上下文名称的先前运行的函数的返回值。$options的第一个值将是URI字符串(或包含URI字符串的关联数组)。
如果 $options 是布尔值,则表示之前的过滤器函数已经处理了URI,您可以通过添加if语句来跳过当前过滤器函数。
$app->filter('auth.authorize', function ($options) { if (is_bool($options)) { return $options; } // something to do });