如何将 Laravel Passport 与密码授予令牌一起使用?
How to use Laravel Passport with Password Grant Tokens?
我刚刚阅读了 https://laravel.com/docs/5.6/passport 文档,我有一些疑问希望有人能帮助我:
首先,一些上下文,我想使用 Passport 为我的移动应用程序(first-party 应用程序)提供 Oauth 身份验证。
当我使用 php artisan passport:client --password
时,我得到了一个客户端 ID 和一个客户端密码。这个值是否必须固定在我的应用程序上?例如将它们存储为硬编码或存储为 "settings" 文件?如果不应存储这些值,那么它应该如何工作?
要将用户注册到我的应用程序,我使用:$user->createToken('The-App')->accessToken;
我知道 accessToken 将是用于发送我所有请求的 Header(授权=> Bearer $accessToken) 但 "The-App" 的价值到底是什么?
为了登录用户,我使用 URL:http://example.com/oauth/token 并作为参数发送:
{
"username": "user@email.com",
"password": "userpassword",
"grant_type": "password",
"client_id": 1, // 我从命令中得到的客户端 ID(问题 1)
"client_secret": "Shhh" // 我从命令中得到的客户端密钥(问题 1)
}
当我使用之前的端点登录用户时,我得到了 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: the-refresh-token // 我从命令中获得的刷新令牌(问题 3)
client_id: 1 // 我从命令中得到的客户端 ID(问题 1)
client_secret: Shhh // 我从命令中得到的客户端秘密(问题 1)
范围:''
我应该使用这个端点吗?鉴于我正在尝试开发的应用程序,或者没有必要。
- 最后,我从 passport 获得了很多我认为不会使用的端点,例如:
oauth/clients*
、oauth/personal-access-tokens*
有没有办法将它们从passport 发布的端点?
非常感谢您的帮助!
如果您使用自己的 api,则无需调用 http://example.com/oauth/token
用于用户登录,因为您需要在应用端存储 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);
}
}
Finally, there are a lot of endpoints that I get from passport that I
don't think I will use for example: oauth/clients*,
oauth/personal-access-tokens* is there a way to remove them from the
endpoints published by passport?
您需要从 AuthServiceProvider 中删除 Passport::routes();
并手动仅放置必需的通行证路由。我想你只需要 oauth/token
路线。
what exactly is "The-App" value for?
如果你检查 oauth_access_tokens table 它有名称字段。 $user->createToken('Token Name')->accessToken;
这里 "Token Name" 存储在名称字段中。
How to use Laravel Passport with Password Grant Tokens?
要生成密码授予令牌,您必须在应用程序端存储 client_id
和 client_secret
(不推荐,检查 this)并假设您是否必须重置 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);
Generate token from 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);
你也可以看看这个https://laravel.com/docs/5.6/passport#implicit-grant-tokens
解决问题 5
Finally, there are a lot of endpoints that I get from passport that I don't think I will use for example: oauth/clients*
, oauth/personal-access-tokens*
is there a way to remove them from the endpoints published by passport?
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));
这样我们只创建我们需要的护照路线。
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
我刚刚阅读了 https://laravel.com/docs/5.6/passport 文档,我有一些疑问希望有人能帮助我:
首先,一些上下文,我想使用 Passport 为我的移动应用程序(first-party 应用程序)提供 Oauth 身份验证。
当我使用
php artisan passport:client --password
时,我得到了一个客户端 ID 和一个客户端密码。这个值是否必须固定在我的应用程序上?例如将它们存储为硬编码或存储为 "settings" 文件?如果不应存储这些值,那么它应该如何工作?要将用户注册到我的应用程序,我使用:
$user->createToken('The-App')->accessToken;
我知道 accessToken 将是用于发送我所有请求的 Header(授权=> Bearer $accessToken) 但 "The-App" 的价值到底是什么?为了登录用户,我使用 URL:http://example.com/oauth/token 并作为参数发送:
{ "username": "user@email.com", "password": "userpassword", "grant_type": "password", "client_id": 1, // 我从命令中得到的客户端 ID(问题 1) "client_secret": "Shhh" // 我从命令中得到的客户端密钥(问题 1) }
当我使用之前的端点登录用户时,我得到了 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: the-refresh-token // 我从命令中获得的刷新令牌(问题 3) client_id: 1 // 我从命令中得到的客户端 ID(问题 1) client_secret: Shhh // 我从命令中得到的客户端秘密(问题 1) 范围:''
我应该使用这个端点吗?鉴于我正在尝试开发的应用程序,或者没有必要。
- 最后,我从 passport 获得了很多我认为不会使用的端点,例如:
oauth/clients*
、oauth/personal-access-tokens*
有没有办法将它们从passport 发布的端点?
非常感谢您的帮助!
如果您使用自己的 api,则无需调用 http://example.com/oauth/token 用于用户登录,因为您需要在应用端存储 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);
}
}
Finally, there are a lot of endpoints that I get from passport that I don't think I will use for example: oauth/clients*, oauth/personal-access-tokens* is there a way to remove them from the endpoints published by passport?
您需要从 AuthServiceProvider 中删除 Passport::routes();
并手动仅放置必需的通行证路由。我想你只需要 oauth/token
路线。
what exactly is "The-App" value for?
如果你检查 oauth_access_tokens table 它有名称字段。 $user->createToken('Token Name')->accessToken;
这里 "Token Name" 存储在名称字段中。
How to use Laravel Passport with Password Grant Tokens?
要生成密码授予令牌,您必须在应用程序端存储 client_id
和 client_secret
(不推荐,检查 this)并假设您是否必须重置 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);
Generate token from
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);
你也可以看看这个https://laravel.com/docs/5.6/passport#implicit-grant-tokens
解决问题 5
Finally, there are a lot of endpoints that I get from passport that I don't think I will use for example:
oauth/clients*
,oauth/personal-access-tokens*
is there a way to remove them from the endpoints published by passport?
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));
这样我们只创建我们需要的护照路线。
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