实现OAuth 2.0整个流程
打赏作者

JobsLong

ruan yi feng 😃

JellyBool

挺好的,这个Blog @JobsLong

xiao 回复 JellyBool

请问用OAuth认证,如何实现自动认证?例如我注册之后如何直接获取access_token而不是让用户再去登录

woailuosj
感觉好像不够完整呢,最后听了感觉思绪有点乱就结束了
JellyBool

是什么地方乱?你看了阮一峰的那篇博客介绍没有,你得对OAuth有基础的了解和对它的认证流程有认识,不然是会思绪很乱。 @woailuosj

woailuosj

@JellyBool 恩恩,我自己整理整理思路

woailuosj

@JellyBool 配置好后,怎么做注册登录?我现在用的是Client Credentials Grant,是表单提交到 oauth/access_token来获取吗?我之前已经用laravel自带的auth做了注册登录,怎么修改?

JellyBool

用户的注册登录跟原来的是一样的啊。如果你也想使用oauth来保护你的注册过程,直接使用oauth middleware来保护就可以了,我们引入oauth主要是给我们的客户端共享部分用户的信息而已,比如说laravist使用微博第三方登录的时候,laravist站点就可以看做一个client,新浪就是我们的sever,然后我们可以通过新浪微博的开发人员实现的oauth拿到用户的部分信息,比如头像地址,昵称等,但是新浪微博的注册和登录照样是正常的。

那么在这个系列当中,我们的身份就是新浪的开发者的这样的角色,而不是普通的注册用户。 @woailuosj

woailuosj

@JellyBool 哦,也就是说现在我提供了一个第三方登录的方式,人家可以通过请求我的Oauth获取到token,来做登录,

JellyBool

恩恩恩,就是这个意思 @woailuosj

liudong0763 回复 JellyBool

哦哦,原来是酱紫

moon

这一篇感觉只是按wiki上的东西稀里糊涂地在做,或许没有弄明白,或者至少没有说清楚。建议把User,Client Server,Open Server的作用分开说明,并在例子中指明哪些操作是属于哪一方的。加油!

JellyBool

OK,我录一个视频说明一下吧 @moon

xlkit456852

api的数据分页会录一个视频吗?

JellyBool 回复 xlkit456852

分页其实比较简单的了:

 $lessons = Lesson::paginate(25);

 return $this->response->paginator($lessons, new LessonTransformer);
daimingkang

ReflectionException in Container.php line 734: Class csrf does not exist

JellyBool 回复 daimingkang

你的 laravel 版本是多少?

