Docusign - 请求 JWT 访问令牌时出现“错误”:“Invalid_request”响应

时间:2021-03-30 10:29:20

标签: c# docusignapi bouncycastle

我正在使用 REST API,并且正在 C# 中实现来自 Docusign 的 Postman Collection 的“02 JWT 访问令牌”。我生成了 RSA 密钥对,标头和正文已准备好 (Header.Body),采用 Base64 格式。

ctx.Guild.CurrentMember.Color

Base64 编码器:

long d1 = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long d2 = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 1800;

string header = "{  \"alg\": \"RS256\",  \"typ\": \"JWT\"}";
string body = "{  \"iss\": \"" + integrationKey + "\",  \"sub\": \"" + apiUsername + "\",  \"aud\": \"" + environment + "\",  \"iat\": " + d1.ToString() + ",  \"exp\": " + d2.ToString() + ",  \"scope\": \"signature impersonation\"}";
string base64 = Base64Encode(header) + "." + Base64Encode(body);

我已使用以下代码进行签名,它似乎有效:

public string Base64Encode(string plainText)
{
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
    return System.Convert.ToBase64String(plainTextBytes);
}

然后我提交。断言(以下代码中的标记)采用“Header.Body.Signature”格式:

private string Sign(string message, string privateKey)
{
    try
    {
        byte[] r = Encoding.UTF8.GetBytes(message);

        StringReader strReader = new StringReader(privateKey);
        PemReader pemReader = new PemReader(strReader);
        AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
        RsaKeyParameters privateRSAKey = (RsaKeyParameters)keyPair.Private;


        ISigner sig = SignerUtilities.GetSigner("SHA256withRSA");
        sig.Init(true, privateRSAKey);
        sig.BlockUpdate(r, 0, r.Length);
        byte[] signedBytes = sig.GenerateSignature();

        return Convert.ToBase64String(signedBytes);

    }
    catch (Exception ex)
    {
        throw ex;
    }
}

我提交时出错,{"error":"Invalid_request"}。

更多信息: https://developers.docusign.com/platform/auth/jwt/jwt-get-token/

3 个答案:

答案 0 :(得分:1)

看来 JWT 断言是从头开始的,这可能会导致很多问题。DocuSign 建议使用库来进行 JWT 断言。 此链接使用我们的 SDK 显示此过程: https://github.com/docusign/code-examples-csharp/blob/06c46b235c094be1346e4ddca692aa9b72a82456/launcher-csharp/Common/JWTAuth.cs

对于这种情况,您可能需要尝试一种方法:public static (string, string, string) AuthenticateWithJWT()。这将使用 .NET Core。 或者您可以使用我们 SDK 上的一个方法:RequestJWTUserToken。 其中之一可能会解决它,考虑应该安装和设置 DocuSign 代码示例启动器以使用此方法。

答案 1 :(得分:0)

DocuSign OAuth JWT 授权使用带有 RS256 算法的 JWT 令牌。

RS256 是指使用 SHA-256 密钥的 RSA 签名算法。

How to create an RS256 JWT 向您展示了如何去做。情况很复杂。我建议你使用库函数!

对于大多数 DocuSign API 方法,请使用我们 SDK 的 RequestJWTUserToken 方法之一。

答案 2 :(得分:0)

问题在于 Base64 编码。它应该是 Base64Url 编码,格式略有不同。值得庆幸的是,它只是添加 '.Replace("=", "").Replace("/", "_").Replace("+", "-");'

public string Base64Encode(string plainText)
    {
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
        return System.Convert.ToBase64String(plainTextBytes).Replace("=", "").Replace("/", "_").Replace("+", "-");
    }


    private string Sign(string message, string privateKey)
    {
        try
        {
            byte[] r = Encoding.UTF8.GetBytes(message);

            StringReader strReader = new StringReader(privateKey);
            PemReader pemReader = new PemReader(strReader);
            AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
            RsaKeyParameters privateRSAKey = (RsaKeyParameters)keyPair.Private;

            ISigner sig = SignerUtilities.GetSigner("SHA256withRSA");
            sig.Init(true, privateRSAKey);
            sig.BlockUpdate(r, 0, r.Length);

            return Convert.ToBase64String(sig.GenerateSignature()).Replace("=", "").Replace("/", "_").Replace("+", "-"); 
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }