2017-09-22 79 views
0

我試圖在使用Tymon的JWTAuth的Laravel 5.5中實現基於令牌的身份驗證。我遵循GitHub Documentation的庫,並使用以下身份驗證流程。這裏是我的登錄路由的認證部分:Laravel Tymon JWT驗證:在驗證之前檢查傑出的有效令牌嗎?

try { 

    // attempt to verify the credentials and create a token for the user 
    if (!$token = JWTAuth::attempt($credentials)) { 
     return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your email address.'], 401); 
    } 

} 

catch (JWTException $e) { 
    // something went wrong whilst attempting to encode the token 
    return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500); 
} 

// all good so return the token 
return response()->json(['success' => true, 'data'=> [ 'token' => $token ]]); 

而且這裏的路線:

Route::group([ 

    'middleware' => ['jwt.auth', 'jwt.refresh'], 

    ], 

    function() { 

     // Routes requiring authentication 
     Route::get('/logout', 'Auth\[email protected]'); 
     Route::get('/protected', function() { 
      return 'This is a protected page. You must be logged in to see it.'; 
    }); 

}); 

所以你可以看到我使用的jwt.auth和jwt.refresh中間件。現在,一切看起來都按預期工作,我可以使用令牌來驗證用戶身份。每個令牌具有一次使用的生命週期,並且在每次請求(刷新流)後我都會提供另一個有效令牌。

然而,我的問題是,如果我有一個還沒有被使用呢,然後我從報頭中取出,並擊中有效憑證/登錄路由的用戶的有效令牌,我發出另一個有效的令牌。所以現在我有兩個有效的令牌可以用來驗證用戶,因爲我的/登錄路由不會使先前發出的令牌無效。

有誰知道一種方法來檢查用戶是否有一個未解決的有效令牌,以便它可以失效,如果用戶從其他地方登錄?

回答

0

我會在做一些研究後回答我自己的問題。根據我的理解,除非明確列入黑名單,否則JWT令牌是有效的。只要服務器認識到它自己創建了令牌,那麼它就可以使用密鑰解密令牌並假定它是有效的。這就是爲什麼令牌壽命如此重要。所以如果你想使發行的令牌無效,你必須等待到期或黑名單。