Laravel Passport 使用 OAuth 2.0
打赏作者

布顽童

终于讲到 Passport 了。支持,支持~~~

布顽童

学习中遇到两个坑,分享下:

  1. node.js 的版本不能使用 v4.5.0 LTS,否则 gulp 会因为缺少依赖而失败;
  2. 使用 Postman 或者是其他 REST 工具的时候,要留意,Accept 和 Authorization 是设置在 Headers 中的,不是在 Request Parameters 中的。
beyondany

`(∩_∩)′

storm19890129

gulp装不上,NPM被墙了,求解决方案~

JellyBool 回复 storm19890129

找个梯子就好了,还需要什么解决方案

Brooks

有没有gulp的配置啊,我这网上找的教程,装了nodejs4.6,npm,vue,配置了之后总出错,也不明白是哪的问题,求指导:
resource目录是这样的:
resource
|----js
|----components
|----app.js
|----bootstrap.js
|----sass
|----_varibales.scss
|----app.scss
gulpfile.json文件是这样的
const elixir = require(‘laravel-elixir’);

require(‘laravel-elixir-vue-2’);

elixir(mix => {
mix.sass(‘app.scss’)
.webpack(‘app.js’);
});

chenxin

这个步骤就是文档上面的步骤嘛!

chenxin
Cannot find module 'laravel-elixir'

执行gulp出现这个,google一下有人说版本不对,更新了也不行。。。。

storm19890129 回复 chenxin

你先执行一下npm install

假如_丶 回复 chenxin
npm install -g npm

更新到最新版本试试

Mankong

按照视频操作 不知道为什么 vue 的组件没有解析

Flourishing 回复 Mankong

我也是组件加载不出来 兄弟 你解决没有呢?

robote

要做一个laravel 的用户中心,供多个系统一起使用,有解决思路么?

robote

我按照这个视频做,怎么要在 passport-client.dev 中配置 passport.dev 一样的数据库表才行呢? 你那需要配置 passport-client.dev 数据库吗?

JellyBool 回复 robote

理论上是不用在 passport-client.dev 设置一样的数据库的,你再仔细看看视频,或者去了解一下 oauth 认证的整个流程先。

qrh

视频上把passport项目和passport-client分开了,可以合在一个项目中么?

JellyBool 回复 qrh

合在一起就不是 oauth 的应用场景了吧

qrh 回复 JellyBool

我看我还是从基础学起吧,难的还吃不透。谢谢站长了

qrh 回复 JellyBool

嗯嗯,好的。谢谢啦

wuan

gulp时报错

module.js:327
throw err;
^

Error: Cannot find module 'isarray’
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (E:\laravel\learn\lesson-api\node_modules\laravel-elixir\node_modules\gulp-util\node_modules\multipipe\node_modules\duplexer2\node_modules\readable-stream\lib_stream_readable.js:25:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions…js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)

JellyBool 回复 wuan

试试重新下载 node_modules 里面的内容

貌似是 isarray 这个没有加载

wuan 回复 JellyBool

gulp好了,但是





这行代码放到home.blade.php时,打开这个页面不显示信息

JellyBool 回复 wuan

chrome devt-tool 有什么报错么?你有引入前端的配置?

UpGod

你的jisi在哪?

JellyBool 回复 UpGod

嗯哼?这是什么?

UpGod 回复 JellyBool

就是视频中的代码放在哪里?不明白jisi是什么鬼。。。

JellyBool 回复 UpGod

代码没有开放出来,jisi 是什么?

vue0706

jelly,同一个服务器怎样设置两个域名访问不同项目?

李宇哲Eric

怎么看不了这个视频了?我用iPhone手机浏览器看的

JellyBool 回复 李宇哲Eric

iPhone 6s 和 6s Plus 我这边测试了 OK,

实在不行,你下载下来看呗

Aaron-wlh

安装passport出现:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.3.31
    - Conclusion: don't install laravel/framework v5.3.31
    - laravel/passport v2.0.0 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.1 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.10 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.11 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.2 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.3 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.4 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.5 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.6 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.7 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.8 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - laravel/passport v2.0.9 requires illuminate/support ~5.4 -> satisfiable by illuminate/support[v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.9].
    - don't install illuminate/support v5.4.0|don't install laravel/framework v5.3.31
    - don't install illuminate/support v5.4.13|don't install laravel/framework v5.3.31
    - don't install illuminate/support v5.4.17|don't install laravel/framework v5.3.31
    - don't install illuminate/support v5.4.19|don't install laravel/framework v5.3.31
    - don't install illuminate/support v5.4.27|don't install laravel/framework v5.3.31
    - don't install illuminate/support v5.4.9|don't install laravel/framework v5.3.31
    - Installation request for laravel/framework (locked at v5.3.31, required as 5.3.*) -> satisfiable by laravel/framework[v5.3.31].
    - Installation request for laravel/passport ^2.0 -> satisfiable by laravel/passport[v2.0.0, v2.0.1, v2.0.10, v2.0.11, v2.0.2, v2.0.3, v2.0.4, v2.0.5, v2.0.6, v2.0.7, v2.0.8, v2.0.9].


Installation failed, reverting ./composer.json to its original content.

是什么原因呢?

JellyBool 回复 Aaron-wlh

版本没有对应而已

punktus0308 回复 Aaron-wlh

“laravel/passport”: “^1.0”,

Aaron-wlh

我的代码:

public function oauth(Request $request)
    {
        $http = new Client();
        $response = $http->post('http://dev.laravist-5.com/oauth/token', [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'client_id' => 4,
                'client_secret' => 'SUcUUlPSGK30FsiouMenQUieZpLjKQiurjEloXPL',
                'redirect_uri' => 'http://dev.passport-client.com/callback',
                'code' => $request->get('code'),
            ]
        ]);
        return json_decode((string) $response->getBody(), true);

    }

报的错误:

ServerException in RequestException.php line 113:
Server error: `POST http://dev.laravist-5.com/oauth/token` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow (truncated...)

