【问题标题】:DocuSign Get JWT Token MEAN StackDocuSign 获取 JWT 令牌 MEAN Stack
【发布时间】:2021-01-07 00:52:22
【问题描述】:

构建一个基本的应用程序,用户可以在其中使用 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. 令牌生成如下:

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 docusignapi mean


    【解决方案1】:

    我建议使用 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
        };
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 1970-01-01
      • 2021-02-12
      • 2020-11-01
      • 2021-10-24
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多