Skip to content

Instantly share code, notes, and snippets.

@mitel
Last active April 28, 2019 18:44
Show Gist options
  • Save mitel/1eb3ad949955b7049e5b24fba4c06833 to your computer and use it in GitHub Desktop.
Save mitel/1eb3ad949955b7049e5b24fba4c06833 to your computer and use it in GitHub Desktop.
JWT signing with b64:false in node.js
const jose = require("node-jose");
const fs = require("fs");
const privateKEY = fs.readFileSync("./newRS256.key", "utf8");
// const publicKEY = fs.readFileSync("./newRS256.key.pub", "utf8");
const keystore = jose.JWK.createKeyStore();
const payload = {
foo: "test"
};
const jose_header = {
alg: "RS256",
b64: false,
kid: "kid",
typ: "JOSE"
};
async function sign() {
const privateJWK = await keystore.add(privateKEY, "pem");
const signed_jwt = await jose.JWS.createSign(
{
fields: jose_header,
//format: "compact"
format: "flatten"
},
{
key: privateJWK
}
)
.update(JSON.stringify(payload), "utf8")
.final();
return signed_jwt;
}
sign().then(signed => console.log(signed));
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA5i0tS/LQd5A5oBBHVlhurp+zxhoCHRWn6C6nXRV9KZsWNupU
98SSc8TrNuzrGrjys1ri6rg5CtehqB4I5H98Kv3FHNRBFdtGHWx4wArvE0niq1PF
PHgl0QnN0Za7MvIKNeirgEZh2ZI9u7jYcyBUN8P4iDjja9fUde8ezsSeDG8/qdGg
u49m7p0QeybseAbmCxONZF7A+LigUDrx+ZVPyNrcvmKnxqp+9OwtLK+XY+QH/ctj
t837wKcQE8f1KSZCbcDfLsS/aUgQWmrDadc0vrxSfEef7nr3/wYl2BYdFbYH/CjQ
+13ZcrMb4zJnPXNpIZSPEUjn12DS7Id6jJ4e8wIDAQABAoIBADzfbb9MM/2q0tKV
+4fGU18zNSQK9MwkUgeBbxma+ULrlPBpAJeWEPU0+s/H0FV83BK60pAosE1Bwaro
ZomnfXsRKfP53kyddOmLKlP/kEKVo7CTsvHPBJFH+nqXVmKeyDyBj/mYo4AawODI
fdXUkvE8jpnYEa0Po0ydNJSSQwoeUTSZcKkBwkQ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KEY-----
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5i0tS/LQd5A5oBBHVlhu
rp+zxhoCHRWn6C6nXRV9KZsWNupU98SSc8TrNuzrGrjys1ri6rg5CtehqB4I5H98
Kv3FHNRBFdtGHWx4wArvE0niq1PFPHgl0QnN0Za7MvIKNeirgEZh2ZI9u7jYcyBU
N8P4iDjja9fUde8ezsSeDG8/qdGgu49m7p0QeybseAbmCxONZF7A+LigUDrx+ZVP
yNrcvmKnxqp+9OwtLK+XY+QH/ctjt837wKcQE8f1KSZCbcDfLsS/aUgQWmrDadc0
vrxSfEef7nr3/wYl2BYdFbYH/CjQ+13ZcrMb4zJnPXNpIZSPEUjn12DS7Id6jJ4e
8wIDAQAB
-----END PUBLIC KEY-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment