尝试获取令牌时,Docusign PHP获取“ invalid_grant:unsupported_grant_type”(JWT身份验证)

时间:2020-10-15 12:30:55

标签: php jwt docusignapi

我正在尝试使用Docusign JWT身份验证来获取访问令牌,但是我总是得到:

{"error":"invalid_grant","error_description":"unsupported_grant_type"}

我仔细检查了所有数据(集成密钥,api用户名等),它们很好。 我遵循了Docusign指南中的所有步骤。

我不确定100%的唯一部分是何时生成JWT令牌的签名。 该文档说:

The first two parts of the JWT are signed with your application's private key (using the RSA SHA-256 digital signature algorithm) as shown in the diagram.

这是我生成签名的方式:

$header = [
            'typ' => 'JWT',
            'alg' => 'RS256'
        ];

$body = [
            'iss' => getenv('INTEGRATION_KEY'),
            'sub' => getenv('API_USERNAME'),
            'iat' => time(),
            'exp' => time() + 3600,
            'aud' => str_replace('https://', '', getenv('AUTH_URL')),
            'scope' => 'signature impersonation'
        ];
$signature = JWT::encode($body, file_get_contents(env('PRIVATE_KEY')), 'RS256');

$header = $this->base64url_encode(json_encode($header));
$body = $this->base64url_encode(json_encode($body));
$jwt = $header . '.' . $body . '.' . $signature;

对吗? 如果不是这样,并且由于JWT :: encode希望将数组作为第一个参数,那么我应该如何使它起作用?

这是我请求访问令牌的方式:

        return Http::withHeaders(
            [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]
        )->post(
            getenv('AUTH_URL') . '/oauth/token',
            [
                'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
                'assertion' => $jwt
            ]
        );

谢谢!

3 个答案:

答案 0 :(得分:1)

显然,Firebase JWT编码方法无法正确编码字符串。

我用了这个:

$header = $this->base64url_encode(json_encode($header));
$body = $this->base64url_encode(json_encode($body));

openssl_sign(
            $header.".".$body,
            $signature,
            file_get_contents(env('PRIVATE_KEY_PATH')),
            "sha256WithRSAEncryption"
        );

成功了。

确保在请求同意和请求jwt令牌时使用相同的作用域。

感谢大家的帮助。

答案 1 :(得分:0)

创建正确的JWT令牌非常困难。建议您使用PHP SDK中的requestJWTUserToken或查看其source来了解它如何发出OAuth请求。

答案 2 :(得分:0)

我在使用 Laravel 6 的应用程序中遇到了同样的问题,我设法解决了如下问题:

// the Header will not be needed as it is automatically generated
$header = [
            'typ' => 'JWT',
            'alg' => 'RS256'
             ];
$body = [
            'iss' => getenv('INTEGRATION_KEY'),
            'sub' => getenv('API_USERNAME'),
            'iat' => time(),
            'exp' => time() + 3600,
            'aud' => str_replace('https://', ​​'', getenv('AUTH_URL')),
            'scope' =>'signature impersonation'
            ];
/**
* Note that when creating the JWT, only the $body is provided, 
* as the function already performs the necessary concatenations.
* in your code you put it like this: 
* $jwt = $header . '.' . $body . '.' . $signature;
* which generates a hash that cannot be validated,
*/

// create the JWT
$jwt = JWT::encode($body , $privateKey, 'RS256');

// make the request
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', getenv('AUTH_URL').'/oauth/token',['query' =>[
                                                                    'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
                                                                    'assertion'  => $jwt,
                                                                    ]
]);

echo '<pre>';
print_r($response);
echo '</pre>';