首页 文章

如何使用Laravel Passport和密码授予令牌?

提问于
浏览
8

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

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

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

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

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

{“username”:“user@email.com”,“password”:“userpassword”,“grant_type”:“password”,“client_id”:1,//我从命令获得的客户端ID(问题1) “client_secret”:“嘘”//我从命令得到的客户端秘密(问题1)}

  • 当我使用前一个 endpoints 登录用户时,我回到了refresh_token,我读到我可以通过http://example.com/oauth/token/refresh刷新令牌,但我尝试请求刷新我得到了错误419,我从csrf中删除了url oauth / token / refresh验证,现在我回来 "message": "Unauthenticated." ,我正在提出以下要求:

Content-Type:x-www-form-urlencoded grant_type:refresh_token refresh_token:-refresh-token //我从命令获得的刷新令牌(问题3)client_id:1 //我从命令获得的客户端ID (问题1)client_secret:嘘//我从命令(问题1)范围得到的客户端秘密:''

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

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

非常感谢你的帮助!

1 回答

  • 21

    如果你正在使用自己的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);
            }
    }
    

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

    您需要从AuthServiceProvider中删除 Passport::routes(); 并手动只放置所需的护照路由 . 我想你只需要 oauth/token 路线 .

    究竟什么是“The-App”的 Value ?

    如果你检查 oauth_access_tokens 表它有名字字段 . $user->createToken('Token Name')->accessToken; 这里 "Token Name" 存储在名称字段中 .

    如何使用带有密码授予令牌的Laravel Passport?

    要生成密码授予令牌,您必须在应用端存储 client_idclient_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);
    

    从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);
    

    You can look this https://laravel.com/docs/5.6/passport#implicit-grant-tokens too.

相关问题