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


Guide me implementing Oauth2 PHP server using thephpleague library

我将Slim FrameworkEloquent ORM一起使用。正在尝试实施https://github.com/thephpleague/oauth2-server但我完全不知道该怎么做。在用composer添加了这个之后,我用这个包中提供的sql文件创建了数据库。

现在建议实现存储接口。我不想这么做,所以我只是复制了在示例文件夹中找到的存储类。我想它们应该像我使用相同的数据库一样工作,对吧?

此外,目前还不清楚如何在数据库中进行初始种子设定。这是我正在尝试password方法的路由器。

$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]

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的负责人在为其开发多年后就退出了该协议,这是出了名的

与OAuth 1.0相比2.0规范更为复杂,互操作性较差、有用性较差、不完整性更强更重要的是,不太安全。

需要明确的是,OAuth 2.0掌握在具有深度对网络安全的理解可能会导致实施然而,在大多数开发人员手中——就像过去一样过去两年的经验2.0可能会产生不安全的实现。

因此,不用说,关于OAuth 2.0存在一些争论。PHP联盟的OAuth 2.0服务器框架的首席开发人员Alex Bilbie似乎是OAuth 2.0的知识渊博的开发人员之一,尽管毫无疑问还有更多的开发人员。尽管如此,请记住Hammer的一个更大的抱怨——缺乏互操作性和完整性——您可能希望在OAuth 2.0实现中寻找以下内容:

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

我个人使用并推荐Alex Bilbie的OAuth 2.0服务器,它现在包括MAC承载令牌,旨在完全兼容。它也在积极发展中。

那么,这对你的项目意味着什么呢?阅读说明书。我们使用的包声称是完全兼容的,这意味着您最好的资源是OAuth 2.0的规范本身。PHP联盟网站下也有不错的文档可以帮助您实现这个特定的实现。

也就是说,您可能缺少客户端/用户组合的作用域。按照OAuth2.0的结构方式,用户需要接受客户端请求的作用域。这意味着你的";scopes";需要在其他表中链接。如果";OK";未经用户(在数据库中)批准,则不会批准。

编辑

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

问这个问题已经好几年了,但下面是Slim 3:的OAuth2库的链接

  • https://github.com/chadicus/slim-oauth2-middleware

  • https://packagist.org/packages/chadicus/slim-oauth2-routes