Laravel 护照 - CreateFreshApiToken 没有 laravel_token cookie
Laravel passport - CreateFreshApiToken no laravel_token cookie
CreateFreshApiToken,一旦添加到 Kernel.php 中的 'web' 中间件组,就应该将 laravel_token cookie 附加到通过网络中间件发出的每个请求的响应中,不是吗?例如,该组中的任何内容都应该接收 cookie:
Route::group(['middleware' => ['web']], function () {
Route::get('/{vue?}', function () { return view('index'); } )->where('vue', '[\/\w\.-]*');
});
我正在尝试替换当前使用 client_secret 的工作流程,看到 oauth2 和网站在同一个域中,在 CreateFreshApiToken 的帮助下自我消耗 api 的想法有一个听起来不错,虽然在这一点上我不确定我是否做对了。
按要求引用我的评论
"You will not receive the token in this method. The cookie created will contain the encrypted token and will be used by the passport token guard to authenticate requests coming with the cookie. So the routes protected by the auth:api
middleware will get authenticated without manually attaching any access token. So after the initial login (which should be the traditional way) you need to reload the page at least once so that the cookie gets created by the CreateFreshApiToken
middleware."
除此之外,您还可以为您的 SPA 使用密码授权。这就是您在网上看到的 90% 的教程所做的。但是他们将客户端 ID 和密码存储在 JavaScript 文件中,从安全角度来看,这是非常糟糕的做法。为了克服这个问题,您可以创建一个代理 route/middleware 来注入客户端 ID 和密码。所以你只需要通过电子邮件和密码来获取访问令牌和刷新令牌。
但是您同样需要将访问令牌和刷新令牌存储在浏览器的本地存储中,这是开发人员通常所做的。但是令牌变得容易受到 XSS 攻击。因此,在使用 SPA 身份验证时,需要从安全角度考虑很多方面。
要克服上述所有问题,您需要将令牌安全地存储在 HttpOnly cookie 中,然后使用中间件从 cookie 中解析令牌并根据任何 API 请求对用户进行身份验证。这是使用 SPA 进行身份验证的最实用和最安全的方法。 Laravel Passport 就是这样做的,但需要对自定义登录路由进行一些初始工作才能实现。缺乏适当的文档以及对其使用和工作的清晰度是 Passport 的一个问题。
这里有一些关于这些的好资源
https://web.archive.org/web/20141208132104/http://alexbilbie.com/2014/11/oauth-and-javascript/
http://esbenp.github.io/2017/03/19/modern-rest-api-laravel-part-4/
CreateFreshApiToken,一旦添加到 Kernel.php 中的 'web' 中间件组,就应该将 laravel_token cookie 附加到通过网络中间件发出的每个请求的响应中,不是吗?例如,该组中的任何内容都应该接收 cookie:
Route::group(['middleware' => ['web']], function () {
Route::get('/{vue?}', function () { return view('index'); } )->where('vue', '[\/\w\.-]*');
});
我正在尝试替换当前使用 client_secret 的工作流程,看到 oauth2 和网站在同一个域中,在 CreateFreshApiToken 的帮助下自我消耗 api 的想法有一个听起来不错,虽然在这一点上我不确定我是否做对了。
按要求引用我的评论
"You will not receive the token in this method. The cookie created will contain the encrypted token and will be used by the passport token guard to authenticate requests coming with the cookie. So the routes protected by the auth:api
middleware will get authenticated without manually attaching any access token. So after the initial login (which should be the traditional way) you need to reload the page at least once so that the cookie gets created by the CreateFreshApiToken
middleware."
除此之外,您还可以为您的 SPA 使用密码授权。这就是您在网上看到的 90% 的教程所做的。但是他们将客户端 ID 和密码存储在 JavaScript 文件中,从安全角度来看,这是非常糟糕的做法。为了克服这个问题,您可以创建一个代理 route/middleware 来注入客户端 ID 和密码。所以你只需要通过电子邮件和密码来获取访问令牌和刷新令牌。
但是您同样需要将访问令牌和刷新令牌存储在浏览器的本地存储中,这是开发人员通常所做的。但是令牌变得容易受到 XSS 攻击。因此,在使用 SPA 身份验证时,需要从安全角度考虑很多方面。
要克服上述所有问题,您需要将令牌安全地存储在 HttpOnly cookie 中,然后使用中间件从 cookie 中解析令牌并根据任何 API 请求对用户进行身份验证。这是使用 SPA 进行身份验证的最实用和最安全的方法。 Laravel Passport 就是这样做的,但需要对自定义登录路由进行一些初始工作才能实现。缺乏适当的文档以及对其使用和工作的清晰度是 Passport 的一个问题。
这里有一些关于这些的好资源 https://web.archive.org/web/20141208132104/http://alexbilbie.com/2014/11/oauth-and-javascript/
http://esbenp.github.io/2017/03/19/modern-rest-api-laravel-part-4/