【发布时间】:2017-08-27 14:54:07
【问题描述】:
我正在开展一个项目,其中 3rd 方应用程序可以从 Laravel 服务器访问数据。我还在 laravel 中创建了一个客户端应用程序进行测试。
以下代码要求授权并且工作正常。
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'=> 'ali@gmail.com',
'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 允许的相同)
- 点击从服务器获取数据
- 输入用户名和密码
- 授权应用
- 经过身份验证的用户访问数据
【问题讨论】: