使用 laravel/lumen 中的客户端 ID 和密钥访问 API

Access API using client id and secret key in laravel/lumen

我已经为我的 Web 应用程序创建了一个 API。现在我想授予对世界的访问权限,但在授予访问权限之前,我想要类似 Facebook API、Twitter API、Google API 的机制,它们提供客户端 ID 和密钥。目前,我正在使用 JWT AuthController,用户使用他的凭据和 return 令牌登录,我不希望用户登录。

我希望用户可以使用客户端 ID 和密钥访问我的 API?另一件事是,我将如何为用户创建客户端 ID 和密钥?

是否可以使用 JWT Auth 实现?

有什么帮助吗?

我已经阅读了这篇文章并且很有前途,但是经过几次post它建议使用oauth2,给你:

https://laracasts.com/discuss/channels/lumen/api-authorization-via-public-and-secret-keys

引用:

Just add in the class to your API config.

namespace App\Providers\Guard;

use Dingo\Api\Auth\Provider\Authorization; use Dingo\Api\Routing\Route; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class GuardProvider extends Authorization { /** * Get the providers authorization method. * * @return string */ public function getAuthorizationMethod() { return 'X-Authorization'; }

/**
 * Authenticate the request and return the authenticated user instance.
 *
 * @param \Illuminate\Http\Request $request
 * @param \Dingo\Api\Routing\Route $route
 *
 * @return mixed
 */
public function authenticate(Request $request, Route $route)
{
    $key = $request->header(env('API_AUTH_HEADER', 'X-Authorization'));
    if (empty($key)) $key = $request->input(env('API_AUTH_HEADER', 'X-Authorization'));
    if (empty($key)) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is missing or an invalid authorization header was sent');

    $user = app('db')->select("SELECT * FROM users WHERE users.key = ?", [$key]);
    if (!$user) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is not valid');

    return $user;
} }