指导我使用 phpleague 库实现 Oauth2 PHP 服务器

2024-03-12

我在用Slim Framework With Eloquent ORM https://packagist.org/packages/illuminate/database。尝试实施https://github.com/thephpleague/oauth2-server https://github.com/thephpleague/oauth2-server但我完全困惑如何做到这一点。使用 Composer 添加此内容后,我使用此包中提供的 sql 文件创建了数据库。

现在建议实现存储接口 http://oauth2.thephpleague.com/implementing-storage-interfaces/。我不想这样做,所以我只是复制了示例文件夹中找到的存储类。我想它们应该可以工作,因为我使用相同的数据库,对吧?

此外还不清楚如何最初为数据库播种。这是我正在尝试的路由器password http://oauth2.thephpleague.com/authorization-server/resource-owner-password-credentials-grant/ method.

$server = new \League\OAuth2\Server\AuthorizationServer;

$server->setSessionStorage(new SessionStorage);
$server->setAccessTokenStorage(new AccessTokenStorage);
$server->setClientStorage(new ClientStorage);
$server->setScopeStorage(new ScopeStorage);

$passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant();
$passwordGrant->setVerifyCredentialsCallback(function ($username, $password) {
    // implement logic here to validate a username and password, return an ID if valid, otherwise return false
    return 1;
});

$server->addGrantType($passwordGrant);

$app->post('/token',function() use ($server,$app){
    try{
        $response = $server->issueAccessToken();

        $res = $app->response();
        $res['Content-Type'] = 'application/json';

        $res->body(json_encode($response));

    } catch (\Exception $e) {

       var_dump($e);
    }

});

我对所发生的事情感到非常沮丧。 这会引发以下异常。 [我已经添加了范围ok in db]

object(League\OAuth2\Server\Exception\InvalidScopeException)[82]
  public 'httpStatusCode' => int 400
  public 'errorType' => string 'invalid_scope' (length=13)
  public 'serverShouldRedirect' => boolean true
  protected 'message' => string 'The requested scope is invalid, unknown, or malformed. Check the "ok" scope.' (length=76)
  private 'string' (Exception) => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string 'C:\wamp\www\linkshare\vendor\league\oauth2-server\src\Grant\AbstractGrant.php' (length=77)
  protected 'line' => int 163
  private 'trace' (Exception) => 
    array (size=11)
      0 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\league\oauth2-server\src\Grant\PasswordGrant.php' (length=77)
          'line' => int 130
          'function' => string 'validateScopes' (length=14)
          'class' => string 'League\OAuth2\Server\Grant\AbstractGrant' (length=40)
          'type' => string '->' (length=2)
          'args' => 
            array (size=2)
              ...
      1 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\league\oauth2-server\src\AuthorizationServer.php' (length=77)
          'line' => int 330
          'function' => string 'completeFlow' (length=12)
          'class' => string 'League\OAuth2\Server\Grant\PasswordGrant' (length=40)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      2 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\index.php' (length=31)
          'line' => int 67
          'function' => string 'issueAccessToken' (length=16)
          'class' => string 'League\OAuth2\Server\AuthorizationServer' (length=40)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      3 => 
        array (size=2)
          'function' => string '{closure}' (length=9)
          'args' => 
            array (size=0)
              ...
      4 => 
        array (size=4)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Route.php' (length=53)
          'line' => int 462
          'function' => string 'call_user_func_array' (length=20)
          'args' => 
            array (size=2)
              ...
      5 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Slim.php' (length=52)
          'line' => int 1326
          'function' => string 'dispatch' (length=8)
          'class' => string 'Slim\Route' (length=10)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      6 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Middleware\Flash.php' (length=64)
          'line' => int 85
          'function' => string 'call' (length=4)
          'class' => string 'Slim\Slim' (length=9)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      7 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Middleware\MethodOverride.php' (length=73)
          'line' => int 92
          'function' => string 'call' (length=4)
          'class' => string 'Slim\Middleware\Flash' (length=21)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      8 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Middleware\PrettyExceptions.php' (length=75)
          'line' => int 67
          'function' => string 'call' (length=4)
          'class' => string 'Slim\Middleware\MethodOverride' (length=30)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      9 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\vendor\slim\slim\Slim\Slim.php' (length=52)
          'line' => int 1271
          'function' => string 'call' (length=4)
          'class' => string 'Slim\Middleware\PrettyExceptions' (length=32)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              ...
      10 => 
        array (size=6)
          'file' => string 'C:\wamp\www\linkshare\index.php' (length=31)
          'line' => int 131
          'function' => string 'run' (length=3)
          'class' => string 'Slim\Slim' (length=9)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)

