使用PHP的DocuSign JWT响应错误invalid_request

时间:2020-06-17 21:57:36

标签: php jwt docusignapi

我在尝试从DocuSign检索JWT令牌时遇到问题,到目前为止,这是我已完成的工作的代码片段(使用DEVELOPER SANDBOX帐户):

// Developer exmple
$header = [
    'typ' => 'JWT',
    'alg' => 'RS256',
];
$time = time();
$body = [
    // Integration key provded by DocuSign at Admin > API and Keys > Apps.
    'iss' => '[Integration key]',
    // User ID provded by DocuSign at Admin > API and Keys.
    'sub' => '[User ID]',
    'iat' => $time,
    'exp' => strtotime( '+45 minutes', $time ),
    'aud' => 'account-d.docusign.com',
    'scope' => 'signature impersonation',
];

// RSA Private key provided by DocuSign
// When integration APP was created
$rsa_key = '-----BEGIN RSA PRIVATE KEY----- [.........]';

// Base64 + URL Encoding
$header = urlencode( base64_encode( json_encode( $header ) ) );
$body = urlencode( base64_encode( json_encode( $body ) ) );

// JWT signature created using Firebase\JWT\JWT package
$signature = JWT::encode(
    $header . '.' . $body,
    $rsa_key,
    'RS256'
);

// Get request using Curl (10quality/php-curl package)
$response = curl_request(
    'https://account-d.docusign.com/oauth/token',
    'POST',
    [
        'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion' => $header . '.' . $body . '.' . $signature,
    ]
);

// Process response
if ( $response ) {
    $response = json_decode( $response );
    if ( isset( $response->error ) )
        throw new Exception( 'DocuSign error: ' . $response->error );
    var_dump( $response );
}

这是我遵循的指南: https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-jsonwebtoken

根据本指南,抛出的错误表明JWT没有正确创建,尽管我已经多次检查了代码,并且遵循了指南中描述的所有内容。

我有点困惑,我对代码的问题一无所知,甚至进行了反向工程以验证编码是否正确。

以前有没有人与DocuSign JWT一起工作过,或者知道我可能做错了什么?


更新:使用客户端https://github.com/docusign/docusign-php-client的有效代码段 工作片段:

$api = new ApiClient( new Configuration );
$api->getOAuth()->setOAuthBasePath( 'account-d.docusign.com' );
$response = $api->requestJWTUserToken(
    '[Integration key]',
    '[User ID]',
    $rsa_key,
    'signature impersonation',
);

2 个答案:

答案 0 :(得分:0)

请考虑使用SDK,而不要尝试自己进行JWT编码。 这将使其更容易,更安全,并最终为您启用其他功能。 开始使用克隆this repo,该克隆在PHP中并具有JWT和Auth Code Grant支持。如果您在这里遇到问题-请让我知道,我们将很乐意为您提供帮助。

答案 1 :(得分:0)

我相信您想替换您的行:

    $header = urlencode( base64_encode( json_encode( $header ) ) );
    $body = urlencode( base64_encode( json_encode( $body ) ) );

使用

    $header = str_replace('=', '', strtr(base64_encode($header ), '+/', '-_'));
    $body = str_replace('=', '', strtr(base64_encode($body), '+/', '-_'));

与您的签名相同:

    $signature = str_replace('=', '', strtr(base64_encode($signature), '+/', '-_'));

我从jwtphpjquery撤消了这个想法。