你的路由的代码,和控制器的代码贴出来看看。有可能你使用了这种:

    Route::group(['before' => 'csrf'], function() {}

或者这样的:

    Route::group(['middleware' => 'csrf'], function() {

这样的写法 5.1 之后应该不支持了

daimingkang 回复 JellyBool

我就是直接粘贴的

Route::post('oauth/authorize', ['as' => 'oauth.authorize.post', 'middleware' => ['csrf', 'check-authorization-params', 'auth'], function () {

    $params = Authorizer::getAuthCodeRequestParams();
    $params['user_id'] = Auth::user()->id;
    $redirectUri = '/';

    // If the user has allowed the client to access its data, redirect back to the client with an auth code.
    if (Request::has('approve')) {
        $redirectUri = Authorizer::issueAuthCode('user', $params['user_id'], $params);
    }

    // If the user has denied the client to access its data, redirect back to the client with an error message.
    if (Request::has('deny')) {
        $redirectUri = Authorizer::authCodeRequestDeniedRedirectUri();
    }
    return Redirect::to($redirectUri);
}]);
JellyBool 回复 daimingkang

所以你把 'middleware' => ['csrf' 中的 csrf 去掉不就好了么

daimingkang 回复 JellyBool

恩 ,这个地方好了 感觉还需要多实战实战

hard88

oauth怎么像jwt-auth使用token一样通过使用access_token获取对应的user信息?

JellyBool 回复 hard88

你使用 password grant type 就可以直接有 token ,这个在 5.3 新系列就有说明

hard88 回复 JellyBool

只能通过直接写 api 的地址获取json数据吗?
有没有类似获取路由

route()

或者
action()
的函数

JellyBool 回复 hard88

你定义了 route 的话,是可以使用的吧

hard88

我想获取api的数据

Route::get('test',function (){
    $data = file_get_contents("http://api.dev/api/lessons");
    $data_arr = json_decode($data);
    dd($data_arr);
});

有没有更好的方法?这样直接写url很奇怪。。

JellyBool 回复 hard88

什么意思? url 很奇怪?

hard88 回复 JellyBool

只能通过直接写 api 的地址获取json数据吗?
有没有类似获取路由

route()    或者   action()

的函数

pygroos

之前买过这个视频 微信付款 你网站改版之后怎么还要付费才能看?

JellyBool 回复 pygroos

我看一下,也有可能是我重构代码的逻辑出问题了。稍等

JellyBool 回复 pygroos

我看了一下代码和后台,这边的逻辑貌似是正确的,而在后台并没有查到你相应的记录,可否告知一下你微信支付的大概时间,我看看订单号

PHPclown

JellyBool好,当我点击Approve后出现了这样的错误:

### ErrorException in AuthManager.php line 292:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\SessionGuard' does not have a method 'handle'
JellyBool 回复 PHPclown

所以你的代码是什么?

PHPclown 回复 JellyBool

这是我的路由文件代码,用的是laravel 5.2版本:

Auth::loginUsingId(1);

Route::post('oauth/authorize', ['as' => 'oauth.authorize.post', 'middleware' => ['check-authorization-params', 'auth'], function() {

    $params = Authorizer::getAuthCodeRequestParams();
    $params['user_id'] = Auth::user()->id;
    $redirectUri = '/';

    // If the user has allowed the client to access its data, redirect back to the client with an auth code.
    if (Request::has('approve')) {
        $redirectUri = Authorizer::issueAuthCode('user', $params['user_id'], $params);
    }

    // If the user has denied the client to access its data, redirect back to the client with an error message.
    if (Request::has('deny')) {
        $redirectUri = Authorizer::authCodeRequestDeniedRedirectUri();
    }

    return Redirect::to($redirectUri);
}]);

Route::get('oauth/authorize', ['as' => 'oauth.authorize.get', 'middleware' => ['check-authorization-params'], function() {
   $authParams = Authorizer::getAuthCodeRequestParams();

   $formParams = array_except($authParams,'client');

   $formParams['client_id'] = $authParams['client']->getId();

   $formParams['scope'] = implode(config('oauth2.scope_delimiter'), array_map(function ($scope) {
       return $scope->getId();
   }, $authParams['scopes']));

   return view('oauth.authorization-form', ['params' => $formParams, 'client' => $authParams['client']]);
}]);

Route::post('oauth/access_token', function() {
    return Response::json(Authorizer::issueAccessToken());
});
1833183060

本课程的源码在哪里啊

zhangbao1992

我的Laravel5.1只安装了 dingo 和 oauth2-server-laravel,没有安装 jwt。测试地址时

api.dev/oauth/authorize?client_id=ZoZgKTVUyiZLdvx0LYB2CBXXdnIfHRUw&redirect_uri=https://bing.com&response_type=code

报这样的错

ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\Guard' does not have a method 'handle'
JellyBool 回复 zhangbao1992

感觉应该是版本没对应,所以配置文件没对的问题

zhangbao1992 回复 JellyBool

这是我的配置信息:

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
    "dingo/api": "1.0.*@dev",
    "lucadegasperi/oauth2-server-laravel": "5.1.*"
},
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "phpspec/phpspec": "~2.1"
},

学这节课时,我把5.4版本换到了5.1版本(因为oauth2-server-laravel不支持5.3+),没有安装jwt。

JellyBool 回复 zhangbao1992

Illuminate\Auth\Guard 这个 guard 类没有 handle 方法。。。。

应该就是后来的 laravel 版本修改了

myhyperion 回复 JellyBool

我也遇到同样的问题了。难道5.1后来的版本也修改了?因为安装项目的时候用的是

composer create-project laravel/laravel learn-api --prefer-dist 5.1

myhyperion 回复 JellyBool

堆栈如下:

Whoops, looks like something went wrong.

1/1
ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class ‘Illuminate\Auth\Guard’ does not have a method 'handle’
in Manager.php line 137
at HandleExceptions->handleError(‘2’, ‘call_user_func_array() expects parameter 1 to be a valid callback, class ‘Illuminate\Auth\Guard’ does not have a method ‘handle’’, ‘/home/vagrant/Code/learn-api/vendor/laravel/framework/src/Illuminate/Support/Manager.php’, ‘137’, array(‘method’ => ‘handle’, ‘parameters’ => array(object(Request), object(Closure))))
at call_user_func_array(array(object(Guard), ‘handle’), array(object(Request), object(Closure))) in Manager.php line 137
at Manager->__call(‘handle’, array(object(Request), object(Closure)))
at call_user_func_array(array(object(AuthManager), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in CheckAuthCodeRequestMiddleware.php line 55
at CheckAuthCodeRequestMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckAuthCodeRequestMiddleware), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 673
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in OAuthExceptionHandlerMiddleware.php line 36
at OAuthExceptionHandlerMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(OAuthExceptionHandlerMiddleware), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in Request.php line 111
at Request->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Request), ‘handle’), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 53

myhyperion 回复 JellyBool

另外,似乎这个api没有对用户对数据的抓取频率和数量进行限制。

wwhu668

原来如此,厉害。

noikiy

实现:现在想弄个简单的微信小程序的api接口给小程序用(目前不是很复杂的那种小程序);
需要:用laravel来写接口
问题:
是不是 和上面的 dingo 和 jw没有关系了?
微信小程序的用户应该是微信用户 微信小程序用户也支持oauth2.0 这种呢?
我记得微信登录只能是公众平台和开放平台才能登录?和小程序有关系吗?
若是就是简单的接口按照上面的dingo 和 jwt 能实现微信小程序的api接口吗?
我是刚开始研究微信小程序的api接口(没有写过小程序的接口)
求jellybool有时间 指点一下?
@jellybool

JellyBool 回复 noikiy

是不是 和上面的 dingo 和 jw没有关系了?

微信小程序的话,你只需要一个最基本的 jwt 就OK

微信小程序用户也支持oauth2.0 这种呢?

这个是小程序自己就有的,不用自己实现

我记得微信登录只能是公众平台和开放平台才能登录?和小程序有关系吗?

没有

若是就是简单的接口按照上面的dingo 和 jwt 能实现微信小程序的api接口吗?

阔以,我之前就写过小程序。最低的需求就是一个 jwt 就OK

noikiy 回复 JellyBool

okay!! thank you !!

wdgwgz 回复 JellyBool

小程序使用dingo 管理API, jwt管理Token 这样配合比较好吧

roc_teng

@jellypool 教主还有这视频的源码吗,oauth2-server-laravel 没用了,这个视频 想继续下去,可那些wiki上的代码无处可寻