我刚刚阅读了https://laravel.com/docs/5.6/passport文档,我有些疑惑,希望有人可以帮助我:
首先,在某些上下文中,我想使用Passport作为为我的移动应用程序(第一方应用程序)提供Oauth身份验证的方法。
php artisan passport:client --password
时,我得到了客户端ID和客户端密钥。这个值是否必须在我的应用上修复?例如,将它们存储为硬编码或“设置”文件?如果不应存储这些值,那么它应该如何工作?$user->createToken('The-App')->accessToken;
我得到的是accessToken将用于发送我的所有请求作为标题(Authorization => Bearer $ accessToken),但究竟什么是“The-App”值?"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)范围得到的客户端秘密:''我应该使用这个端点吗?鉴于我正在努力开发的应用程序,或者没有必要。
oauth/clients*
,oauth/personal-access-tokens*
有没有办法将它们从护照上发布的端点中删除?非常感谢你的帮助!
如果您正在使用自己的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);
}
}
最后,我从护照中获得了许多端点,我认为不会使用这些端点,例如:oauth / clients *,oauth / personal-access-tokens *有没有办法将它们从发布的端点中删除护照?
您需要从AuthServiceProvider中删除Passport::routes();
并手动只放置所需的护照路由。我想你只需要oauth/token
路线。
究竟什么是“The-App”的价值?
如果你检查oauth_access_tokens表它有名字字段。 $user->createToken('Token Name')->accessToken;
这里存储在名称字段中的“令牌名称”。
如何使用Laravel Passport和密码授予令牌?
要生成密码授予令牌,您必须在应用程序端存储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' => '[email protected]',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
从
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
最后,我从护照中得到了许多端点,我认为我不会使用这些端点:
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));
这样我们只创建我们需要的护照路线。
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