Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

a way to verify using a pem ? #1029

Open
walshe opened this issue Aug 5, 2021 · 1 comment
Open

a way to verify using a pem ? #1029

walshe opened this issue Aug 5, 2021 · 1 comment

Comments

@walshe
Copy link

walshe commented Aug 5, 2021

I usually verify a jwt using a pem ( jwt.verify(token, pem, { algorithms: [completeDecodedToken.header.alg] });) calculated by using kid in header and using that to find correct jwk which can be used to create a pem... but the verify() in your lib needs an audience(s) in the second param

const completeDecodedToken = jwtJsDecode.jwtDecode(token);

  console.log("decoded token", completeDecodedToken);

  if(!completeDecodedToken){
    throw new Error(`Could not decode JWT: ${token}`);
  }
  
  // decode token and use kid to find correct jwk
  const jwk = keys.filter(jwk => jwk.kid == completeDecodedToken.header.kid)

  // verify the token
  if(!jwk.length){
    throw new Error(`Could not find matching jwk for kid ${completeDecodedToken.header.kid}`);
  }

  //use kid to create a pem (https://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file)
  const pem = jwkToPem(jwk[0]);

  console.log('verifying token using pem..')

  try{
    const jwt = require('jsonwebtoken'); // for seom reason this wont work with okta tokens 
    jwt.verify(token, pem, { algorithms: [completeDecodedToken.header.alg] });
  }catch(err){

    console.warn('token verification failed', err.message, err.name);
    context.fail("Unauthorized");
    return;
  }
@arvindkrishnakumar-okta
Copy link

@walshe Thanks for raising this!

@shuowu-okta @denysoblohin-okta can you address this question?

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

No branches or pull requests

2 participants