php passport security,php – 如何使用密码授予令牌的Laravel Passport?

本文讨论了在Laravel应用中使用Passport进行OAuth身份验证时遇到的问题,包括客户端ID和客户端密钥的存储、登录用户、刷新令牌的使用以及不常用Passport端点的管理。解决方案建议了如何创建自定义API进行用户登录,以及如何正确使用刷新令牌。同时,提出了手动管理Passport路由以移除不必要的端点。
摘要由CSDN通过智能技术生成

首先,在某些上下文中,我想使用Passport作为为我的移动应用程序(第一方应用程序)提供Oauth身份验证的方法.

>当我使用php artisan passport:client –password时,我会收到客户端ID和客户端密钥.这个值是否必须在我的应用上修复?例如,将它们存储为硬编码或“设置”文件?如果不应存储这些值,那么它应该如何工作?

>要将用户注册到我的应用程序,我使用:$user-> createToken(‘The-App’) – > accessToken;我得到的是accessToken将用于发送我的所有请求作为标题(Authorization => Bearer $accessToken),但究竟什么是“The-App”值?

>登录用户我正在使用URL:http://example.com/oauth/token并作为参数发送:

{

“username”:“user@email.com”,

“password”:“userpassword”,

“grant_type”:“密码”,

“client_id”:1,//我从命令中获得的客户端ID(问题1)

“client_secret”:“嘘”//我从命令中得到的客户端秘密(问题1)

}

>当我使用前一个端点登录用户时,我回到了refresh_token,我读到我可以通过http://example.com/oauth/token/refresh刷新令牌,但我尝试请求刷新我得到了错误419,我从csrf中删除了url oauth / token / refresh验证,现在我收到“消息”:“未经验证.”,我正在提出以下请求:

内容类型:x-www-form-urlencoded

grant_type:refresh_token

refresh_token:-refresh-token //我从命令中获取的刷新令牌(问题3)

client_id:1 //我从命令获得的客户端ID(问题1)

client_secret:嘘//我从命令得到的客户端秘密(问题1)

范围: ”

我应该使用这个端点吗?鉴于我正在努力开发的应用程序,或者没有必要.

>最后,我从护照中获得了许多端点,我认为不会使用这些端点,例如:oauth / clients *,oauth / personal-access-tokens *有没有办法将它们从已发布的端点中删除护照?

非常感谢你的帮助!

解决方法:

如果您正在使用自己的api,那么您不需要拨打http://example.com/oauth/token

用户登录,因为那时你需要在app端存储client_id和client_secret.最好为登录创建一个api,然后您可以检查凭据并生成个人令牌.

public function login(Request $request)

{

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {

// Authentication passed...

$user = Auth::user();

$token = $user->createToken('Token Name')->accessToken;

return response()->json($token);

}

}

Finally, there are a lot of endpoints that I get from passport that I

don’t think I will use for example: oauth/clients*,

oauth/personal-access-tokens* is there a way to remove them from the

endpoints published by passport?

你需要删除Passport :: routes();来自AuthServiceProvider并手动输入所需的护照路线.我认为你只需要oauth / token路由.

what exactly is “The-App” value for?

如果你检查oauth_access_tokens表它有名字字段. $user-> createToken(‘令牌名称’) – > accessToken;这里存储在名称字段中的“令牌名称”.

How to use Laravel Passport with Password Grant Tokens?

要生成密码授予令牌,您必须在应用程序端存储client_id和client_secret(不推荐,请检查this)并假设您必须重置client_secret然后旧版本应用程序停止工作,这些都是问题.要生成密码授予令牌,您必须像在步骤3中提到的那样调用此API.

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [

'form_params' => [

'grant_type' => 'password',

'client_id' => 'client-id',

'client_secret' => 'client-secret',

'username' => 'taylor@laravel.com',

'password' => 'my-password',

'scope' => '',

],

]);

return json_decode((string) $response->getBody(), true);

Generate token from refresh_token

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [

'form_params' => [

'grant_type' => 'refresh_token',

'refresh_token' => 'the-refresh-token',

'client_id' => 'client-id',

'client_secret' => 'client-secret',

'scope' => '',

],

]);

return json_decode((string) $response->getBody(), true);

标签:php,laravel,oauth,laravel-passport

来源: https://codeday.me/bug/20190910/1800264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值