正确理解和使用 OAuth 2.0 非常具有挑战性。事实上,OAuth 2.0的领先经过多年的开发后放弃了该协议 http://hueniverse.com/2012/07/26/oauth-2-0-and-the-road-to-hell/。根据 Eran Hammer(上述首席开发人员)的说法:

与OAuth 1.0相比,2.0规范更加复杂, 互操作性较差、有用性较差、不完整且大多数 重要的是,安全性较差。

需要明确的是,OAuth 2.0 掌握在具有深厚知识的开发人员手中 了解网络安全可能会导致安全 执行。然而,在大多数开发者手中——正如一直以来 过去两年的经验——2.0可能会产生 不安全的实施。

不用说,关于 OAuth 2.0 存在一些争议。 Alex Bilbie 是 PHP League 的 OAuth 2.0-Server 框架的首席开发人员,他似乎是 OAuth 2.0 知识最丰富的开发人员之一,尽管无疑还有更多。尽管如此,请记住 Hammer 最大的抱怨之一 - 缺乏互操作性和完整性 - 您可能希望在 OAuth 2.0 实现中寻找以下内容:

  • 积极发展
  • 完全符合OAuth 2.0

我个人使用并推荐 Alex Bilbie 的 OAuth 2.0-Server,它现在包含 MAC 不记名令牌,旨在完全合规。它也正在积极开发中。

那么,这对您的项目意味着什么?阅读规格。我们使用的软件包声称完全合规,这意味着您最好的资源是OAuth 2.0 的规范 http://oauth.net/2/本身。下面还有不错的文档PHP联盟网站 http://oauth2.thephpleague.com/这可以帮助您完成这个特定的实施。

话虽这么说,您可能缺少客户/用户组合的范围。 OAuth2.0 的结构方式是,您的用户需要接受客户端请求的范围。这意味着您的“范围”需要链接到其他表中。如果“OK”未被用户(在数据库中)批准,那么它将不会被批准。

Edit

听起来范围对你来说不是问题。这个场所不太适合进行故障排除,因此我建议您确保拥有您选择使用的任何框架的最新版本,并向开发人员报告任何错误(对于 PHPLeague,它是通过 Github 进行的)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

