ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-09-10 19:33:57  阅读:316  来源: 互联网

标签:php laravel oauth laravel-passport


我刚刚阅读了https://laravel.com/docs/5.6/passport文档,我有些疑惑希望有人可以帮助我:

首先,在某些上下文中,我想使用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);

你也可以看看这个https://laravel.com/docs/5.6/passport#implicit-grant-tokens.

标签:php,laravel,oauth,laravel-passport
来源: https://codeday.me/bug/20190910/1800264.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有