2017-04-02 43 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

很高兴你已经找到它了 –