Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can this work, when it generates a new key for every operation? #300

Open
Stokestack opened this issue Nov 8, 2024 · 0 comments
Open

Comments

@Stokestack
Copy link

Stokestack commented Nov 8, 2024

Thanks for the tutorial.

I'm getting verification failures, however. The problem is that you're never using the same key. First you generate a JWT with:

    //authenticate a user
    const payload = {
        id: user._id,
        name: username
    };
    const jwt =  await create({ alg: "HS512", typ: "JWT" }, { payload }, key);

The key value above is created on the fly by this, in apiKey.ts:

export const key = await crypto.subtle.generateKey(
    { name: "HMAC", hash: "SHA-512" },
    true,
    ["sign", "verify"],
  );

But later, when you try to verify the JWT from an incoming query, you create a whole new key and use it:

    //authenticate a user
    const payload = {
        id: user._id,
        name: username
    };
    const jwt =  await create({ alg: "HS512", typ: "JWT" }, { payload }, key); <-- key is generated again in apiKey.ts.

So you're encoding with one key and trying to decode with another. This always fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant