reekoheek/bono-auth

此包已被废弃且不再维护。作者建议使用 xinix-technology/bono-auth 包。

Bono Auth

2.0.2 2016-05-20 11:18 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:29:39 UTC


README

您使用 Bono 框架进行授权和认证的解决方案。

设置

要使用 bono-auth,您必须使用 config.php 中的 bono-auth 中间件来设置 bono。

'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 过滤器的性质,$options 将是同一名称的先前运行函数的返回值。$options 的第一个值将是 URI 字符串(或包含 URI 字符串的关联数组)。

如果 $options 是_bool,则表示之前的过滤器函数已经处理了 URI,您可以通过添加 if 语句来跳过当前的过滤器函数。

$app->filter('auth.authorize', function ($options) {
  if (is_bool($options)) {
    return $options;
  }
  // something to do
});