oakserver / oak

A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
https://oakserver.org
MIT License
5.17k stars 235 forks source link

getting error with jwt validate function after upgrading #478

Open ralyodio opened 2 years ago

ralyodio commented 2 years ago
$ deno run --allow-all --unstable index.ts
error: Uncaught (in promise) DataError: invalid key data
    at Object.opSync (deno:core/01_core.js:149:12)
    at importKeyHMAC (deno:ext/crypto/00_crypto.js:2204:34)
    at SubtleCrypto.importKey (deno:ext/crypto/00_crypto.js:883:18)
    at file:///home/ettinger/www/grazily/grazily-api/src/middleware/jwt.ts:3:33

here's the offending code:


export const validateJWT = async (
  { request, response, state }: RouterContext<any, any, any>,
  next: VoidFunction,
) => {
  const auth = request.headers.get("Authorization");
  if (!auth) return;
  const matches = /Bearer\s*(.*)/.exec(auth);
  let jwt;

  if (matches && matches.length > 1) {
    jwt = matches[1];
  }

  try {
    if (!jwt) {
      throw { error: "Missing JWT Token 😨" };
    }

    await verify(jwt, key)
      .then(async (payload) => {
        console.log("Valid JWT Token! 😎");
        state.user = payload;
        await next(); // The next() will continue with the excecution
      })
      .catch((e) => {
        console.log(e);
        response.body = { error: e.toString() };
        response.status = 401;
      });
  } catch (error) {
    console.error(error);
    response.body = error;
    response.status = 500;
  }
};

I got an error with this line: { request, response, state }: RouterContext, so I changed it to: { request, response, state }: RouterContext<any, any, any>, and now it crashes.

$ deno --version                          
deno 1.18.0+d4dd9ae (canary, x86_64-unknown-linux-gnu)
v8 9.8.177.6
typescript 4.5.2
ralyodio commented 2 years ago

Line 3 is actually empty

kitsonk commented 2 years ago

None of that stack trace contains any oak code, why do you think it is an issue with oak?