DocuSign 获取 JWT 令牌 MEAN Stack

DocuSign Get JWT Token MEAN Stack

构建一个基本应用程序,用户可以在其中使用 MEAN Stack 找到服务提供商,协商结束后,协议将自动生成并且必须由双方签署。 卡在了生成 JWT 令牌以进行身份​​验证的过程中。 我遵循的步骤是:

  1. 生成一个 url 以获取用户同意并将其传递给前端。用户将被重定向并可以从那里授予权限。
var url = "https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature&client_id=42017946-xxxx-xxxx-xxxx-81b0ca97dc9a&redirect_uri=http://localhost:4200/authorization_code/callback";

res.status(200).json({
    status: 1,
    message: 'Fetched',
    value: url
});
  1. 使用 URL 中的代码成功重定向后,API 调用后端以生成 JWT 令牌。

  2. Token生成如下:

var jwt = require('jsonwebtoken');
var privateKey = fs.readFileSync(require('path').resolve(__dirname, '../../src/environments/docusign'));

const header = {
    "alg": "RS256",
    "typ": "JWT"
};

const payload = { 
    iss: '42017946-xxxx-xxxx-a5cd-xxxxxx', 
    sub: '123456', 
    iat: Math.floor(+new Date() / 1000), 
    aud: "account-d.docusign.com", 
    scope: "signature" 
};

var token = jwt.sign(payload, privateKey, { algorithm: 'RS256', header: header });

上面使用的私钥来自 docusign 管理面板。 iss -> 针对我的应用程序的集成密钥。 sub -> 管理面板用户符号下拉列表中的用户id

  1. 获取访问令牌
const axios = require('axios');

axios.post('https://account-d.docusign.com/oauth/token', 
    { 
        grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", 
        assertion: token 
    })
    .then(resposne => {
        console.log(response);
    })
    .catch(err => {
        if (err.response) {
            console.log(err);
        } else if (err.request) {} 
        else {}
    })

但我不断收到错误消息:{ error: 'invalid_grant', error_description: 'no_valid_keys_or_signatures' }

我建议使用 node.JS SDK 或 npm 包并使用 build-it JWT 方法进行身份验证。代码如下所示: (click here for GitHub example)

DsJwtAuth.prototype.getToken = async function _getToken() {
    // Data used
    // dsConfig.dsClientId
    // dsConfig.impersonatedUserGuid
    // dsConfig.privateKey
    // dsConfig.dsOauthServer
    const jwtLifeSec = 10 * 60, // requested lifetime for the JWT is 10 min
        scopes = "signature", // impersonation scope is implied due to use of JWT grant
        dsApi = new docusign.ApiClient();
    dsApi.setOAuthBasePath(dsConfig.dsOauthServer.replace('https://', '')); // it should be domain only.
    const results = await dsApi.requestJWTUserToken(dsConfig.dsClientId,
        dsConfig.impersonatedUserGuid, scopes, rsaKey,
        jwtLifeSec);

    const expiresAt = moment().add(results.body.expires_in, 's').subtract(tokenReplaceMin, 'm');
    this.accessToken = results.body.access_token;
    this._tokenExpiration = expiresAt;
    return {
        accessToken: results.body.access_token,
        tokenExpirationTimestamp: expiresAt
    };