指导我使用 phpleague 库实现 Oauth2 PHP 服务器 的相关文章

  • 很好地阻止了齿轮工人

    我有许多 Gearman 工作人员不断运行 保存用户页面浏览记录等内容 偶尔 我会更新 Gearman 工作人员使用的 PHP 代码 为了让工作人员切换到新代码 我杀死并重新启动了工作人员的 PHP 进程 有什么更好的方法来做到这一点 据推
  • 如何将 php Web 应用程序转换为桌面应用程序并保留数据库 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我们有一个用 PHP 开发的 Web 应用程序 但大多数客户并没有一直连接到互联网 那么 有没有办法将应用程序转换为桌面应用程序 以便
  • PMA 4.5.2.0 file_exists():open_basedir 限制生效

    从 PPA 在我的 Ubuntu 服务器上安装 phpMyAdmin 后 https launchpad net nijel archive ubuntu phpmyadmin https launchpad net nijel archi
  • Laravel 验证:存在附加列条件 - 自定义验证规则

    在 Laravel 中指定存在验证规则时 是否有一种方法可以引用另一个字段 我希望能够说输入 a 必须存在于表 a 中 输入 b 必须存在于表 b 中 并且表 b 中列 x 的值必须等于输入 a 最好通过例子来解释 public rules
  • 我如何知道请求是否来自 flash swf?

    我有一个用 flash 开发的应用程序 我需要访问一些 php 文件 因此 如果访问来自 swf 则 php 文件会返回一些数据 如何判断请求是否来自Flash 无需将 get post 变量传递给 php 可能是用户代理 推荐人 请记住
  • 使用 PHP/COM/ADSI/LDAP 更改 AD 密码

    我已经被这个问题困扰了好几天了 我尝试了各种解决方案均无济于事 请帮忙 Problem 我们有两个域控制器 它们不属于我们的管理范围 我们能够通过端口 389 上的 LDAP 进行连接 但无法通过端口 636 安全连接 我们正在开发一个系统
  • Ubuntu 18.04升级后php7.2-curl无法安装

    今天从 16 04 升级到 18 04do release upgrade d 在升级过程中 我被告知一些软件包将被删除 其中包括 删除 libperl5 22 lxc common perl modules 5 22 php imagic
  • Twitter API 是否自动缩短 URL

    我知道这个问题之前已经被问过 但是我发现的答案有点旧 我知道Twitter 最近已在所有帖子中强制使用 t co xyz 服务 https support twitter com articles 109623 faqs about twi
  • PHP 如果不存在,则从字符串中删除 ','

    我正在运行这段代码 stmt pdo conn gt prepare SELECT from admin where support emails support emails and logged logged and disabled
  • Facebook 中用户的时区是如何编码的

    我需要检查用户的时区 但我找不到它的真正定义 参考API http developers facebook com docs reference api user says 用户的时区与 UTC 的偏移量 现在在维基百科上这些是可能的时区
  • 如何计算加权平均值?

    我的语言是PHP 但是算法应该是相当通用的 我有一个关联数组 比方说 评级和评级次数 ratings array 1 gt 1 2 gt 3 3 gt 6 4 gt 3 5 gt 3 这相当于 1 2 2 2 3 3 3 3 3 3 4 4
  • 如何读取 XML 文件并从中获取值以在 PHP 编码的 HTML 页面中显示

    我有一个 XML 文件 其中有一些重复的标签 其中包含不同的值 我需要获取这些值并显示在我的网页中 请帮助我得到这个 如果您使用 PHP5 可以查看 SimpleXML 您可以在这里找到介绍教程 http www w3schools com
  • 如何在 Laravel 代码中使用 Artisan 命令?

    如何在我的 php caode Ex 中使用 Artisan 命令行 php artisan version to 您可以从控制器调用 Artisan 命令 如下所示 calling of migrate install Artisan c
  • 使用php将文本文件转换为xml?

    data txt ha15rs 250 home2 gif 2 ha36gs 150 home3 gif 1 ha27se 300 home4 gif 4 ha4678 200 home5 gif 5 我想使用 php 使用 simplex
  • 在 Apache 服务器上将特定的 .htm 页面处理为 .php [重复]

    这个问题在这里已经有答案了 我正在为 Apache 服务器编程 并且只需要将一个特定的 html 页面 例如 first htm 作为 PHP 脚本进行处理 可以设置吗 SetHandler http httpd apache org do
  • PHP 日志文件颜色

    我正在编写一个 PHP 日志文件类 但我想为写入文件的行添加颜色 我遇到的问题是颜色也会改变终端的颜色 我想要实现的是仅更改写入日志文件的行的颜色 class logClass extends Singleton private funct
  • Laravel - 覆盖模型 ID

    我正在开发电子书管理系统 我使用 UUID 而不是自动递增整数主键 它工作得很好 protected static function boot parent boot static creating function model model
  • 无法与站点通信以检查致命错误

    无法与站点通信以检查致命错误 因此 PHP 更改已恢复 您需要通过其他方式上传 PHP 文件更改 例如使用 SFTP 有什么解决办法 我正在 WordPress 中编辑头文件 遇到这个问题 尝试这个 我有同样的问题并决定调查一下 更改 wp
  • 如何获取 URL 中未知的 $_GET 值的数组?

    我正在使用一个在线购物车 它接受订购的商品并通过 URL 将其唯一 ID 传递到 process php 页面 所以 URL 看起来像这样 process php code 1 231 code 2 532 code 3 342 Anoth
  • 如何显示 PHP 对象

    我有这样的代码 dataRecord1 client gt GetRecord token table filter echo pre print r dataRecord1 echo pre foreach dataRecord1 gt

随机推荐