【问题标题】:Own users table instead of oauth_clients with OAuth2-server for Lumen拥有用户表而不是 oauth_clients 和 OAuth2-server for Lumen
【发布时间】:2016-08-23 10:25:18
【问题描述】:

我已经使用Tutorial 在我的 Lumen API 中成功实现了 OAuth2 服务器。本教程使用OAuth2 server for Laravel + Lumen

很遗憾,documentation part of creating own grants 还不存在。

在我的数据库中现在有 oauth_clients 表。但我也有自己的表users。还有一些关于用户的更多信息。所有实体都引用此表。

现在我想检查 users 表中 oauth_clients 表的凭据。这可能吗?

【问题讨论】:

    标签: php oauth oauth-2.0 lumen lumen-5.2


    【解决方案1】:

    oauth_clients 表仅用于管理 client_id 和 client_secret(以及范围等)。

    您需要使用 PasswordGrant 而不是 ClientCredentialsGrant 来验证用户身份(从而处理 users 表和 oauth_clients)。

    更多详情请见:https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/docs/authorization-server/password.md

    PS:在 Lumen 中,您可能会遇到以下异常:

      if (Auth::once($credentials)) {
          return Auth::user()->id;
      }
    

    所以你需要用类似这样的方式重写它:

    $user = User::where('username', strtolower($username))->first();
    
    if (!isset($user)) {
        return null;
    }
    
    if (app('hash')->check($password, $user->getAuthPassword())) {
        return $user->id;
    }
    
    return null;
    

    注意:您仍然需要定义至少一个 client_id/client_secret 组合的 oauth_clients 表(请注意,secret 不是散列,应该是普通的)

    设置完成后,您将获得类似的访问令牌(不完全是这样,取决于您的实现):

    curl -X POST http://yourapp/access_token -d "grant_type=password&client_id=test&client_secret=testsecret&username=testuser&password=testpass"
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      相关资源
      最近更新 更多