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

getting error with jwt validate function after upgrading #478

Open
ralyodio opened this issue Feb 19, 2022 · 2 comments
Open

getting error with jwt validate function after upgrading #478

ralyodio opened this issue Feb 19, 2022 · 2 comments

Comments

@ralyodio
Copy link

ralyodio commented Feb 19, 2022

$ 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
Copy link
Author

ralyodio commented Feb 19, 2022

Line 3 is actually empty

@kitsonk
Copy link
Collaborator

kitsonk commented Feb 19, 2022

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

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

2 participants