DocuSign获取JWT代币平均堆栈



构建一个基本应用程序,用户可以使用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' }

我建议使用该节点。JS SDK或npm包,并使用build-it JWT方法进行身份验证。代码如下所示:(点击此处查看GitHub示例(

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
};

最新更新