【问题标题】:Issuing Access Tokens with Passport in limited lifetime在有限的生命周期内使用 Passport 发行访问令牌
【发布时间】:2019-05-27 11:47:09
【问题描述】:

后端:Laravel api。 前端:Angular。

在 Laravel 上设置 Passport 包后,我使用常规方式对用户进行身份验证,然后以这种方式发出令牌:

 $newToken = $user->createToken('myapp')->accessToken;

这很好用,但问题是令牌生命周期是 1 年,因为它认为 Personal Access Tokens 和文档说:

个人访问令牌始终是长期存在的。他们的一生不 使用 tokensExpireIn 或 refreshTokensExpireIn 时修改 方法。

我的问题是如何在生命周期有限的应用程序中为我的用户颁发令牌?

另一方面,暴露 client_secredclient_id 似乎很危险,所以我不能从我的 Angular 应用程序调用 oauth/token

【问题讨论】:

    标签: laravel api oauth-2.0 laravel-passport


    【解决方案1】:

    我确定这不是最好的解决方案,但这就是我想出的,非常感谢eightyfive 回答:

    首先在您的身份验证控制器中添加这个受保护的方法。

    protected function oauthLogin(Request $request)    
    {
        $client = DB::table('oauth_clients')
            ->where('password_client', true)
            ->first();    
        $request->request->add([
            "grant_type" => "password",
            "username" => $request->email,
            "password" => $request->password,
            "client_id" => $client->id,
            "client_secret" => $client->secret,
        ]);    
        $tokenRequest = $request->create(
            env('APP_URL') . '/oauth/token',
            'post'
        );    
        $instance = Route::dispatch($tokenRequest);    
        return json_decode($instance->getContent());    
    }
    

    那么只有在用户认证后才调用登录方法里面的方法:

    public function login(Request $request)
        {
    
            $credentials = $request->only('email', 'password');
            try {
                // verify the credentials and create a token for the user
                if ($token = Auth::guard('web')->attempt($credentials)) {
    
                    $user = Auth::guard('web')->user();
    
                    $roles = $user->roles()->get()->pluck('name');
    
                    $accessToken = $this->oauthLogin($request)->access_token; // instead of $user->createToken('token')->accessToken,
    
                    return response()->json([
                        'token' => $accessToken,
                        'roles' => $roles,
                        'email' => $request->input('email')
                    ]);
                } else {
                    return response()->json(['error' => 'invalid_credentials'], 401);
                }
            } catch (Exception $e) {
                // something went wrong
                return response()->json(['error' => 'could_not_create_token'], 500);
            }
        }
    

    这种方式下发的token仅限于你在AuthServiceProvider.php中设置的时间:

    public function boot()
        {
            $this->registerPolicies();
    
            //
            Passport::routes();
    
            Passport::tokensExpireIn(now()->addHour(1));
    
            Passport::refreshTokensExpireIn(now()->addHour(1));
    
        }
    

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2018-10-26
      • 2020-02-26
      • 2020-12-19
      • 1970-01-01
      • 2019-06-17
      • 2011-04-29
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多