在Postman Pre-request脚本中生成RSA JWT

qq24tv8q  于 12个月前  发布在  Postman
关注(0)|答案(2)|浏览(226)

我有下面的预请求脚本,我正在使用它来尝试为Google API生成JWT- Google使用RS 256加密,这是我认为我卡住的地方-CryptoJS似乎只支持HmacSHA 256-任何建议都将是有帮助的:
以下是Postman的预请求脚本:

function base64url(source) {
    // Encode in classical base64
    encodedSource = CryptoJS.enc.Base64.stringify(source);

    // Remove padding equal characters
    encodedSource = encodedSource.replace(/=+$/, '');

    // Replace characters according to base64url specifications
    encodedSource = encodedSource.replace(/\+/g, '-');
    encodedSource = encodedSource.replace(/\//g, '_');

    return encodedSource;
}

function addIAT(request) {
    var iat = Math.floor(Date.now() / 1000) - 100;
    data.iat = iat;
    return data;
}

function addEXP(request) {
    var exp = Math.floor(Date.now() / 1000) + 3300;
    data.exp = exp;
    return data;
}

var header = {
    "alg": "RS256",
    "typ": "JWT",
    "kid": "xxx"
};

var data = {
     "iss": "[email protected]",
     "aud": "https://oauth2.googleapis.com/token",
     "scope": "https://www.googleapis.com/auth/cloud-platform"

};

data = addIAT(data);
data = addEXP(data);

var privateKEY = "-----BEGIN PRIVATE KEY-----xxxxxxxxxxxxxxxLcGGNkna2Y3URoT0vDNneqzaasmJk4JZ97BcFLOulTihA49z8zmQKBgQDKX9AWS88cnfyiXSxtRNUFYvN4SzMDTJ1o59Gm6Sk77t7Ylfm+8PKA00SQeN7FuU/cbU4PkbAyzcp7eAGE3KXoLC/pWQ14srWGAsQkti1PmBo400ajRQReJPs3XxIl3yl4swlRgn+w9x6xy3CRnWrZRQSxRrtdDkBJcp7Lml+4mwKBgQCCeiTsktlMTOy9LqxOUMh6Lt7Z5jceNSwusW8Z4YVsewiSsRezufLBRcTywifgPOyUTP3S7etEfW2CKF0smpM0drfxd/3Ic7oKr7ESY5zwNcV7Q3NUGxaqGy8yoxEhKkLsYkOzYUdyNyfJd5Sh8yq7ICrX7/UGkVLOi44VrdaluQKBgC3kmH3V5zvoH/h6BK8q4tv72pa3BvSClVfK6mJdkbpDq0mWiTJh1bydLHlOz8YrBg9IwmEJetmqjXZ+emm01/LUwnC6fzGV5VBkpDJnFdNs/NVSJDy2VA09ebLO3oC0IOV8RGq1m1t4Tv+m0PpUpnxrCGtjTO4HY1DEq3okofxtAoGABjq4QegVIlImU5LSAEKgnUiwA1CHGW3+ZzfCczAv2VRfk/DSlYLmsxLRIfjsCVEo79NiVGyIsKmt5TJRxVLXp++ydKCEN/YRrjqEFgHNoH0rDCuV/IKAeN17/TYKphuebSX6mVsfo7GXI1kSoGJkDDnPKR4peiIF/YC9BTqQgIs=-----END PRIVATE KEY-----"

var secret = 'myjwtsecret';

// encode header
var stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header));
var encodedHeader = base64url(stringifiedHeader);

// encode data
var stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data));
var encodedData = base64url(stringifiedData);

//encode privatekey
//var stringifiedPrivatekey = CryptoJS.enc.Utf8.parse(JSON.stringify(privateKEY));
//var encodedPrivatekey = base64url(stringifiedPrivatekey);

// build token
var token = encodedHeader + "." + encodedData

// sign token
//var signature = CryptoJS.HmacSHA256(token, secret);
//signature = base64url(signature);
var signature = CryptoJS.RS256(token , privateKEY);
signedToken = base64url(signature);

var jwt = token + "." + signedToken

postman.setEnvironmentVariable("payload", jwt);

字符串

xfb7svmp

xfb7svmp1#

我已经在我项目中发现这个问题好几次了,所以我决定创建一个简单的方法来解决这个问题,在这里我已经发布了一个“库”来轻松地执行加密操作,比如在postman的“预请求”和“测试”脚本中生成jwt、PKCE挑战......,如果你有任何疑问,请看一看并联系我。
致以最诚挚的问候。

d6kp6zgx

d6kp6zgx2#

您可以在您的收藏中的授权选项卡下选择:

** JWT **:Bearer
算法:RS 256
私钥:{{SECRET_KEY}}(直接加载您的文件或设置为集合变量)
Payload:{{PAYLOAD}}(在预请求脚本中设置)

然后在您的Pre-request Script页签中:

// Load your api key
const apiKey =  pm.collectionVariables.get('API_KEY')

// Common payload params, create the ones you need
const uri = pm.request.url.getPathWithQuery()
const sub = apiKey
const iat = Math.floor(Date.now() / 1000)
const exp = iat + 20

const payload = {
    uri,
    iat,
    exp,
    sub,
}

// Set your payload, don't forget to stringify it
pm.collectionVariables.set("PAYLOAD",JSON.stringify(payload))

字符串
您可以将相同的逻辑应用于JWT Headers

相关问题