Ama*_*ila 12 javascript jwt jitsi jitsi-meet
谁能给我提供一个生成 jwt 令牌的示例,其中三个标头为(alg、kid、typ),格式如下:
{
"alg": "RS256",
"kid": "vpaas-magic-cookie-1fc542a3e4414a44b2611668195e2bfe/4f4910",
"typ": "JWT"
}
Run Code Online (Sandbox Code Playgroud)
在https://developer.8x8.com/jaas/docs/api-keys-jwt下。
Jwt 令牌会在几个小时的时间限制内过期,因此我试图找到一种在我的代码本身中生成令牌的方法。
最后,我的 javascript 如下所示,我在选项列表中添加 jwt 令牌以进行身份验证。
var options = {
roomName: "vpaas-magic-cookie-secretKey/Room123",
jwt: 'JWTTOKEN',
,
Run Code Online (Sandbox Code Playgroud)
根据我在https://jwt.io/下阅读的内容,我需要来自解码详细信息的编码密钥。根据生成令牌,我认为它使用 HS256 算法。在 javascript 中执行此操作的步骤是什么?
编辑:在用户回答后,我对他的代码做了一些更改,目前正在生成一半的 JWT 令牌。我正在使用服务器上已生成的令牌进行检查 - Jaas.8x8
<script>
const HMACSHA256 = (stringToSign, secret) => "not_implemented"
// The header typically consists of two parts:
// the type of the token, which is JWT, and the signing algorithm being used,
// such as HMAC SHA256 or RSA.
const header = {
"kid": "vpaas-magic-cookie-07fabede3674457a84c95fsecretcode/myroom001",
"alg": "RS256",
"typ": "JWT"
}
const encodedHeaders = btoa(JSON.stringify(header))
// create the signature part you have to take the encoded header,
// the encoded payload, a secret, the algorithm specified in the header,
// and sign that.
const signature = HMACSHA256(`${encodedHeaders}`, "mysecret")
console.log(`${encodedHeaders}.${signature}`)
</script>
Run Code Online (Sandbox Code Playgroud)
从代码生成的令牌是
eyJraWQiOiJ2cGFhcy1tYWdpYy1jb29raWUtMDdmYWJlZGUzNjc0NDU3YTg0Yzk1ZmE4MGIxNGY1ZDcvVGVzdFJhdW0wMDEiLCJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.not_implemented
Run Code Online (Sandbox Code Playgroud)
而已经在线生成的token是:
eyJraWQiOiJ2cGFhcy1tYWdpYy1jb29raWUtMDdmYWJlZGUzNjc0NDU3YTg0Yzk1ZmE4MGIxNGY1ZDcvMTg1ZDY2LVNBTVBMRV9BUFAiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJqaXRzaSIsImV4cCI6MTYyMDM4ODU3NiwibmJmIjoxNjIwMzgxMzcxLCJpc3MiOiJjaGF0Iiwicm9vbSI6IioiLCJzdWIiOiJ2cGFhcy1tYWdpYy1jb29raWUtMDdmYWJlZGUzNjc0NDU3YTg0Yzk1ZmE4MGIxNGY1ZDciLCJjb250ZXh0Ijp7ImZlYXR1cmVzIjp7ImxpdmVzdHJlYW1pbmciOmZhbHNlLCJvdXRib3VuZC1jYWxsIjpmYWxzZSwidHJhbnNjcmlwdGlvbiI6ZmFsc2UsInJlY29yZGluZyI6ZmFsc2V9LCJ1c2VyIjp7Im1vZGVyYXRvciI6dHJ1ZSwibmFtZSI6IlRlc3QgVXNlciIsImlkIjoiYXV0aDB8NjA5M2EyYzM3Zjc3MGEwMDcxMGE5YzY5IiwiYXZhdGFyIjoiIiwiZW1haWwiOiJ0ZXN0LnVzZXJAY29tcGFueS5jb20ifX19.aNqg_VLXyafH8Se5rThe6TLz0F2AEnJSmuoZBQ4fXEm1PMx4SBRpelJsrmL76D_jKS5NT-GkuPDVcDgLv6nx9G4ywjws1AH4Lkt0FcJ3eH2OjbFI2WxPzJF_tDJbtPme5LJmGZwEa509v2QD0r-kr31M7FZ83S-kz3O1xKc33FnMJwNlqvgCSN2S0QwF6R5J01zDk41gCk0wGr3DXAmlz0FtCU0qJ5nN9iMUpr5QUY1D-hRApWMhoLPmxkuqnQKLjGwgxU8lh33wq_Laqu7qV57lYrI27er_c42YePwuitWEAAshQU4Ylf2v8sVRv06kQdFPVvICVdsTTI-DLbc3aQ
Run Code Online (Sandbox Code Playgroud)
所以基本上前几个字符串是正确的,但其余的没有生成。我想这和我的秘密有关?这到底是什么?
The*_*ool 15
所有说明都写在这个页面上,https://jwt.io/introduction。
为简单起见,我没有实现 HMACSHA256 或使用库来执行此操作。你需要实现这个功能。
const HMACSHA256 = (stringToSign, secret) => "not_implemented"
// The header typically consists of two parts:
// the type of the token, which is JWT, and the signing algorithm being used,
// such as HMAC SHA256 or RSA.
const header = {
"alg": "HS256",
"typ": "JWT"
}
const encodedHeaders = btoa(JSON.stringify(header))
// The second part of the token is the payload, which contains the claims.
// Claims are statements about an entity (typically, the user) and
// additional data. There are three types of claims:
// registered, public, and private claims.
const claims = {
"role": "admin"
}
const encodedPlayload = btoa(JSON.stringify(claims))
// create the signature part you have to take the encoded header,
// the encoded payload, a secret, the algorithm specified in the header,
// and sign that.
const signature = HMACSHA256(`${encodedHeaders}.${encodedPlayload}`, "mysecret")
const encodedSignature = btoa(signature)
const jwt = `${encodedHeaders}.${encodedPlayload}.${encodedSignature}`
console.log({jwt})
Run Code Online (Sandbox Code Playgroud)
请注意,我的答案与您的问题不同,因为它使用的是 HS256 而不是 RS256。如果您想了解更多有关差异的信息,可以查看此处:RS256 与 HS256:有什么区别?。
归档时间: |
|
查看次数: |
29470 次 |
最近记录: |