隐式授权 Laravel 5.4 护照不支持的授权类型错误

3

我已经成功地使用passport 2.0和laravel 5.4实现了授权码授权和密码授权。在AuthServiceProvider.php中添加了Passport::enableImplicitGrant();之后,我试着在angular2应用程序中实现隐式授权。

  getImplicitAccessToken() {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'Accept' : 'application/json'
    });
    const query = {
      'grant_type' : 'token',
      'client_id' : Constants.IMPLICIT_TEST_CLIENT_ID,
      'redirect_uri' : window.location.origin + '/implicit-code-grant',
      'scope': ''
    };
    const params = this.getParamsFromJson(query);
    window.location.href = Constants.OAUTH_AUTHORIZATION_URL + '?' + params.toString();
  }
  private getParamsFromJson(query: any) {
    const params = new URLSearchParams();
    for (const key in query) {
      params.set(key, query[key]);
    }
    return params;
  }

然而,我遇到了一个unsupported_grant_type错误。

我也有这个问题,目前正在网上寻找解决方案。 - Frederick G. Sandalo
1个回答

0

在 Laravel 5.4 文档中使用 Implicit Grant Type 时

为什么 Implicit Grant 无法工作?

遵循教程的步骤却出现了以上情况:

// 20170711152854
// http://oauth2server1/oauth/authorize?KEY=14997536295521&client_id=1&redirect_uri=http%3A%2F%2Fauthorizationgrantclient1%2Fcallback&response_type=token&scope=%3FXDEBUG_SESSION_START%3DECLIPSE`enter code here`_DBGP

    {
      "error": "unsupported_grant_type",
      "message": "The authorization grant type is not supported by the authorization server.",
      "hint": "Check the `grant_type` parameter"
    }

============================================================================================

在隐式授权令牌请求代码中,它正在向以下地址发送请求: http://oauth2server1/oauth/authorize?$query
============================================================================================

oauth/authorize GET 请求的处理程序是: Laravel\Passport\Http\Controllers\AuthorizationController@authorize 根据 php artisan route:list

============================================================================================

......在某个地方的代码中

============================================================================================

In vendor\league\oauth2-server\src\AuthorizationServer.php -> function validateAuthorizationRequest()

    /**
     * Validate an authorization request
     *
     * @param ServerRequestInterface $request
     *
     * @throws OAuthServerException
     *
     * @return AuthorizationRequest
     */
    public function validateAuthorizationRequest(ServerRequestInterface $request)
    {
        foreach ($this->enabledGrantTypes as $grantType)
        {
            if($grantType->canRespondToAuthorizationRequest($request)) // <— ValidationStartsHere
            {
                return $grantType->validateAuthorizationRequest($request);
            }
        }

        throw OAuthServerException::unsupportedGrantType();
    }

============================================================================================

......在某个地方的代码行中

============================================================================================

In vendor/league/oauth2-server/src/Grant/AuthCodeGrant.php -> function canRespondToAuthorizationRequest()

    /**
     * {@inheritdoc}
     */
    public function canRespondToAuthorizationRequest(ServerRequestInterface $request)
    {
        return (array_key_exists('response_type', $request->getQueryParams())  // TRUE
                && $request->getQueryParams()['response_type'] === 'code'      // FALSE
                && isset($request->getQueryParams()['client_id'])              // TRUE
        );
    }

the values of the following variables are as follows:
$request->getQueryParams():
“KEY”           => “14997536295521”,
“client_id”     => “1”,
“redirect_uri”  => “http://authorizationgrantclient1/callback”, // refer this value back to how to make an        implicit grant token request
“response_type” => “token”,
“scope”         => “”

作为一个效果...这段代码总是返回 false,而且代码执行回到调用函数

============================================================================================

going back to vendor\league\oauth2-server\src\AuthorizationServer.php->validateAuthorizationRequest()

    /**
     * Validate an authorization request
     *
     * @param ServerRequestInterface $request
     *
     * @throws OAuthServerException
     *
     * @return AuthorizationRequest
     */
    public function validateAuthorizationRequest(ServerRequestInterface $request)
    {
        foreach ($this->enabledGrantTypes as $grantType) {
            if ($grantType->canRespondToAuthorizationRequest($request)) {
                return $grantType->validateAuthorizationRequest($request);
            }
        }

        throw OAuthServerException::unsupportedGrantType(); // <—looks familiar?
    }

============================================================================================

...在某个地方

============================================================================================

In vendor\league\oauth2-server\src\Exception\OAuthServerException.php->function unsupportedGrantType()

    /**
     * Unsupported grant type error.
     *
     * @return static
     */
    public static function unsupportedGrantType()
    {
        $errorMessage = 'The authorization grant type is not supported by the authorization server.';
        $hint = 'Check the `grant_type` parameter';

        return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint);
    }

看起来非常熟悉,对吧?


我想更正一下,我通过在授权服务器中添加以下行来解决了这个问题:在AuthServiceProvider.php中public function boot() { $this->registerPolicies(); Passport::routes(); Passport::enableImplicitGrant(); }之前,我将它们放在了我的客户端应用程序中。 - Frederick G. Sandalo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接