2017-04-02 40 views
0

我正在開發第三方應用程序可以從Laravel服務器訪問數據的項目。我也在laravel中創建了一個客戶端應用程序進行測試。Laravel Passport在授權之前驗證用戶

以下代碼要求授權並且其工作正常。

Route::get('/applyonline', function() { 
$query = http_build_query([ 
    'client_id' => 5, 
    'redirect_uri' => 'http://client.app/callback', 
    'response_type' => 'code', 
    'scope' => '', 
]); 
return redirect('http://server.app/oauth/authorize?'.$query); 
}); 

如何在授權之前驗證用戶身份?現在我可以使用此代碼訪問數據表單服務器。

Route::get('/callback', function (Request $request) { 
$http = new GuzzleHttp\Client; 
$response = $http->post('http://server.app/oauth/token', [ 
    'form_params' => [ 
     'grant_type' => 'password', 
     'client_id' => 2, 
     'client_secret' => 'fcMKQc11SwDUdP1f8ioUf8OJwzIOxuF8b2VKZyip', 
     'username'=> '[email protected]', 
     'password' => 'password', 
    ], 
]); 

$data = json_decode((string) $response->getBody(), true); 
$access_token = 'Bearer '. $data['access_token']; 
$response = $http->get('http://server.app/api/user', [ 
    'headers' => [ 
     'Authorization' => $access_token 
    ] 
]); 

$applicant = json_decode((string) $response->getBody(), true); 

return view('display.index',compact('applicant')); 

});

雖然上面的代碼工作正常,但我不認爲它是一個很好的方式來問客戶端的用戶名和密碼。

我想用這個流程(同Facebook允許)

  • 點擊獲取數據從服務器
  • 輸入用戶名和密碼進行身份驗證的用戶
  • 授權應用
  • 訪問數據

回答

0

那是一個愚蠢的錯誤。它適用於授權碼授權類型。我的錯誤是我在同一個瀏覽器中同時測試了服務器和客戶端,而沒有註銷。所以客戶端從服務器訪問自己的數據。此流程圖確實幫助我瞭解護照授權的過程。 http://developer.agaveapi.co/images/2014/09/Authorization-Code-Flow.png

Route::get('/callback', function (Request $request) { 
$http = new GuzzleHttp\Client; 
$response = $http->post('http://server.app/oauth/token', [ 
    'form_params' => [ 
     'grant_type' => 'authorization_code', 
     'client_id' => 5, 
     'client_secret' => 'fcMKQc11SwDUdP1f8ioUf8OJwzIOxuF8b2VKZyip', 
     'redirect_uri' => 'http://client.app/callback', 
     'code' => $request->code, 
    ], 
]); 
return json_decode((string) $response->getBody(), true);}); 
+0

很高興你已經找到它了 –