使用公钥/私钥编码 JWT 令牌

时间:2021-05-10 12:31:27

标签: javascript encoding jwt jitsi jitsi-meet

我正在尝试从 https://jwt.io/ 的解码输入(RS256 算法)中获取编码的 JWT (https://jwt.io/) 令牌。问题是我有公钥和私钥,但我不确定如何在 javascript 中使用它们进行编码。我正在使用 crypto.js 和 base64,但如何在编码时添加公钥和私钥?

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/hmac-sha256.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-base64.min.js"></script>

<script>

    // 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": "RS256",
        "kid": "vpaas-magic-cookie-SecretKey/da330b",
        "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 = {
        "aud": "jitsi",
        "exp": 1620649654,
        "nbf": 1620642449,
        "iss": "chat",
        "room": "*",
        "sub": "vpaas-magic-cookie-SecretKey",
        "context": {
            "features": {
                "livestreaming": true,
                "outbound-call": true,
                "transcription": true,
                "recording": true
            },
            "user": {
                "moderator": true,
                "name": "userName",
                "id": "AuthenticationID",
                "avatar": "",
                "email": "userEmail"
            }
        }
    }

const encodedPlayload = btoa(JSON.stringify(claims))

var privateKey = "./privateKey.key";
var publicKey = "./publicKey.key";

</script>

0 个答案:

没有答案