不正确的base64url
JWT指南在这里 – https://scotch.io/tutorials/the-anatomy-of-a-json-web-token#header – 说他们在这个上运行base64url
:
{ "typ": "JWT", "alg": "HS256" }
他们最终得出这个结论:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
我试试这个代码:
var b64u = require("base64url") var rez = b64u(JSON.stringify({ "typ": "JWT", "alg": "HS256" })); var shouldbe = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'; console.log(rez); console.log(shouldbe); console.log(rez == shouldbe);
正如在这里看到的在线testing: https : //tonicdev.com/56c5484d7a4ea10d0002c623/5733af59234d9d1200d8c818
但是他们不匹配。
有没有人看到任何简单的问题?
Base64输出取决于您从JSON.stringify
调用中获得的string的JSON.stringify
。
作为参考,这是一个使用预先构build的JSONstring的工作示例。
let expected = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'; let str1 = '{"alg":"HS256","typ":"JWT"}'; let str2 = '{"typ":"JWT","alg":"HS256"}'; // note that you don't need a library to Base64 encode strings in node let base64str1 = new Buffer(str1).toString('base64'); let base64str2 = new Buffer(str2).toString('base64'); console.log(base64str1); // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' console.log(base64str2); // 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9' console.log('base64str1 equals expected?', base64str1 === expected); // true console.log('base64str2 equals expected?', base64str2 === expected); // false