如何使用Laravel Passport和密码授予令牌?

Joh*_*Ace 15 php oauth laravel laravel-passport

我刚刚阅读了https://laravel.com/docs/5.6/passport文档,我有些疑惑,希望有人可以帮助我:

首先,在某些上下文中,我想使用Passport作为为我的移动应用程序(第一方应用程序)提供Oauth身份验证的方法.

  1. 当我使用时,php artisan passport:client --password我会找回客户端ID和客户端密钥.这个值是否必须在我的应用上修复?例如,将它们存储为硬编码或"设置"文件?如果不应存储这些值,那么它应该如何工作?

  2. 要将用户注册到我的应用程序,我使用:$user->createToken('The-App')->accessToken;我得到的是accessToken将用于发送我的所有请求作为标题(Authorization => Bearer $ accessToken),但究竟什么是"The-App"值?

  3. 要登录我正在使用网址的用户:http://example.com/oauth/token并作为参数发送:

    {"username":"user@email.com","password":"userpassword","grant_type":"password","client_id":1,//我从命令获得的客户端ID(问题1) "client_secret":"嘘"//我从命令得到的客户端秘密(问题1)}

  4. 当我使用前一个端点登录用户时,我得到了一个refresh_token,我读到我可以通过http://example.com/oauth/token/refresh刷新令牌,但我尝试请求刷新我得到错误419,我从csrf验证中删除了url oauth/token/refresh,现在我回来了"message": "Unauthenticated.",我正在提出以下请求:

    Content-Type:x-www-form-urlencoded grant_type:refresh_token refresh_token:-refresh-token //我从命令获得的刷新令牌(问题3)client_id:1 //我从命令获得的客户端ID (问题1)client_secret:嘘//我从命令(问题1)范围得到的客户端秘密:''

我应该使用这个端点吗?鉴于我正在努力开发的应用程序,或者没有必要.

  1. 最后,有很多终点,我从护照,我不认为我会如使用得到的:oauth/clients*,oauth/personal-access-tokens*是有办法从护照公布的端点删除它们?

非常感谢你的帮助!

rkj*_*rkj 27

如果您正在使用自己的api,那么您无需 为用户登录调用 http://example.com/oauth/token,因为您需要在app端存储client_id和client_secret.最好为登录创建一个api,然后您可以检查凭据并生成个人令牌.

public function login(Request $request)
{
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
             $user = Auth::user();
             $token = $user->createToken('Token Name')->accessToken;

            return response()->json($token);
        }
}
Run Code Online (Sandbox Code Playgroud)

最后,我从护照中获得了很多端点,我认为不会使用这些端点,例如:oauth/clients*,oauth/personal-access-tokens*有没有办法将它们从发布的端点中删除护照?

您需要Passport::routes();从AuthServiceProvider中删除并手动只放置所需的护照路由.我想你只需要oauth/token路线.

究竟什么是"The-App"的价值?

如果你检查oauth_access_tokens表它有名字字段.$user->createToken('Token Name')->accessToken;这里存储在名称字段中的"令牌名称".

如何使用Laravel Passport和密码授予令牌?

要生成密码授予令牌,您必须存储client_idclient_secret在应用程序端(不推荐,检查 )并假设您必须重置client_secret然后旧版本应用程序停止工作,这些是问题.要生成密码授予令牌,您必须像在步骤3中提到的那样调用此API.

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);
Run Code Online (Sandbox Code Playgroud)

从中生成令牌 refresh_token

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'refresh_token',
        'refresh_token' => 'the-refresh-token',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);
Run Code Online (Sandbox Code Playgroud)

您也可以查看https://laravel.com/docs/5.6/passport#implicit-grant-tokens.

  • @amansoni211 是的,你是对的,但在这种情况下,我们通常从服务器端调用这些 api。这里的问题是在应用程序端使用“client_id”和“client_secrect”,因此您不能将其存储在客户端。希望它可以让你清楚 (2认同)

Dan*_*ite 5

解决问题 5

最后,有很多终点,我从护照,我不认为我会如使用得到的: oauth/clients*oauth/personal-access-tokens*是有办法从护照公布的端点删除它们?


Passport::routes($callback = null, array $options = [])采用可选$callback函数和可选$options参数。

回调函数接受一个$router参数,然后您可以从中选择要安装的路由,如下所示AuthServiceProvider.php,启用更精细的配置:

Passport::routes(function ($router) {
    $router->forAccessTokens();
    $router->forPersonalAccessTokens();
    $router->forTransientTokens();
});

Passport::tokensExpireIn(Carbon::now()->addMinutes(10));

Passport::refreshTokensExpireIn(Carbon::now()->addDays(10));
Run Code Online (Sandbox Code Playgroud)

这样我们只创建我们需要的护照路线。

forAccessTokens(); 使我们能够创建访问令牌。
forPersonalAccessTokens(); 使我们能够创建个人令牌,尽管我们不会在本文中使用它。最后, forTransientTokens(); 创建用于刷新令牌的路由。

如果您运行,php artisan route:list您可以看到 Laravel Passport 安装的新端点。

| POST | oauth/token         | \Laravel\Passport\Http\Controllers\AccessTokenController@issueToken
| POST | oauth/token/refresh | \Laravel\Passport\Http\Controllers\TransientTokenController@refresh
Run Code Online (Sandbox Code Playgroud)