2017-10-16 141 views
0

我如何使用k6JWT簽名編碼JSON對象?JWT使用k6簽名

export default function() { 

    const mySecret = "aaaaaaa"; 
    const token = jwt.sign({ foo: 'bar' }, mySecret, { algorithm: 'RS256'}); 

} 

回答

2

您可以使用內置的k6/encodingk6/crypto模塊,JWTs工作。

由於k6/crypto模塊尚未支持公鑰加密,因此只能使用共享祕密對您的JWT進行簽名(不需要在純JS中進行加密)。

下面是一個示例腳本:

import crypto from "k6/crypto"; 
import encoding from "k6/encoding"; 

const algToHash = { 
    HS256: "sha256", 
    HS384: "sha384", 
    HS512: "sha512" 
}; 

function sign(data, hashAlg, secret) { 
    let hasher = crypto.createHMAC(hashAlg, secret); 
    hasher.update(data); 

    // Some manual base64 rawurl encoding as `Hasher.digest(encodingType)` 
    // doesn't support that encoding type yet. 
    return hasher.digest("base64").replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, ""); 
} 

function encode(payload, secret, algorithm) { 
    algorithm = algorithm || "HS256"; 
    let header = encoding.b64encode(JSON.stringify({ typ: "JWT", alg: algorithm }), "rawurl"); 
    payload = encoding.b64encode(JSON.stringify(payload), "rawurl"); 
    let sig = sign(header + "." + payload, algToHash[algorithm], secret); 
    return [header, payload, sig].join("."); 
} 

function decode(token, secret, algorithm) { 
    let parts = token.split('.'); 
    let header = JSON.parse(encoding.b64decode(parts[0], "rawurl")); 
    let payload = JSON.parse(encoding.b64decode(parts[1], "rawurl")); 
    algorithm = algorithm || algToHash[header.alg]; 
    if (sign(parts[0] + "." + parts[1], algorithm, secret) != parts[2]) { 
     throw Error("JWT signature verification failed"); 
    } 
    return payload; 
} 

export default function() { 
    let message = { key2: "value2" }; 
    let token = encode(message, "secret"); 
    console.log("encoded", token); 
    let payload = decode(token, "secret"); 
    console.log("decoded", JSON.stringify(payload)); 
}