【问题标题】:Method Laravel\\Passport\\Bridge\\AccessToken::__toString() must not throw an exception方法 Laravel\\Passport\\Bridge\\AccessToken::__toString() 不能抛出异常
【发布时间】:2021-07-09 04:43:02
【问题描述】:

我使用这些版本的这些包来创建用户登录的令牌,但在创建令牌时遇到此错误:

composer.json

"require": {
    "php": "^7.3|^8.0",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.12",
    "laravel/passport": "^10.1.3",
    "laravel/tinker": "^2.5",
    "laravel/ui": "^3.2",
    "laravelcollective/html": "^6.2",
    "lcobucci/jwt": "3.4.5",
    "spatie/laravel-permission": "^4.0"
},
"require-dev": {
    "barryvdh/laravel-ide-helper": "^2.9",
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.2",
    "nunomaduro/collision": "^5.0",
    "phpunit/phpunit": "^9.3.3"
}

routes/api.php

Route::post('login', [ApiLoginController::class, 'login']);

ApiLoginController.php

public function login(Request $request)
{
    $this->validateLogin($request);

    $user = User::where($this->username, $request->get('username'))
        ->first();

    if (Auth::attempt([$this->username => $request->get('username'), 'password' => $request->get('password')])) {

        if ($user->status != 'active') {
            $msg = 'Account is not active';

            return $this->customError($msg);
        }

        $success['token'] = $user->createToken('Personal Access Client')->accessToken;
        $success['name'] = $user->name;
        $success['phone'] = $user->phone;
        $success['email'] = $user->email;

        $user->password = null;
        $user->save();

        return $this->success($success, "Login completed successfully");

    }

    //delete user password
    if ($user){
        $user->password = null;
        $user->save();
    }

    $msg = 'The information entered does not match our information';

    return $this->customError($msg);
}

邮递员回复

{
"message": "Method Laravel\\Passport\\Bridge\\AccessToken::__toString() must not throw an exception, caught Lcobucci\\JWT\\Signer\\InvalidKeyProvided: It was not possible to parse your key, reason: error:0908F070:PEM routines:get_header_and_data:short header",
"exception": "Symfony\\Component\\ErrorHandler\\Error\\FatalError",
"file": "F:\\xampp\\htdocs\\Diapad-BackEnd\\vendor\\league\\oauth2-server\\src\\ResponseTypes\\BearerTokenResponse.php",
"line": 0,
"trace": []
}

此过程无需令牌行即可工作。甚至创建了一个标记但不作为字符串返回。 我多次降低甚至升级了我的包版本,但都没有成功。

【问题讨论】:

  • 好像你的 PHP 版本有点落后。自 PHP 7.4 起允许从 __toString() 抛出异常
  • @Repox 这可能是真的,但我认为__toString 抛出异常是不正常的。可以称之为……一场特殊的事件。
  • @apokryfos 好吧,既然有一个RFC 允许这样做,并且一致接受,我会假设在__toString() 中抛出异常是不可能,而不是_ “不正常”,而是已实现的期望需求。无论如何,该行为在 PHP 7.4 及更高版本中被接受。
  • @NegarJavadzadeh 这是另一个错误。那是因为你的实现抛出了一个你没有捕捉到的异常。你应该try { } catch(\Lcobucci\\JWT\\Signer\\InvalidKeyProvided $e) { handle_the_exception_message($e->getMessage()); }

标签: php laravel jwt laravel-passport oauth2-server


【解决方案1】:

您可能使用的是 7.4 之前的 PHP 版本。

this RFC 允许在 __toString() 方法中抛出异常,这已被 PHP 7.4 接受

laravel/passportrelies on lcobucci/jwt 以及所需的league/oauth2-server also does

lcobucci/jwt 有一个 minimum of PHP 7.4 dependency written,您的 composer installcomposer update 应该在某个时候捕捉到它,除非您自己没有安装它们或使用带有 --ignore-platform-reqs 标志的 composer

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 2014-12-19
    • 2016-09-23
    • 1970-01-01
    • 2021-08-03
    • 2017-09-09
    • 2016-07-13
    • 2015-04-04
    • 2014-11-18
    相关资源
    最近更新 更多