使用 RSA 私钥在 Powershell 中将 JSON Web 令牌加密为 RSA RS256



我被困住了,我正在尝试为Docusign签署一个Json Web Token。 https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-jsonwebtoken Docusign只提供RSA私钥和公钥哈希。就是这样。JWT 必须使用 RS256 签名。

我找到了一个 JWT 模块 https://www.powershellgallery.com/packages/JWT/1.1.0 但这需要我安装证书。但我所拥有的只是密钥哈希。

利用其他一些代码,我发现我能够创建一个 JWT 令牌 ,尽管使用了错误的算法。 https://www.reddit.com/r/PowerShell/comments/8bc3rb/generate_jwt_json_web_token_in_powershell/我一直在尝试通过创建一个新对象来修改它以使用 RSACryto 提供程序,但没有成功。

我尝试创建一个新对象,看看是否可以导入密钥,以便我可以对令牌进行签名。但我似乎做不到。

$rsa = New-Object -TypeName System.Security.Cryptography.RSACryptoServiceProvider
$keyhash = "-----BEGIN RSA PRIVATE KEY-----
XXXXXX
-----END RSA PRIVATE KEY-----"
$blob = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($keyhash))

我尝试使用 ImportCspBlob 方法,但它要求将字符串转换为字节,但我似乎也无法做到这一点。

我不确定我是否以正确的方式接近这一点。我一直收到任何错误

Exception calling "ImportCspBlob" with "1" argument(s): "Bad Version of provider.
or
Cannot convert value to type "System.Byte". Error: "Input string was not in a correct format."

编辑: 我有一个使用 Node.js 的解决方法,尽管 id 仍然想看看是否有可能在 Powershell 中本地完成我尝试做的事情。这项工作可能对某些人来说很有用,因为似乎没有很多使用Powershell和Docusign API的参考。

我在这里找到了一个节点.JS脚本,该脚本使用 RS256 算法创建 JWT 令牌。 https://github.com/BlitzkriegSoftware/NodejwtRSA ,

我删除了所有额外的内容,因此控制台的输出只是令牌,并将相关范围添加到"有效载荷数据"中,并在符号选项下更新了 sub、aud 和 iss。我的 RSA 私钥存储在本地系统上的文件中。

nodejs 脚本 - 我在下面修改的版本

'use strict';
const path = require('path');
const fs = require('fs');
// https://github.com/auth0/node-jsonwebtoken
var jwt = require('jsonwebtoken');
// Private Key (must read as utf8)
var privateKey = fs.readFileSync('./rsatest.pk','utf8');
// Sample claims payload with user defined fields (this can be anything, but briefer is better):
var payload = { };
// Populate with fields and data
payload.scope = "signature impersonation";
// Values for the rfc7519 fields
var iss = "XXXXX-XXXX-XXX-XXX-XXX";
var sub = "XXXXX-XXXX-XXX-XXX-XXX";
var aud = "account-d.docusign.com";
// Expiration timespan: https://github.com/auth0/node-jsonwebtoken#token-expiration-exp-claim
var exp = "1h";
// JWT Token Options, see: https://tools.ietf.org/html/rfc7519#section-4.1 for the meaning of these
// Notice the `algorithm: "RS256"` which goes with public/private keys
var signOptions = {
issuer : iss,
subject: sub,
audience: aud,
expiresIn: exp,
algorithm: "RS256"
};
var token = jwt.sign(payload, privateKey, signOptions);
console.log(token)
process.exitCode = 0;

我从 Powershell 调用了它,并将访问令牌反馈回我的脚本,以便我可以获取我的访问令牌并开始进行 API 调用。

#get the JWT token
$token = & node C:tempnodejwt.js
# Generate Header for API calls.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body ="grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=$token"
$authuri = "https://account-d.docusign.com/oauth/token"
#send the JWT and get the access token 
$accesstoken = Invoke-RestMethod -Method post -Uri $authuri -Headers $headers -Body $body -Verbose
$getheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$getheaders.Add("Content-Type", "application/json")
$getheaders.Add('Authorization','Bearer ' + $accesstoken.access_token)    
$geturi = "https://account-d.docusign.com/oauth/userinfo"
#use the access token to make api calls 
Invoke-RestMethod -Method get -Uri $geturi -Headers $getheaders 

我也遇到了同样的问题,被卡住了。我用Python来获取JWT。请务必安装 PyJWT 和加密库。

$ pip install PyJWT
$ pip install pyjwt[crypto]

import time
import jwt
from datetime import datetime, timedelta
private_key= b'-----BEGIN PRIVATE KEY-----
`-----END PRIVATE KEY-----n'
encoded_jwt = jwt.encode({
"iss":"<service account e-mail>",
"scope":"",
"aud":"",
"exp":int(time.time())+3600,
"iat":int(time.time())
}, private_key, algorithm='RS256')
encoded_jwt

最新更新