我试过这种:

 public function oauth(Request $request)
    {
        $post_data = [
            'grant_type' => 'authorization_code',
            'client_id' => 4,
            'client_secret' => 'SUcUUlPSGK30FsiouMenQUieZpLjKQiurjEloXPL',
            'redirect_uri' => 'http://dev.passport-client.com/callback',
            'code' => $request->get('code'),
        ];
        //http://dev.laravist-5.com/oauth/token
        $result = json_decode($this->curl('http://dev.laravist-5.com/oauth/token', 'POST', $post_data), true);
        dd($result);
        return json_decode((string) $result->getBody(), true);
    }


    private function curl($url, $method = 'GET', $data = [])
    {
        $handler = curl_init();
        curl_setopt($handler, CURLOPT_URL, $url);
        curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($handler, CURLOPT_HEADER, 0);
        if ($method == 'POST') {
            curl_setopt($handler, CURLOPT_POST, 1);
            curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($handler);
        curl_close($handler);
        return $output;
    }

结果是: null
不清楚这个报错是什么意思呢,我究竟哪里出了问题?

JellyBool 回复 Aaron-wlh

具体看看这个 http://dev.laravist-5.com/oauth/token url 报错信息就可以了

zcdll

视频里面 passport.devpassport-client.dev 分别访问两个 Laravel 项目是怎么做到的呀?能稍微详细说一下吗?

JellyBool 回复 zcdll

就是使用的 homestead 的吧,或者你使用 Valet 就可以了啊。很简单的

zcdll 回复 JellyBool

项目不是通过 composer create-project 建立的呀?是通过 homestead 建立的呀?
我以为是通过 hosts 和 nginx 弄得呢。。试了半天不会弄。。

JellyBool 回复 zcdll

额。。。homestead 只是一个开发环境,Valet 也是开发环境。

这个跟用不用 composer 创建项目没有什么关系。其实视频中本身就是用 composer 安装的 Laravel 项目的

masterJiaxing

这里的gulp看的有点懵,不知道他干了什么?

JellyBool 回复 masterJiaxing

编译 js 和 css 文件

masterJiaxing 回复 JellyBool

哦哦,那我想要达到老师的这种效果,我要怎么弄?我不会使用gulp,也不知道哪些文件是需要被编译的,老师可以把编译的配置发出来吗?

ccpone

jelly ,为什么首页多了个Authorized Applications
You have not authorized any applications.。。。。。。上级目录也没有passport-client文件夹求解

JellyBool 回复 ccpone

上级目录也没有passport-client文件夹求解 这是什么?

为什么首页多了个Authorized Applications 这又是什么表现

ccpone 回复 JellyBool

Laravel

qingyang
OAuth Clients
Create New Client
Client ID Name Secret
3 passport-client LNQfwoh5TXODsDGbyiRTNQYexpuapwg8c4V5B4Kp Edit Delete
Authorized Applications
You have not authorized any applications.
Personal Access Tokens
Create New Token
You have not created any personal access tokens.
这是首页,也没有自动生成一个新的passport-client项目。。。

JellyBool 回复 ccpone

额,这是正常的吧。不会自动生成新的 passport-client 项目。

ccpone 回复 JellyBool

。。。。。我还以为是自动生成的,然后用phpstorm打开=。=

JellyBool 回复 ccpone

这个项目 passport-client 需要你去创建啊。你先好好理清他们的关系

longjl1980

请教老师,我用Passport::routes();注册路由后,为什么php artisan route:list 无法查看路由了?

JellyBool 回复 longjl1980

为什么php artisan route:list 无法查看路由了?

具体是什么错误。。。。。。。

longjl1980 回复 JellyBool

[ErrorException]
You must set the encryption key going forward to improve the security of this library - see this page for more information https://oauth2.thephpleague.com/v5-security-improvements/

JellyBool 回复 longjl1980

感觉是姿势不太对,你的 setEncryptionKey 没有。看看你的配置是否OK,或者你的操作流程是否OK

longjl1980 回复 JellyBool

好的,我去检查一下!谢谢

longjl1980

请教老师下
我的代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class OAuthController extends Controller
{
    public function oauth(Request $request){

    	$http = new \GuzzleHttp\Client;
    	$response = $http->post('http://various.dev/oauth/token', [
    		'form_params' => [
	            'grant_type' => 'authorization_code',
	            'client_id' => 5,
	            'client_secret' => 'bcNyfgMaB6c9oQ5JVSVFHYjGs0JyP6CFYRHi4JGm',
	            'redirect_uri' => 'http://various-client.dev/callback',
	            'code' => $request->code
        	],
    	]);
    	return json_decode((string)$response->getBody(),true);
    }
}

如果通过浏览器操作报错

Client error: `POST http://various.dev/oauth/token` resulted in a `400 Bad Request` response:
{"error":"invalid_request","message":"The request is missing a required parameter, includes an invalid parameter value, (truncated...)

但同样的参数,如果用postman来post,结果确实正确的,请问是什么原因?

JellyBool 回复 longjl1980

你的这个 http://various.dev/oauth/token 缺少参数了?

longjl1980 回复 JellyBool

我全部都是按照你视频上做的,就是没找到缺少什么,但是用postman模拟提交,参数全部是上面的,但是是正确的,可以拿到access_token,是不是因为是本地的原因,造成guzzle的POST出问题,还是别的,请指点下。
PS:我刚才又重新开了个demo,一样的结果!我是用的laravel5.4 ,passport 3, guzzle 6.3

JellyBool 回复 longjl1980

试着指定一下 post 的 header:

header('Content-Type: application/x-www-form-urlencoded')

也注意一下这个:

'client_id' => '5',
'redirect_url' => 'http://various-client.dev/callback',
longjl1980 回复 JellyBool

我有一步一步的检查了下,发现报错的具体原因是“Cannot decrypt the authorization code”,深入下去,发现在本地测试时Vendor->defuse->php-excryption->src->Crypto.php中的decryptInternal方法出现异常,但是用postman模拟提交这里却没有问题,可以正常解密出信息,查了不少资料但我始终找不出原因,麻烦老师帮忙看一看!我用的服务器是apache2.4+php7.1

zhouqiang2017 回复 JellyBool

‘grant_type’ => ‘client_credentials’,

zhouqiang2017 回复 longjl1980

‘grant_type’ => ‘client_credentials’,

bit世界风

passport-client.app/oauth 打开后的确有跳转,但是要求输入用户名密码,不知道为什么验证不通过,我在possport.app上注册成功的,这个密码是什么?

JellyBool 回复 bit世界风

确认你的用户邮箱和密码对就行

f8988082

api路由给了auth:api后怎么一直提示Route [login] not defined.啊

JellyBool 回复 f8988082

某处你使用了 route(‘login’) 这种用法吧,而你的 login 这个路由没有给定 name(‘login’)

f8988082 回复 JellyBool

新创建的一个laravel只装了passport

JellyBool 回复 f8988082

你就给 api 呗,可能是你的 auth 会使用到 route(‘login’) 这种

f8988082 回复 JellyBool

不用auth也可以做认证吗?

f8988082 回复 JellyBool

已解决

/**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect()->guest(route('login'));
    }
hsyan

用postman去访问这个http:///your-server.com/api/user 用这个会重定向到/home那个页面,没登陆就会重定向login页面,我在auth.php里面的guards数组配置了,api的 drver => passport
访问那个api/user没有出现401

JellyBool 回复 hsyan

提供 jwt_token 就不会 401 了

phpnodebug

我开始卡在passport版本问题上 解决后 又卡在curl的证书上 win和ubuntu加了证书后还是报错

完颜不破

大神 请问下 passport-client这个项目是要手动创建吗?

JellyBool 回复 完颜不破

是的啊,这个也是一个 laravel 的项目