如何检索 Laravel Passport 访问令牌有效期或日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51661614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve Laravel Passport access token expiration duration or datetime?
提问by KeitelDOG
I'm using Laravel 5.4 and Passport 4. I want to use only First-Party-App
only. So as suggested from this answer, I want to stay away from putting the ClientID and ClientSecret in the App. I have put in boot()
method of AuthServiceProvider
:
我正在使用 Laravel 5.4 和 Passport 4。我只想使用First-Party-App
。所以正如这个答案所建议的那样,我想远离将 ClientID 和 ClientSecret 放在应用程序中。我已经采用了以下boot()
方法AuthServiceProvider
:
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));
I added my own route in api.php
to accept login from App :
我添加了自己的路由api.php
以接受来自 App 的登录:
Route::post('login', 'Auth\LoginController@apiLogin');
This is my Action :
这是我的行动:
public function apiLogin(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
$user = Auth::user();
$token = $user->createToken('API Access')->accessToken;
return response()->json(["token_type" =>"Bearer","expires_in" => 2592000,"access_token" => $token]);
}
return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}
Is there any method to retrieve the number of seconds for expires_in
(30 days => 2592000s), or the datetime so I could make the calculation automatically?
是否有任何方法可以检索expires_in
(30 天 => 2592000秒)的秒数或日期时间,以便我可以自动进行计算?
回答by KeitelDOG
Here is how I managed to get it from the object:
这是我设法从对象中获取它的方法:
As Tim Lewis pointed me in the comments, there is a $token property, $user->createToken('API Access')
is a Laravel\Passport\PersonalAccessTokenResult
object that contains 2 public properties : $accessToken (String)
and $token (Laravel\Passport\Token)
. So I get the token with $objToken = $user->createToken('API Access');
and calculate expiration time in seconds with $expiration = $objToken->token->expires_at->diffInSeconds(Carbon::now());
. Here is the final code :
正如蒂姆刘易斯在评论中指出的那样,有一个 $token 属性,$user->createToken('API Access')
是一个Laravel\Passport\PersonalAccessTokenResult
包含 2 个公共属性的对象:$accessToken (String)
和$token (Laravel\Passport\Token)
。所以我得到了令牌$objToken = $user->createToken('API Access');
并计算了以秒为单位的到期时间$expiration = $objToken->token->expires_at->diffInSeconds(Carbon::now());
。这是最终的代码:
public function apiLogin(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));
$user = Auth::user();
$objToken = $user->createToken('API Access');
$strToken = $objToken->accessToken;
$expiration = $objToken->token->expires_at->diffInSeconds(Carbon::now());
return response()->json(["token_type" => "Bearer", "expires_in" => $expiration, "access_token" => $strToken]);
}
return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}
But be careful if using these 2 lines in AuthServiceProvider boot() :
但是如果在 AuthServiceProvider boot() 中使用这两行要小心:
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));
as it won't replace the expiration with Personal Access Token in Password Grant Type of Laravel 5.4 according to this Laravel Passport Issue.
因为它不会根据此Laravel Passport Issue 用Laravel 5.4 的密码授予类型中的个人访问令牌替换过期时间。
回答by overtrue
<?php
//...
use Laravel\Passport\Bridge\PersonalAccessGrant;
use League\OAuth2\Server\AuthorizationServer;
//...
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot()
{
// http://php.net/manual/zh/dateinterval.construct.php
$lifetime = new \DateInterval('P1W');
$this->app->get(AuthorizationServer::class)
->enableGrantType(
new PersonalAccessGrant(),
$lifetime
);
}
//...
}
//...